Search in sources :

Example 1 with TransactionListener

use of io.requery.TransactionListener in project requery by requery.

the class ThreadLocalTransaction method begin.

@Override
public Transaction begin(TransactionIsolation isolation) {
    EntityTransaction transaction = threadLocal.get();
    if (transaction == null) {
        EntityCache cache = configuration.getCache();
        TransactionMode mode = configuration.getTransactionMode();
        TransactionListener listener = new CompositeTransactionListener(configuration.getTransactionListenerFactories());
        transaction = mode == TransactionMode.MANAGED ? new ManagedTransaction(listener, configuration, cache) : new ConnectionTransaction(listener, configuration, cache, mode != TransactionMode.NONE);
        threadLocal.set(transaction);
    }
    transaction.begin(isolation);
    return this;
}
Also used : TransactionListener(io.requery.TransactionListener) EntityCache(io.requery.EntityCache)

Example 2 with TransactionListener

use of io.requery.TransactionListener in project requery by requery.

the class InsertReturningOperation method evaluate.

@Override
public Result<Tuple> evaluate(final QueryElement<Result<Tuple>> query) {
    DefaultOutput generator = new DefaultOutput(configuration, query);
    String sql = generator.toSql();
    BoundParameters parameters = generator.parameters();
    int count;
    PreparedStatement statement = null;
    Set<Type<?>> types = query.entityTypes();
    TransactionListener transactionListener = new CompositeTransactionListener(configuration.getTransactionListenerFactories());
    try {
        Connection connection = configuration.getConnection();
        StatementListener listener = configuration.getStatementListener();
        if (query.insertType() == InsertType.SELECT) {
            statement = connection.prepareStatement(sql, Statement.NO_GENERATED_KEYS);
        } else {
            statement = prepare(sql, connection);
        }
        mapParameters(statement, parameters);
        listener.beforeExecuteUpdate(statement, sql, parameters);
        transactionListener.beforeCommit(types);
        count = statement.executeUpdate();
        listener.afterExecuteUpdate(statement, count);
        transactionListener.afterCommit(types);
        if (selection == null || selection.isEmpty() || query.insertType() == InsertType.SELECT) {
            connection.close();
            MutableTuple tuple = new MutableTuple(1);
            tuple.set(0, NamedExpression.ofInteger("count"), count);
            return new CollectionResult<Tuple>(tuple);
        } else {
            ResultSet results = statement.getGeneratedKeys();
            return new GeneratedKeyResult(configuration, selection, connection, results, count);
        }
    } catch (Exception e) {
        throw StatementExecutionException.closing(statement, e, sql);
    }
}
Also used : TransactionListener(io.requery.TransactionListener) DefaultOutput(io.requery.sql.gen.DefaultOutput) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) MutableTuple(io.requery.query.MutableTuple) InsertType(io.requery.query.element.InsertType) Type(io.requery.meta.Type) ResultSet(java.sql.ResultSet)

Example 3 with TransactionListener

use of io.requery.TransactionListener in project requery by requery.

the class CompletableEntityStoreTest method setup.

@Before
public void setup() throws SQLException {
    Platform platform = new HSQL();
    CommonDataSource dataSource = DatabaseType.getDataSource(platform);
    EntityModel model = io.requery.test.model.Models.DEFAULT;
    final TransactionListener transactionListener = new TransactionListener() {

        @Override
        public void beforeBegin(TransactionIsolation isolation) {
        }

        @Override
        public void afterBegin(TransactionIsolation isolation) {
            transactionState = TransactionState.BEGIN;
        }

        @Override
        public void beforeCommit(Set<Type<?>> types) {
        }

        @Override
        public void afterCommit(Set<Type<?>> types) {
            transactionState = TransactionState.COMMIT;
        }

        @Override
        public void beforeRollback(Set<Type<?>> types) {
        }

        @Override
        public void afterRollback(Set<Type<?>> types) {
            transactionState = TransactionState.ROLLBACK;
        }
    };
    Configuration configuration = new ConfigurationBuilder(dataSource, model).useDefaultLogging().setStatementCacheSize(10).setBatchUpdateSize(50).setWriteExecutor(Executors.newSingleThreadExecutor()).addTransactionListenerFactory(new Supplier<TransactionListener>() {

        @Override
        public TransactionListener get() {
            return transactionListener;
        }
    }).build();
    data = new CompletableEntityStore<>(new EntityDataStore<Persistable>(configuration));
    SchemaModifier tables = new SchemaModifier(configuration);
    tables.createTables(TableCreationMode.DROP_CREATE);
}
Also used : TransactionListener(io.requery.TransactionListener) ConfigurationBuilder(io.requery.sql.ConfigurationBuilder) HashSet(java.util.HashSet) Set(java.util.Set) Platform(io.requery.sql.Platform) Configuration(io.requery.sql.Configuration) HSQL(io.requery.sql.platform.HSQL) TransactionIsolation(io.requery.TransactionIsolation) EntityModel(io.requery.meta.EntityModel) CommonDataSource(javax.sql.CommonDataSource) SchemaModifier(io.requery.sql.SchemaModifier) EntityDataStore(io.requery.sql.EntityDataStore) Supplier(io.requery.util.function.Supplier) Before(org.junit.Before)

Aggregations

TransactionListener (io.requery.TransactionListener)3 EntityCache (io.requery.EntityCache)1 TransactionIsolation (io.requery.TransactionIsolation)1 EntityModel (io.requery.meta.EntityModel)1 Type (io.requery.meta.Type)1 MutableTuple (io.requery.query.MutableTuple)1 InsertType (io.requery.query.element.InsertType)1 Configuration (io.requery.sql.Configuration)1 ConfigurationBuilder (io.requery.sql.ConfigurationBuilder)1 EntityDataStore (io.requery.sql.EntityDataStore)1 Platform (io.requery.sql.Platform)1 SchemaModifier (io.requery.sql.SchemaModifier)1 DefaultOutput (io.requery.sql.gen.DefaultOutput)1 HSQL (io.requery.sql.platform.HSQL)1 Supplier (io.requery.util.function.Supplier)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1