Search in sources :

Example 76 with DataSource

use of javax.sql.DataSource in project spring-framework by spring-projects.

the class DataSourceTransactionManagerTests method testPropagationRequiresNewWithExistingTransactionAndUnrelatedDataSource.

@Test
public void testPropagationRequiresNewWithExistingTransactionAndUnrelatedDataSource() throws Exception {
    Connection con2 = mock(Connection.class);
    final DataSource ds2 = mock(DataSource.class);
    given(ds2.getConnection()).willReturn(con2);
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    PlatformTransactionManager tm2 = new DataSourceTransactionManager(ds2);
    final TransactionTemplate tt2 = new TransactionTemplate(tm2);
    tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds2));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertTrue("Is new transaction", status.isNewTransaction());
            assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
            tt2.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                    assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
                    assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
                    assertTrue("Is new transaction", status.isNewTransaction());
                    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                    assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                    status.setRollbackOnly();
                }
            });
            assertTrue("Is new transaction", status.isNewTransaction());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds2));
    verify(con).commit();
    verify(con).close();
    verify(con2).rollback();
    verify(con2).close();
}
Also used : Connection(java.sql.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 77 with DataSource

use of javax.sql.DataSource in project spring-framework by spring-projects.

the class DataSourceTransactionManagerTests method doTestTransactionRollbackRestoringAutoCommit.

private void doTestTransactionRollbackRestoringAutoCommit(boolean autoCommit, boolean lazyConnection, final boolean createStatement) throws Exception {
    if (lazyConnection) {
        given(con.getAutoCommit()).willReturn(autoCommit);
        given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
    }
    if (!lazyConnection || createStatement) {
        given(con.getAutoCommit()).willReturn(autoCommit);
    }
    final DataSource dsToUse = (lazyConnection ? new LazyConnectionDataSourceProxy(ds) : ds);
    tm = new DataSourceTransactionManager(dsToUse);
    TransactionTemplate tt = new TransactionTemplate(tm);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    final RuntimeException ex = new RuntimeException("Application exception");
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
                assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue("Is new transaction", status.isNewTransaction());
                Connection con = DataSourceUtils.getConnection(dsToUse);
                if (createStatement) {
                    try {
                        con.createStatement();
                    } catch (SQLException ex) {
                        throw new UncategorizedSQLException("", "", ex);
                    }
                }
                throw ex;
            }
        });
        fail("Should have thrown RuntimeException");
    } catch (RuntimeException ex2) {
        // expected
        assertTrue("Correct exception thrown", ex2.equals(ex));
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    if (autoCommit && (!lazyConnection || createStatement)) {
        InOrder ordered = inOrder(con);
        ordered.verify(con).setAutoCommit(false);
        ordered.verify(con).rollback();
        ordered.verify(con).setAutoCommit(true);
    }
    if (createStatement) {
        verify(con, times(2)).close();
    } else {
        verify(con).close();
    }
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) DataSource(javax.sql.DataSource)

Example 78 with DataSource

use of javax.sql.DataSource in project spring-framework by spring-projects.

the class SQLErrorCodesFactoryTests method getErrorCodesFromDataSource.

private SQLErrorCodes getErrorCodesFromDataSource(String productName, SQLErrorCodesFactory factory) throws Exception {
    DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
    given(databaseMetaData.getDatabaseProductName()).willReturn(productName);
    Connection connection = mock(Connection.class);
    given(connection.getMetaData()).willReturn(databaseMetaData);
    DataSource dataSource = mock(DataSource.class);
    given(dataSource.getConnection()).willReturn(connection);
    SQLErrorCodesFactory secf = null;
    if (factory != null) {
        secf = factory;
    } else {
        secf = SQLErrorCodesFactory.getInstance();
    }
    SQLErrorCodes sec = secf.getErrorCodes(dataSource);
    SQLErrorCodes sec2 = secf.getErrorCodes(dataSource);
    assertSame("Cached per DataSource", sec2, sec);
    verify(connection).close();
    return sec;
}
Also used : Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) DataSource(javax.sql.DataSource)

Example 79 with DataSource

use of javax.sql.DataSource in project spring-framework by spring-projects.

the class SQLErrorCodesFactoryTests method testGetFromDataSourceWithSQLException.

@Test
public void testGetFromDataSourceWithSQLException() throws Exception {
    SQLException expectedSQLException = new SQLException();
    DataSource dataSource = mock(DataSource.class);
    given(dataSource.getConnection()).willThrow(expectedSQLException);
    SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes(dataSource);
    assertIsEmpty(sec);
}
Also used : SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 80 with DataSource

use of javax.sql.DataSource in project spring-framework by spring-projects.

the class JndiDataSourceLookupTests method testSunnyDay.

@Test
public void testSunnyDay() throws Exception {
    final DataSource expectedDataSource = new StubDataSource();
    JndiDataSourceLookup lookup = new JndiDataSourceLookup() {

        @Override
        protected <T> T lookup(String jndiName, Class<T> requiredType) {
            assertEquals(DATA_SOURCE_NAME, jndiName);
            return requiredType.cast(expectedDataSource);
        }
    };
    DataSource dataSource = lookup.getDataSource(DATA_SOURCE_NAME);
    assertNotNull("A DataSourceLookup implementation must *never* return null from getDataSource(): this one obviously (and incorrectly) is", dataSource);
    assertSame(expectedDataSource, dataSource);
}
Also used : DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

DataSource (javax.sql.DataSource)546 Connection (java.sql.Connection)200 Test (org.junit.Test)192 SQLException (java.sql.SQLException)118 Context (javax.naming.Context)70 ResultSet (java.sql.ResultSet)59 Statement (java.sql.Statement)59 NamingException (javax.naming.NamingException)57 InitialContext (javax.naming.InitialContext)55 EJBException (javax.ejb.EJBException)40 HashMap (java.util.HashMap)38 PreparedStatement (java.sql.PreparedStatement)37 Properties (java.util.Properties)35 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)34 RemoteException (java.rmi.RemoteException)32 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)31 UserTransaction (javax.transaction.UserTransaction)30 IOException (java.io.IOException)29 ArrayList (java.util.ArrayList)26 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)21