Ejemplo n.º 1
0
 private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter) {
   try {
     if (parameter != null && keyStatement != null && keyStatement.getKeyProperties() != null) {
       String keyProperty =
           keyStatement.getKeyProperties()[0]; // just one key property is supported
       final Configuration configuration = ms.getConfiguration();
       final MetaObject metaParam = configuration.newMetaObject(parameter);
       if (keyProperty != null && metaParam.hasSetter(keyProperty)) {
         // Do not close keyExecutor.
         // The transaction will be closed by parent executor.
         Executor keyExecutor =
             configuration.newExecutor(executor.getTransaction(), ExecutorType.SIMPLE);
         List<Object> values =
             keyExecutor.query(
                 keyStatement, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
         if (values.size() == 0) {
           throw new ExecutorException("SelectKey returned no data.");
         } else if (values.size() > 1) {
           throw new ExecutorException("SelectKey returned more than one value.");
         } else {
           metaParam.setValue(keyProperty, values.get(0));
         }
       }
     }
   } catch (ExecutorException e) {
     throw e;
   } catch (Exception e) {
     throw new ExecutorException(
         "Error selecting key or setting result to parameter object. Cause: " + e, e);
   }
 }
Ejemplo n.º 2
0
 private Executor newExecutor() throws SQLException {
   final Environment environment = configuration.getEnvironment();
   if (environment == null)
     throw new ExecutorException(
         "ResultLoader could not load lazily.  Environment was not configured.");
   final DataSource ds = environment.getDataSource();
   if (ds == null)
     throw new ExecutorException(
         "ResultLoader could not load lazily.  DataSource was not configured.");
   final TransactionFactory transactionFactory = environment.getTransactionFactory();
   final Transaction tx = transactionFactory.newTransaction(ds, null, false);
   return configuration.newExecutor(tx, ExecutorType.SIMPLE);
 }
Ejemplo n.º 3
0
 private void init() throws SQLException, TransactionException {
   // Open JDBC Transaction
   Connection connection = dataSource.getConnection();
   connection = ConnectionLogger.newInstance(connection);
   if (connection == null) {
     throw new TransactionException(
         "JdbcTransaction could not start transaction.  Cause: The DataSource returned a null connection.");
   }
   // Isolation Level
   isolationLevel.applyIsolationLevel(connection);
   // AutoCommit
   if (connection.getAutoCommit()) {
     connection.setAutoCommit(false);
   }
   executor =
       configuration.newExecutor(
           new org.apache.ibatis.transaction.jdbc.JdbcTransaction(connection, false));
 }
Ejemplo n.º 4
0
  private SqlSession openSessionFromDataSource(
      ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
      DataSource ds = ShardMapperProxy.getCurrentDs();
      final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory =
          getTransactionFactoryFromEnvironment(environment);

      tx = transactionFactory.newTransaction(ds, level, autoCommit);

      final Executor executor = configuration.newExecutor(tx, execType);
      return new DefaultSqlSession(configuration, executor);
    } catch (Exception e) {
      closeTransaction(tx); // may have fetched a connection so lets call close()
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }