Search in sources :

Example 1 with ConnectionProxy

use of cn.taketoday.jdbc.datasource.ConnectionProxy in project today-infrastructure by TAKETODAY.

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(cn.taketoday.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) TransactionAwareDataSourceProxy(cn.taketoday.jdbc.datasource.TransactionAwareDataSourceProxy) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) ConnectionProxy(cn.taketoday.jdbc.datasource.ConnectionProxy) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with ConnectionProxy

use of cn.taketoday.jdbc.datasource.ConnectionProxy in project today-infrastructure by TAKETODAY.

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(cn.taketoday.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) TransactionAwareDataSourceProxy(cn.taketoday.jdbc.datasource.TransactionAwareDataSourceProxy) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) ConnectionProxy(cn.taketoday.jdbc.datasource.ConnectionProxy) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ConnectionProxy

use of cn.taketoday.jdbc.datasource.ConnectionProxy in project today-framework by TAKETODAY.

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(cn.taketoday.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) TransactionAwareDataSourceProxy(cn.taketoday.jdbc.datasource.TransactionAwareDataSourceProxy) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) ConnectionProxy(cn.taketoday.jdbc.datasource.ConnectionProxy) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ConnectionProxy

use of cn.taketoday.jdbc.datasource.ConnectionProxy in project today-framework by TAKETODAY.

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(cn.taketoday.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) TransactionAwareDataSourceProxy(cn.taketoday.jdbc.datasource.TransactionAwareDataSourceProxy) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) ConnectionProxy(cn.taketoday.jdbc.datasource.ConnectionProxy) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with ConnectionProxy

use of cn.taketoday.jdbc.datasource.ConnectionProxy in project today-framework by TAKETODAY.

the class JdbcTemplateTests method testConnectionCallback.

@Test
public void testConnectionCallback() throws Exception {
    String result = this.template.execute(new ConnectionCallback<String>() {

        @Override
        public String doInConnection(Connection con) {
            assertThat(con instanceof ConnectionProxy).isTrue();
            assertThat(((ConnectionProxy) con).getTargetConnection()).isSameAs(JdbcTemplateTests.this.connection);
            return "test";
        }
    });
    assertThat(result).isEqualTo("test");
}
Also used : Connection(java.sql.Connection) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConnectionProxy(cn.taketoday.jdbc.datasource.ConnectionProxy) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionProxy (cn.taketoday.jdbc.datasource.ConnectionProxy)6 Test (org.junit.jupiter.api.Test)6 UncategorizedSQLException (cn.taketoday.jdbc.UncategorizedSQLException)4 TransactionAwareDataSourceProxy (cn.taketoday.jdbc.datasource.TransactionAwareDataSourceProxy)4 TransactionStatus (cn.taketoday.transaction.TransactionStatus)4 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)4 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)4 SQLException (java.sql.SQLException)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 InOrder (org.mockito.InOrder)4 Connection (java.sql.Connection)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2