Java Code Examples for javax.sql.PooledConnection#addConnectionEventListener()
The following examples show how to use
javax.sql.PooledConnection#addConnectionEventListener() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CPDSConnectionFactory.java From Tomcat8-Source-Read with MIT License | 6 votes |
@Override public synchronized PooledObject<PooledConnectionAndInfo> makeObject() { PooledConnectionAndInfo pci; try { PooledConnection pc = null; if (userName == null) { pc = cpds.getPooledConnection(); } else { pc = cpds.getPooledConnection(userName, Utils.toString(userPassword)); } if (pc == null) { throw new IllegalStateException("Connection pool data source returned null from getPooledConnection"); } // should we add this object as a listener or the pool. // consider the validateObject method in decision pc.addConnectionEventListener(this); pci = new PooledConnectionAndInfo(pc, userName, userPassword); pcMap.put(pc, pci); } catch (final SQLException e) { throw new RuntimeException(e.getMessage()); } return new DefaultPooledObject<>(pci); }
Example 2
Source File: CPDSConnectionFactory.java From commons-dbcp with Apache License 2.0 | 6 votes |
@Override public synchronized PooledObject<PooledConnectionAndInfo> makeObject() { PooledConnectionAndInfo pci; try { PooledConnection pc = null; if (userName == null) { pc = cpds.getPooledConnection(); } else { pc = cpds.getPooledConnection(userName, Utils.toString(userPassword)); } if (pc == null) { throw new IllegalStateException("Connection pool data source returned null from getPooledConnection"); } // should we add this object as a listener or the pool. // consider the validateObject method in decision pc.addConnectionEventListener(this); pci = new PooledConnectionAndInfo(pc, userName, userPassword); pcMap.put(pc, pci); } catch (final SQLException e) { throw new RuntimeException(e.getMessage()); } return new DefaultPooledObject<>(pci); }
Example 3
Source File: KeyedCPDSConnectionFactory.java From commons-dbcp with Apache License 2.0 | 6 votes |
/** * Creates a new {@link PooledConnectionAndInfo} from the given {@link UserPassKey}. * * @param upkey * {@link UserPassKey} containing user credentials * @throws SQLException * if the connection could not be created. * @see org.apache.commons.pool2.KeyedPooledObjectFactory#makeObject(java.lang.Object) */ @Override public synchronized PooledObject<PooledConnectionAndInfo> makeObject(final UserPassKey upkey) throws Exception { PooledConnectionAndInfo pci = null; PooledConnection pc = null; final String userName = upkey.getUsername(); final String password = upkey.getPassword(); if (userName == null) { pc = cpds.getPooledConnection(); } else { pc = cpds.getPooledConnection(userName, password); } if (pc == null) { throw new IllegalStateException("Connection pool data source returned null from getPooledConnection"); } // should we add this object as a listener or the pool. // consider the validateObject method in decision pc.addConnectionEventListener(this); pci = new PooledConnectionAndInfo(pc, userName, upkey.getPasswordCharArray()); pcMap.put(pc, pci); return new DefaultPooledObject<>(pci); }
Example 4
Source File: MiniConnectionPoolManager.java From fixflow with Apache License 2.0 | 6 votes |
private synchronized Connection getConnection3() throws SQLException { if (isDisposed) { throw new IllegalStateException("Connection pool has been disposed."); } // test again with lock PooledConnection pconn; if (!recycledConnections.isEmpty()) { pconn = recycledConnections.remove(); } else { pconn = dataSource.getPooledConnection(); pconn.addConnectionEventListener(poolConnectionEventListener); } Connection conn = pconn.getConnection(); activeConnections++; assertInnerState(); return conn; }
Example 5
Source File: ConnectionPoolCacheImpl.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Creates a new connection for the pool. * * @return the connection from the database as Object. * @throws PoolException */ @Override public Object getNewPoolConnection() throws PoolException { if (m_cpds != null) { PooledConnection poolConn = null; try { poolConn = m_cpds.getPooledConnection(configProps.getUser(), configProps.getPassword()); } catch (SQLException sqx) { throw new PoolException(LocalizedStrings.ConnectionPoolCacheImpl_CONNECTIONPOOLCACHEIMPLGENEWCONNECTION_EXCEPTION_IN_CREATING_NEW_POOLEDCONNECTION.toLocalizedString(), sqx); } poolConn .addConnectionEventListener((javax.sql.ConnectionEventListener) connEventListner); return poolConn; } else { LogWriterI18n writer = TransactionUtils.getLogWriterI18n(); if (writer.fineEnabled()) writer.fine("ConnectionPoolCacheImpl::geNewConnection: ConnectionPoolCache not intialized with ConnectionPoolDatasource"); throw new PoolException(LocalizedStrings.ConnectionPoolCacheImpl_CONNECTIONPOOLCACHEIMPLGENEWCONNECTION_CONNECTIONPOOLCACHE_NOT_INTIALIZED_WITH_CONNECTIONPOOLDATASOURCE.toLocalizedString()); } }
Example 6
Source File: TranxPoolCacheImpl.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Creates a new connection for the pool. This connection can participate in * the transactions. * * @return the connection from the database as PooledConnection object. */ @Override public Object getNewPoolConnection() throws PoolException { if (m_xads != null) { PooledConnection poolConn = null; try { poolConn = m_xads.getXAConnection(configProps.getUser(), configProps .getPassword()); } catch (SQLException sqx) { throw new PoolException(LocalizedStrings.TranxPoolCacheImpl_TRANXPOOLCACHEIMPLGETNEWCONNECTION_EXCEPTION_IN_CREATING_NEW_TRANSACTION_POOLEDCONNECTION.toLocalizedString(), sqx); } poolConn .addConnectionEventListener((javax.sql.ConnectionEventListener) connEventListner); return poolConn; } else { LogWriterI18n writer = TransactionUtils.getLogWriterI18n(); if (writer.fineEnabled()) writer.fine("TranxPoolCacheImpl::getNewConnection: ConnectionPoolCache not intialized with XADatasource"); throw new PoolException(LocalizedStrings.TranxPoolCacheImpl_TRANXPOOLCACHEIMPLGETNEWCONNECTION_CONNECTIONPOOLCACHE_NOT_INTIALIZED_WITH_XADATASOURCE.toLocalizedString()); } }
Example 7
Source File: J2EEDataSourceTest.java From spliceengine with GNU Affero General Public License v3.0 | 6 votes |
/** * Test that notification of an error event doesn't fail when the * listener is null. */ private void subtestErrorEventWithNullListener(PooledConnection pc) throws SQLException { pc.addConnectionEventListener(null); Connection c = pc.getConnection(); // Shut down the database to invalidate all connections getTestConfiguration().shutdownDatabase(); try { // Should trigger an error event since the connection is no // longer valid c.prepareStatement("VALUES 1"); fail("Statement should fail after database shutdown"); } catch (SQLException e) { if (usingEmbedded()) { // No current connection is expected on embedded assertSQLState("08003", e); } else { // The client driver reports communication error assertSQLState("08006", e); } } c.close(); pc.close(); }
Example 8
Source File: PooledConnectionTest.java From mariadb-connector-j with GNU Lesser General Public License v2.1 | 6 votes |
@Test(expected = SQLException.class) public void testPooledConnectionClosed() throws Exception { ConnectionPoolDataSource ds = new MariaDbDataSource(hostname != null ? hostname : "localhost", port, database); PooledConnection pc = ds.getPooledConnection(username, password); Connection connection = pc.getConnection(); MyEventListener listener = new MyEventListener(); pc.addConnectionEventListener(listener); pc.addStatementEventListener(listener); connection.close(); assertTrue(listener.closed); /* Verify physical connection is still ok */ connection.createStatement().execute("select 1"); /* close physical connection */ pc.close(); /* Now verify physical connection is gone */ connection.createStatement().execute("select 1"); fail("should never get there : previous must have thrown exception"); }
Example 9
Source File: ConnectionPoolCacheImpl.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Creates a new connection for the pool. * * @return the connection from the database as Object. * @throws PoolException */ @Override public Object getNewPoolConnection() throws PoolException { if (m_cpds != null) { PooledConnection poolConn = null; try { poolConn = m_cpds.getPooledConnection(configProps.getUser(), configProps.getPassword()); } catch (SQLException sqx) { throw new PoolException(LocalizedStrings.ConnectionPoolCacheImpl_CONNECTIONPOOLCACHEIMPLGENEWCONNECTION_EXCEPTION_IN_CREATING_NEW_POOLEDCONNECTION.toLocalizedString(), sqx); } poolConn .addConnectionEventListener((javax.sql.ConnectionEventListener) connEventListner); return poolConn; } else { LogWriterI18n writer = TransactionUtils.getLogWriterI18n(); if (writer.fineEnabled()) writer.fine("ConnectionPoolCacheImpl::geNewConnection: ConnectionPoolCache not intialized with ConnectionPoolDatasource"); throw new PoolException(LocalizedStrings.ConnectionPoolCacheImpl_CONNECTIONPOOLCACHEIMPLGENEWCONNECTION_CONNECTIONPOOLCACHE_NOT_INTIALIZED_WITH_CONNECTIONPOOLDATASOURCE.toLocalizedString()); } }
Example 10
Source File: TranxPoolCacheImpl.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Creates a new connection for the pool. This connection can participate in * the transactions. * * @return the connection from the database as PooledConnection object. */ @Override public Object getNewPoolConnection() throws PoolException { if (m_xads != null) { PooledConnection poolConn = null; try { poolConn = m_xads.getXAConnection(configProps.getUser(), configProps .getPassword()); } catch (SQLException sqx) { throw new PoolException(LocalizedStrings.TranxPoolCacheImpl_TRANXPOOLCACHEIMPLGETNEWCONNECTION_EXCEPTION_IN_CREATING_NEW_TRANSACTION_POOLEDCONNECTION.toLocalizedString(), sqx); } poolConn .addConnectionEventListener((javax.sql.ConnectionEventListener) connEventListner); return poolConn; } else { LogWriterI18n writer = TransactionUtils.getLogWriterI18n(); if (writer.fineEnabled()) writer.fine("TranxPoolCacheImpl::getNewConnection: ConnectionPoolCache not intialized with XADatasource"); throw new PoolException(LocalizedStrings.TranxPoolCacheImpl_TRANXPOOLCACHEIMPLGETNEWCONNECTION_CONNECTIONPOOLCACHE_NOT_INTIALIZED_WITH_XADATASOURCE.toLocalizedString()); } }
Example 11
Source File: J2EEDataSourceTest.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Test that notification of an error event doesn't fail when the * listener is null. */ private void subtestErrorEventWithNullListener(PooledConnection pc) throws SQLException { pc.addConnectionEventListener(null); Connection c = pc.getConnection(); // Shut down the database to invalidate all connections getTestConfiguration().shutdownDatabase(); try { // Should trigger an error event since the connection is no // longer valid c.prepareStatement("VALUES 1"); fail("Statement should fail after database shutdown"); } catch (SQLException e) { if (usingEmbedded()) { // No current connection is expected on embedded assertSQLState("08003", e); } else { // The client driver reports communication error assertSQLState("08006", e); } } c.close(); pc.close(); }
Example 12
Source File: PooledConnectionRegressionTest.java From FoxTelem with GNU General Public License v3.0 | 5 votes |
/** * Tests that PacketTooLargeException doesn't clober the connection. * * @throws Exception * if the test fails. */ public void testPacketTooLargeException() throws Exception { final ConnectionEventListener conListener = new ConnectionListener(); PooledConnection pc = null; pc = this.cpds.getPooledConnection(); pc.addConnectionEventListener(conListener); createTable("testPacketTooLarge", "(field1 LONGBLOB)"); Connection connFromPool = pc.getConnection(); PreparedStatement pstmtFromPool = ((ConnectionWrapper) connFromPool).clientPrepare("INSERT INTO testPacketTooLarge VALUES (?)"); this.rs = this.stmt.executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet'"); this.rs.next(); int maxAllowedPacket = this.rs.getInt(2); int numChars = (int) (maxAllowedPacket * 1.2); pstmtFromPool.setBinaryStream(1, new BufferedInputStream(new FileInputStream(newTempBinaryFile("testPacketTooLargeException", numChars))), numChars); try { pstmtFromPool.executeUpdate(); fail("Expecting PacketTooLargeException"); } catch (PacketTooBigException ptbe) { // We're expecting this one... } // This should still work okay, even though the last query on the same connection didn't... this.rs = connFromPool.createStatement().executeQuery("SELECT 1"); assertTrue(this.connectionErrorEventCount == 0); assertTrue(this.closeEventCount == 0); }
Example 13
Source File: PooledConnectionTest.java From mariadb-connector-j with GNU Lesser General Public License v2.1 | 5 votes |
@Test(expected = SQLException.class) public void testPooledConnectionException() throws Exception { Assume.assumeTrue(System.getenv("MAXSCALE_VERSION") == null && System.getenv("SKYSQL") == null); ConnectionPoolDataSource ds = new MariaDbDataSource(hostname != null ? hostname : "localhost", port, database); PooledConnection pc = null; try { pc = ds.getPooledConnection(username, password); MyEventListener listener = new MyEventListener(); pc.addConnectionEventListener(listener); MariaDbConnection connection = (MariaDbConnection) pc.getConnection(); /* Ask server to abort the connection */ try { connection.createStatement().execute("KILL CONNECTION_ID()"); } catch (Exception e) { /* exception is expected here, server sends query aborted */ } /* Try to read after server side closed the connection */ connection.createStatement().execute("SELECT 1"); fail("should never get there"); } finally { if (pc != null) { pc.close(); } } }
Example 14
Source File: J2EEDataSourceTest.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Test that notification of a close event doesn't fail when the * listener is null. */ private void subtestCloseEventWithNullListener(PooledConnection pc) throws SQLException { pc.addConnectionEventListener(null); // Trigger a close event pc.getConnection().close(); pc.close(); }
Example 15
Source File: J2EEDataSourceTest.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Test that notification of a close event doesn't fail when the * listener is null. */ private void subtestCloseEventWithNullListener(PooledConnection pc) throws SQLException { pc.addConnectionEventListener(null); // Trigger a close event pc.getConnection().close(); pc.close(); }
Example 16
Source File: PooledConnectionRegressionTest.java From Komondor with GNU General Public License v3.0 | 5 votes |
/** * Tests that PacketTooLargeException doesn't clober the connection. * * @throws Exception * if the test fails. */ public void testPacketTooLargeException() throws Exception { final ConnectionEventListener conListener = new ConnectionListener(); PooledConnection pc = null; pc = this.cpds.getPooledConnection(); pc.addConnectionEventListener(conListener); createTable("testPacketTooLarge", "(field1 LONGBLOB)"); Connection connFromPool = pc.getConnection(); PreparedStatement pstmtFromPool = ((ConnectionWrapper) connFromPool).clientPrepare("INSERT INTO testPacketTooLarge VALUES (?)"); this.rs = this.stmt.executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet'"); this.rs.next(); int maxAllowedPacket = this.rs.getInt(2); int numChars = (int) (maxAllowedPacket * 1.2); pstmtFromPool.setBinaryStream(1, new BufferedInputStream(new FileInputStream(newTempBinaryFile("testPacketTooLargeException", numChars))), numChars); try { pstmtFromPool.executeUpdate(); fail("Expecting PacketTooLargeException"); } catch (PacketTooBigException ptbe) { // We're expecting this one... } // This should still work okay, even though the last query on the same connection didn't... this.rs = connFromPool.createStatement().executeQuery("SELECT 1"); assertTrue(this.connectionErrorEventCount == 0); assertTrue(this.closeEventCount == 0); }
Example 17
Source File: PooledConnectionRegressionTest.java From r-course with MIT License | 5 votes |
/** * Tests that PacketTooLargeException doesn't clober the connection. * * @throws Exception * if the test fails. */ public void testPacketTooLargeException() throws Exception { final ConnectionEventListener conListener = new ConnectionListener(); PooledConnection pc = null; pc = this.cpds.getPooledConnection(); pc.addConnectionEventListener(conListener); createTable("testPacketTooLarge", "(field1 LONGBLOB)"); Connection connFromPool = pc.getConnection(); PreparedStatement pstmtFromPool = ((ConnectionWrapper) connFromPool).clientPrepare("INSERT INTO testPacketTooLarge VALUES (?)"); this.rs = this.stmt.executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet'"); this.rs.next(); int maxAllowedPacket = this.rs.getInt(2); int numChars = (int) (maxAllowedPacket * 1.2); pstmtFromPool.setBinaryStream(1, new BufferedInputStream(new FileInputStream(newTempBinaryFile("testPacketTooLargeException", numChars))), numChars); try { pstmtFromPool.executeUpdate(); fail("Expecting PacketTooLargeException"); } catch (PacketTooBigException ptbe) { // We're expecting this one... } // This should still work okay, even though the last query on the same connection didn't... this.rs = connFromPool.createStatement().executeQuery("SELECT 1"); assertTrue(this.connectionErrorEventCount == 0); assertTrue(this.closeEventCount == 0); }
Example 18
Source File: J2EEDataSourceTest.java From spliceengine with GNU Affero General Public License v3.0 | 5 votes |
/** * Test that notification of a close event doesn't fail when the * listener is null. */ private void subtestCloseEventWithNullListener(PooledConnection pc) throws SQLException { pc.addConnectionEventListener(null); // Trigger a close event pc.getConnection().close(); pc.close(); }
Example 19
Source File: ConnectionPoolingDataSourceIT.java From snowflake-jdbc with Apache License 2.0 | 4 votes |
@Test public void testPooledConnection() throws SQLException { Map<String, String> properties = getConnectionParameters(); SnowflakeConnectionPoolDataSource poolDataSource = new SnowflakeConnectionPoolDataSource(); poolDataSource.setUrl(properties.get("uri")); poolDataSource.setPortNumber(Integer.parseInt(properties.get("port"))); poolDataSource.setSsl("on".equals(properties.get("ssl"))); poolDataSource.setAccount(properties.get("account")); poolDataSource.setUser(properties.get("user")); poolDataSource.setPassword(properties.get("password")); PooledConnection pooledConnection = poolDataSource.getPooledConnection(); TestingConnectionListener listener = new TestingConnectionListener(); pooledConnection.addConnectionEventListener(listener); Connection connection = pooledConnection.getConnection(); connection.createStatement().execute("select 1"); try { // should fire connection error events connection.setCatalog("unexisted_database"); fail(); } catch (SQLException e) { assertThat(e.getErrorCode(), is(2043)); } // should not close underlying physical connection // and fire connection closed events connection.close(); List<ConnectionEvent> connectionClosedEvents = listener.getConnectionClosedEvents(); List<ConnectionEvent> connectionErrorEvents = listener.getConnectionErrorEvents(); // assert connection close event assertThat(connectionClosedEvents.size(), is(1)); ConnectionEvent closedEvent = connectionClosedEvents.get(0); assertThat(closedEvent.getSQLException(), is(nullValue())); assertThat(closedEvent.getSource(), instanceOf(SnowflakePooledConnection.class)); assertThat((PooledConnection) closedEvent.getSource(), sameInstance(pooledConnection)); // assert connection error event assertThat(connectionErrorEvents.size(), is(1)); ConnectionEvent errorEvent = connectionErrorEvents.get(0); assertThat(errorEvent.getSource(), instanceOf(SnowflakePooledConnection.class)); assertThat((PooledConnection) errorEvent.getSource(), sameInstance(pooledConnection)); // 2043 is the error code for object not existed assertThat(errorEvent.getSQLException().getErrorCode(), is(2043)); // assert physical connection is not closed Connection physicalConnection = ((SnowflakePooledConnection) pooledConnection).getPhysicalConnection(); assertThat(physicalConnection.isClosed(), is(false)); pooledConnection.removeConnectionEventListener(listener); // will close physical connection pooledConnection.close(); assertThat(physicalConnection.isClosed(), is(true)); }
Example 20
Source File: PooledConnectionProxy.java From commons-dbcp with Apache License 2.0 | 4 votes |
public PooledConnectionProxy(final PooledConnection pooledConnection) { this.delegate = pooledConnection; pooledConnection.addConnectionEventListener(this); }