Java Code Examples for java.sql.Connection.toString()

The following are Jave code examples for showing how to use toString() of the java.sql.Connection class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: agroal   File: EnlistmentTests.java   View Source Code Vote up 5 votes
@Test
@DisplayName( "Enroll connection after previous connection close test" )
public void enrollConnectionCloseTest() throws SQLException {
    TransactionManager txManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
    TransactionSynchronizationRegistry txSyncRegistry = new com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple();

    AgroalDataSourceConfigurationSupplier configurationSupplier = new AgroalDataSourceConfigurationSupplier()
            .connectionPoolConfiguration( cp -> cp
                    .transactionIntegration( new NarayanaTransactionIntegration( txManager, txSyncRegistry ) )
                    .connectionFactoryConfiguration( cf -> cf
                            .autoCommit( true ) )
            );

    try ( AgroalDataSource dataSource = AgroalDataSource.from( configurationSupplier ) ) {
        txManager.begin();

        Connection connection = dataSource.getConnection();
        logger.info( format( "Got connection {0}", connection ) );
        String connectionToString = connection.toString();
        connection.close();

        Connection secondConnection = dataSource.getConnection();
        logger.info( format( "Got connection {0}", secondConnection ) );

        // TODO: comparing toString is brittle. Find a better way to make sure the underlying physical connection is the same.
        assertEquals( connectionToString, secondConnection.toString(), "Expect the same connection under the same transaction" );
        assertFalse( secondConnection.getAutoCommit(), "AutoCommit temporarily disabled in enlisted connection" );
        secondConnection.close();

        txManager.commit();

        assertTrue( connection.isClosed() );
        assertTrue( secondConnection.isClosed() );
    } catch ( NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException e ) {
        fail( "Exception: " + e.getMessage() );
    }
}