Search in sources :

Example 1 with ConnectionProxy

use of org.springframework.jdbc.datasource.ConnectionProxy 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 2 with ConnectionProxy

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

the class JdbcTemplateTests method testConnectionCallback.

@Test
public void testConnectionCallback() throws Exception {
    String result = this.template.execute((ConnectionCallback<String>) con -> {
        assertThat(con instanceof ConnectionProxy).isTrue();
        assertThat(((ConnectionProxy) con).getTargetConnection()).isSameAs(JdbcTemplateTests.this.connection);
        return "test";
    });
    assertThat(result).isEqualTo("test");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) DataAccessException(org.springframework.dao.DataAccessException) Connection(java.sql.Connection) BatchUpdateException(java.sql.BatchUpdateException) LinkedCaseInsensitiveMap(org.springframework.util.LinkedCaseInsensitiveMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) DatabaseMetaData(java.sql.DatabaseMetaData) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ArrayList(java.util.ArrayList) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) SQLException(java.sql.SQLException) SQLWarningException(org.springframework.jdbc.SQLWarningException) BDDMockito.given(org.mockito.BDDMockito.given) ResultSet(java.sql.ResultSet) Map(java.util.Map) DataSource(javax.sql.DataSource) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) SQLWarning(java.sql.SQLWarning) SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) AbstractInterruptibleBatchPreparedStatementSetter(org.springframework.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) Mockito.times(org.mockito.Mockito.times) ConnectionProxy(org.springframework.jdbc.datasource.ConnectionProxy) PreparedStatement(java.sql.PreparedStatement) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) Mockito.never(org.mockito.Mockito.never) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator) Collections(java.util.Collections) Mockito.reset(org.mockito.Mockito.reset) ResultSetMetaData(java.sql.ResultSetMetaData) Types(java.sql.Types) StringUtils(org.springframework.util.StringUtils) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConnectionProxy(org.springframework.jdbc.datasource.ConnectionProxy) Test(org.junit.jupiter.api.Test)

Example 3 with ConnectionProxy

use of org.springframework.jdbc.datasource.ConnectionProxy 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 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)3 ConnectionProxy (org.springframework.jdbc.datasource.ConnectionProxy)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 InOrder (org.mockito.InOrder)2 BatchUpdateException (java.sql.BatchUpdateException)1 CallableStatement (java.sql.CallableStatement)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLWarning (java.sql.SQLWarning)1 Statement (java.sql.Statement)1 Types (java.sql.Types)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1