Package com.ibatis.sqlmap.client

Examples of com.ibatis.sqlmap.client.SqlMapSession


    // we run against a TransactionAwareDataSourceProxy underneath, but unfortunately
    // we still need it to make iBATIS batch execution work properly: If iBATIS
    // doesn't recognize an existing transaction, it automatically executes the
    // batch for every single statement...

    SqlMapSession session = this.sqlMapClient.openSession();
    if (logger.isDebugEnabled()) {
      logger.debug("Opened SqlMapSession [" + session + "] for iBATIS operation");
    }
    Connection ibatisCon = null;

    try {
      Connection springCon = null;
      boolean transactionAware = (dataSource instanceof TransactionAwareDataSourceProxy);

      // Obtain JDBC Connection to operate on...
      try {
        ibatisCon = session.getCurrentConnection();
        if (ibatisCon == null) {
          springCon = (transactionAware ?
              dataSource.getConnection() : DataSourceUtils.doGetConnection(dataSource));
          session.setUserConnection(springCon);
          if (logger.isDebugEnabled()) {
            logger.debug("Obtained JDBC Connection [" + springCon + "] for iBATIS operation");
          }
        }
        else {
          if (logger.isDebugEnabled()) {
            logger.debug("Reusing JDBC Connection [" + ibatisCon + "] for iBATIS operation");
          }
        }
      }
      catch (SQLException ex) {
        throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
      }

      // Execute given callback...
      try {
        return session.queryForList(queryId, parameters);
      }
      catch (SQLException ex) {
        SQLExceptionTranslator sqlStateSQLExceptionTranslator;

        if(dataSource != null) {
          sqlStateSQLExceptionTranslator = new SQLStateSQLExceptionTranslator();
        } else {
          sqlStateSQLExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource);
        }

        throw sqlStateSQLExceptionTranslator.translate("SqlMapClient operation", null, ex);
      }
      finally {
        try {
          if (springCon != null) {
            if (transactionAware) {
              springCon.close();
            }
            else {
              DataSourceUtils.doReleaseConnection(springCon, dataSource);
            }
          }
        }
        catch (Throwable ex) {
          logger.debug("Could not close JDBC Connection", ex);
        }
      }

      // Processing finished - potentially session still to be closed.
    }
    finally {
      // Only close SqlMapSession if we know we've actually opened it
      // at the present level.
      if (ibatisCon == null) {
        session.close();
      }
    }
  }
View Full Code Here


    // we run against a TransactionAwareDataSourceProxy underneath, but unfortunately
    // we still need it to make iBATIS batch execution work properly: If iBATIS
    // doesn't recognize an existing transaction, it automatically executes the
    // batch for every single statement...

    SqlMapSession session = this.sqlMapClient.openSession();
    if (logger.isDebugEnabled()) {
      logger.debug("Opened SqlMapSession [" + session + "] for iBATIS operation");
    }
    Connection ibatisCon = null;

    try {
      Connection springCon = null;
      boolean transactionAware = (dataSource instanceof TransactionAwareDataSourceProxy);

      // Obtain JDBC Connection to operate on...
      try {
        ibatisCon = session.getCurrentConnection();
        if (ibatisCon == null) {
          springCon = (transactionAware ?
              dataSource.getConnection() : DataSourceUtils.doGetConnection(dataSource));
          session.setUserConnection(springCon);
          if (logger.isDebugEnabled()) {
            logger.debug("Obtained JDBC Connection [" + springCon + "] for iBATIS operation");
          }
        }
        else {
          if (logger.isDebugEnabled()) {
            logger.debug("Reusing JDBC Connection [" + ibatisCon + "] for iBATIS operation");
          }
        }
      }
      catch (SQLException ex) {
        throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
      }

      // Execute given callback...
      try {
        session.startBatch();
        for (T item : items) {
          session.update(statementId, item);
        }
        try {
          return session.executeBatchDetailed();
        } catch (BatchException e) {
          throw e.getBatchUpdateException();
        }
      }
      catch (SQLException ex) {
        SQLExceptionTranslator sqlStateSQLExceptionTranslator;

        if(dataSource != null) {
          sqlStateSQLExceptionTranslator = new SQLStateSQLExceptionTranslator();
        } else {
          sqlStateSQLExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource);
        }

        throw sqlStateSQLExceptionTranslator.translate("SqlMapClient operation", null, ex);
      }

      // Processing finished - potentially session still to be closed.
    }
    finally {
      // Only close SqlMapSession if we know we've actually opened it
      // at the present level.
      if (ibatisCon == null) {
        session.close();
      }
    }
  }
View Full Code Here

    writer.afterPropertiesSet();
  }

  @Test
  public void testWriteAndFlush() throws Exception {
    SqlMapSession sms = mock(SqlMapSession.class);
    when(smc.openSession()).thenReturn(sms);
    sms.close();
    when(sms.getCurrentConnection()).thenReturn(null);
    sms.setUserConnection(null);
    sms.startBatch();
    when(sms.update("updateFoo", new Foo("bar"))).thenReturn(-2);
    List<BatchResult> results = Collections.singletonList(new BatchResult("updateFoo", "update foo"));
    results.get(0).setUpdateCounts(new int[] {1});
    when(sms.executeBatchDetailed()).thenReturn(results);
    writer.write(Collections.singletonList(new Foo("bar")));
  }
View Full Code Here

    writer.write(Collections.singletonList(new Foo("bar")));
  }

  @Test
  public void testWriteAndFlushWithEmptyUpdate() throws Exception {
    SqlMapSession sms = mock(SqlMapSession.class);
    when(smc.openSession()).thenReturn(sms);
    sms.close();
    when(sms.getCurrentConnection()).thenReturn(null);
    sms.setUserConnection(null);
    sms.startBatch();
    when(sms.update("updateFoo", new Foo("bar"))).thenReturn(1);
    List<BatchResult> results = Collections.singletonList(new BatchResult("updateFoo", "update foo"));
    results.get(0).setUpdateCounts(new int[] {0});
    when(sms.executeBatchDetailed()).thenReturn(results);
    try {
      writer.write(Collections.singletonList(new Foo("bar")));
      fail("Expected EmptyResultDataAccessException");
    }
    catch (EmptyResultDataAccessException e) {
View Full Code Here

  }

  @Test
  public void testWriteAndFlushWithFailure() throws Exception {
    final RuntimeException ex = new RuntimeException("ERROR");
    SqlMapSession sms = mock(SqlMapSession.class);
    when(smc.openSession()).thenReturn(sms);
    sms.close();
    when(sms.getCurrentConnection()).thenReturn(null);
    sms.setUserConnection(null);
    sms.startBatch();
    when(sms.update("updateFoo", new Foo("bar"))).thenThrow(ex);
    try {
      writer.write(Collections.singletonList(new Foo("bar")));
      fail("Expected RuntimeException");
    }
    catch (RuntimeException e) {
View Full Code Here

    public DataSource getDataSource() {
        return client.getDataSource();
    }

    public SqlMapSession openSession() {
        SqlMapSession session = client.openSession();
        IbatisUtils.setClientImpl(session, clientImplWrapper);
        return new SqlMapSessionWrapper(client, session);
    }
View Full Code Here

        IbatisUtils.setClientImpl(session, clientImplWrapper);
        return new SqlMapSessionWrapper(client, session);
    }

    public SqlMapSession openSession(Connection conn) {
        SqlMapSession session = client.openSession(conn);
        IbatisUtils.setClientImpl(session, clientImplWrapper);
        return new SqlMapSessionWrapper(client, session);
    }
View Full Code Here

        IbatisUtils.setClientImpl(session, clientImplWrapper);
        return new SqlMapSessionWrapper(client, session);
    }

    public SqlMapSession getSession() {
        SqlMapSession session = client.getSession();
        IbatisUtils.setClientImpl(session, clientImplWrapper);
        return new SqlMapSessionWrapper(client, session);
    }
View Full Code Here

      throw new IllegalStateException(e.getMessage(), e);
    }
  }

  public SqlMapSession openSession(Connection conn) {
    SqlMapSession session = raw.openSession(conn);
    IbatisUtils.setClientImpl(session, this);
    return new SqlMapSessionWrapper(raw, session);
  }
View Full Code Here

    IbatisUtils.setClientImpl(session, this);
    return new SqlMapSessionWrapper(raw, session);
  }

  public SqlMapSession getSession() {
    SqlMapSession session = raw.getSession();
    IbatisUtils.setClientImpl(session, this);
    return new SqlMapSessionWrapper(raw, session);
  }
View Full Code Here

TOP

Related Classes of com.ibatis.sqlmap.client.SqlMapSession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.