Search in sources :

Example 1 with TransactionException

use of org.jdbi.v3.core.transaction.TransactionException in project jdbi by jdbi.

the class TestOnDemandSqlObject method testExceptionOnClose.

@Test(expected = TransactionException.class)
public void testExceptionOnClose() throws Exception {
    JdbiPlugin plugin = new JdbiPlugin() {

        @Override
        public Handle customizeHandle(Handle handle) {
            Handle h = spy(handle);
            when(h.createUpdate(anyString())).thenThrow(new TransactionException("connection reset"));
            doThrow(new CloseException("already closed", null)).when(h).close();
            return h;
        }
    };
    db.installPlugin(plugin);
    Spiffy s = db.onDemand(Spiffy.class);
    s.insert(1, "Tom");
}
Also used : TransactionException(org.jdbi.v3.core.transaction.TransactionException) CloseException(org.jdbi.v3.core.CloseException) JdbiPlugin(org.jdbi.v3.core.spi.JdbiPlugin) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 2 with TransactionException

use of org.jdbi.v3.core.transaction.TransactionException in project jdbi by jdbi.

the class TransactionDecorator method decorateHandler.

@Override
public Handler decorateHandler(Handler base, Class<?> sqlObjectType, Method method) {
    final Transaction txnAnnotation = method.getAnnotation(Transaction.class);
    final TransactionIsolationLevel isolation = txnAnnotation.value();
    final boolean readOnly = txnAnnotation.readOnly();
    return (target, args, handle) -> {
        Handle h = handle.getHandle();
        if (h.isInTransaction()) {
            // Already in transaction. The outermost @Transaction method determines the transaction isolation level.
            TransactionIsolationLevel currentLevel = h.getTransactionIsolationLevel();
            if (currentLevel != isolation && isolation != TransactionIsolationLevel.UNKNOWN) {
                throw new TransactionException("Tried to execute nested @Transaction(" + isolation + "), " + "but already running in a transaction with isolation level " + currentLevel + ".");
            }
            if (h.isReadOnly() && !readOnly) {
                throw new TransactionException("Tried to execute a nested @Transaction(readOnly=false) " + "inside a readOnly transaction");
            }
            return base.invoke(target, args, handle);
        }
        HandleCallback<Object, Exception> callback = th -> base.invoke(target, args, handle);
        final boolean flipReadOnly = readOnly != h.isReadOnly();
        if (flipReadOnly) {
            h.setReadOnly(readOnly);
        }
        try {
            if (isolation == TransactionIsolationLevel.UNKNOWN) {
                return h.inTransaction(callback);
            } else {
                return h.inTransaction(isolation, callback);
            }
        } finally {
            if (flipReadOnly) {
                h.setReadOnly(!readOnly);
            }
        }
    };
}
Also used : HandlerDecorator(org.jdbi.v3.sqlobject.HandlerDecorator) HandleCallback(org.jdbi.v3.core.HandleCallback) TransactionIsolationLevel(org.jdbi.v3.core.transaction.TransactionIsolationLevel) Transaction(org.jdbi.v3.sqlobject.transaction.Transaction) Handle(org.jdbi.v3.core.Handle) Method(java.lang.reflect.Method) TransactionException(org.jdbi.v3.core.transaction.TransactionException) Handler(org.jdbi.v3.sqlobject.Handler) TransactionException(org.jdbi.v3.core.transaction.TransactionException) Transaction(org.jdbi.v3.sqlobject.transaction.Transaction) HandleCallback(org.jdbi.v3.core.HandleCallback) TransactionIsolationLevel(org.jdbi.v3.core.transaction.TransactionIsolationLevel) Handle(org.jdbi.v3.core.Handle)

Example 3 with TransactionException

use of org.jdbi.v3.core.transaction.TransactionException in project dropwizard by dropwizard.

the class LoggingJdbiExceptionMapperTest method testPlainJdbiException.

@Test
void testPlainJdbiException() throws Exception {
    JdbiException jdbiException = new TransactionException("Transaction failed for unknown reason");
    jdbiExceptionMapper.logException(9812, jdbiException);
    verify(logger).error("Error handling a request: 0000000000002654", jdbiException);
}
Also used : TransactionException(org.jdbi.v3.core.transaction.TransactionException) JdbiException(org.jdbi.v3.core.JdbiException) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionException (org.jdbi.v3.core.transaction.TransactionException)3 Handle (org.jdbi.v3.core.Handle)2 Method (java.lang.reflect.Method)1 CloseException (org.jdbi.v3.core.CloseException)1 HandleCallback (org.jdbi.v3.core.HandleCallback)1 JdbiException (org.jdbi.v3.core.JdbiException)1 JdbiPlugin (org.jdbi.v3.core.spi.JdbiPlugin)1 TransactionIsolationLevel (org.jdbi.v3.core.transaction.TransactionIsolationLevel)1 Handler (org.jdbi.v3.sqlobject.Handler)1 HandlerDecorator (org.jdbi.v3.sqlobject.HandlerDecorator)1 Transaction (org.jdbi.v3.sqlobject.transaction.Transaction)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1