Search in sources :

Example 1 with TransactionAwareDataSourceProxy

use of org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy in project spring-framework by spring-projects.

the class JdbcTransactionManagerTests method testTransactionAwareDataSourceProxy.

@Test
public void testTransactionAwareDataSourceProxy() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    given(con.getWarnings()).willThrow(new SQLException());
    TransactionTemplate tt = new TransactionTemplate(tm);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
            TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
            try {
                Connection tCon = dsProxy.getConnection();
                tCon.getWarnings();
                tCon.clearWarnings();
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).commit();
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionAwareDataSourceProxy(org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with TransactionAwareDataSourceProxy

use of org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy in project spring-framework by spring-projects.

the class JdbcTransactionManagerTests method testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining.

@Test
public void testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
            final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
            dsProxy.setReobtainTransactionalConnections(true);
            try {
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
            tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    // something transactional
                    assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
                    try {
                        assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                        // should be ignored
                        dsProxy.getConnection().close();
                    } catch (SQLException ex) {
                        throw new UncategorizedSQLException("", "", ex);
                    }
                }
            });
            try {
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).commit();
    ordered.verify(con).setAutoCommit(true);
    verify(con, times(2)).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) TransactionAwareDataSourceProxy(org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) ConnectionProxy(org.springframework.jdbc.datasource.ConnectionProxy) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with TransactionAwareDataSourceProxy

use of org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy in project spring-framework by spring-projects.

the class JdbcTransactionManagerTests method testTransactionAwareDataSourceProxyWithSuspension.

@Test
public void testTransactionAwareDataSourceProxyWithSuspension() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
            final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
            try {
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
            tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    // something transactional
                    assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
                    try {
                        assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                        // should be ignored
                        dsProxy.getConnection().close();
                    } catch (SQLException ex) {
                        throw new UncategorizedSQLException("", "", ex);
                    }
                }
            });
            try {
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).commit();
    ordered.verify(con).setAutoCommit(true);
    verify(con, times(2)).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) TransactionAwareDataSourceProxy(org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) ConnectionProxy(org.springframework.jdbc.datasource.ConnectionProxy) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

SQLException (java.sql.SQLException)3 Test (org.junit.jupiter.api.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 InOrder (org.mockito.InOrder)3 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)3 TransactionAwareDataSourceProxy (org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)3 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)3 ConnectionProxy (org.springframework.jdbc.datasource.ConnectionProxy)2 Connection (java.sql.Connection)1