Search in sources :

Example 1 with Transaction

use of io.ebean.Transaction in project act-ebean by actframework.

the class EbeanService method doEndTxIfActive.

@Override
protected void doEndTxIfActive(Object delegate) {
    Transaction tx = ebean.currentTransaction();
    if (null == tx) {
        return;
    }
    if (TxContext.readOnly()) {
        ebeanReadOnly.endTransaction();
    } else {
        SpiEbeanServer server = (SpiEbeanServer) ebean;
        server.scopedTransactionExit(null, 1);
    }
}
Also used : SpiEbeanServer(io.ebeaninternal.api.SpiEbeanServer) Transaction(io.ebean.Transaction)

Example 2 with Transaction

use of io.ebean.Transaction in project ebean by ebean-orm.

the class JdbcTransactionTest method postNestedCallback.

@Test
public void postNestedCallback() {
    // test both pre-commit and post-commit to ensure callbacks don't disappear and new ones can be added
    AtomicInteger preCommitCallCount = new AtomicInteger();
    AtomicInteger postCommitCallCount = new AtomicInteger();
    try (Transaction transaction = DB.beginTransaction()) {
        DB.currentTransaction().register(new TransactionCallbackAdapter() {

            @Override
            public void postCommit() {
                postCommitCallCount.incrementAndGet();
                DB.currentTransaction().register(new TransactionCallbackAdapter() {

                    @Override
                    public void postCommit() {
                        postCommitCallCount.incrementAndGet();
                    }
                });
            }

            @Override
            public void preCommit() {
                preCommitCallCount.incrementAndGet();
            }
        });
        EBasic basic = new EBasic("b1");
        DB.save(basic);
        // transaction will fail if recursive post-commit is failing
        transaction.commit();
        // precommit executed once
        assertThat(preCommitCallCount.get()).isEqualTo(1);
        // postcommit executed twice
        assertThat(postCommitCallCount.get()).isEqualTo(2);
    }
}
Also used : TransactionCallbackAdapter(io.ebean.TransactionCallbackAdapter) Transaction(io.ebean.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EBasic(org.tests.model.basic.EBasic) Test(org.junit.jupiter.api.Test)

Example 3 with Transaction

use of io.ebean.Transaction in project ebean by ebean-orm.

the class TestQueryFindEach method persistenceContext_scope.

@Test
public void persistenceContext_scope() {
    ResetBasicData.reset();
    Query<Contact> query = DB.find(Contact.class);
    try (final Transaction transaction = DB.beginTransaction()) {
        // effectively loads customers into persistence context
        final List<Customer> customerList = DB.find(Customer.class).select("name").findList();
        SpiTransaction spiTxn = (SpiTransaction) transaction;
        PersistenceContext pc = spiTxn.getPersistenceContext();
        assertThat(pc.size(Customer.class)).isEqualTo(customerList.size());
        LoggedSql.start();
        query.findEach(contact -> {
            // use customer from persistence context (otherwise would invoke lazy loading)
            assertNotNull(contact.getCustomer().getName());
        });
        final List<String> sql = LoggedSql.stop();
        assertThat(sql).hasSize(1);
    }
}
Also used : SpiTransaction(io.ebeaninternal.api.SpiTransaction) SpiTransaction(io.ebeaninternal.api.SpiTransaction) Transaction(io.ebean.Transaction) Customer(org.tests.model.basic.Customer) PersistenceContext(io.ebean.bean.PersistenceContext) Contact(org.tests.model.basic.Contact) Test(org.junit.jupiter.api.Test)

Example 4 with Transaction

use of io.ebean.Transaction in project ebean by ebean-orm.

the class DummyDao method doSomething.

@Transactional(type = TxType.REQUIRES_NEW)
public void doSomething() {
    logger.info("  --- in DummyDao.doSomething() with TxType.REQUIRES_NEW");
    Transaction txn = DB.currentTransaction();
    if (txn == null) {
        logger.error("  NO TRANSACTION ??");
    } else {
        logger.info("  --- txn - " + txn);
    }
}
Also used : Transaction(io.ebean.Transaction) Transactional(io.ebean.annotation.Transactional)

Example 5 with Transaction

use of io.ebean.Transaction in project ebean by ebean-orm.

the class DummyDao method doWithBatchOptionsSet.

@Transactional(batch = PersistBatch.ALL, batchOnCascade = PersistBatch.ALL, batchSize = 99)
private void doWithBatchOptionsSet() {
    Transaction txn = DB.currentTransaction();
    assertTrue(txn.isBatchMode());
    assertTrue(txn.isBatchOnCascade());
    assertEquals(99, txn.getBatchSize());
}
Also used : Transaction(io.ebean.Transaction) Transactional(io.ebean.annotation.Transactional)

Aggregations

Transaction (io.ebean.Transaction)139 Test (org.junit.jupiter.api.Test)100 SqlUpdate (io.ebean.SqlUpdate)15 Database (io.ebean.Database)12 IgnorePlatform (io.ebean.annotation.IgnorePlatform)12 SpiTransaction (io.ebeaninternal.api.SpiTransaction)12 ArrayList (java.util.ArrayList)12 EBasicVer (org.tests.model.basic.EBasicVer)10 Transactional (io.ebean.annotation.Transactional)9 SpiEbeanServer (io.ebeaninternal.api.SpiEbeanServer)6 Customer (org.tests.model.basic.Customer)6 UTMaster (org.tests.model.basic.UTMaster)6 MnyB (org.tests.model.m2m.MnyB)6 ForPlatform (io.ebean.annotation.ForPlatform)5 PersistenceException (javax.persistence.PersistenceException)5 EBasic (org.tests.model.basic.EBasic)5 Order (org.tests.model.basic.Order)5 Document (org.tests.model.draftable.Document)5 Version (models.Version)3 AuditLog (org.tests.idkeys.db.AuditLog)3