Java Code Examples for javax.sql.PooledConnection#removeConnectionEventListener()

The following examples show how to use javax.sql.PooledConnection#removeConnectionEventListener() . 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: KeyedCPDSConnectionFactory.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * This will be called if the Connection returned by the getConnection method came from a PooledConnection, and the
 * user calls the close() method of this connection object. What we need to do here is to release this
 * PooledConnection from our pool...
 */
@Override
public void connectionClosed(final ConnectionEvent event) {
    final PooledConnection pc = (PooledConnection) event.getSource();
    // if this event occurred because we were validating, or if this
    // connection has been marked for removal, ignore it
    // otherwise return the connection to the pool.
    if (!validatingSet.contains(pc)) {
        final PooledConnectionAndInfo pci = pcMap.get(pc);
        if (pci == null) {
            throw new IllegalStateException(NO_KEY_MESSAGE);
        }
        try {
            pool.returnObject(pci.getUserPassKey(), pci);
        } catch (final Exception e) {
            System.err.println("CLOSING DOWN CONNECTION AS IT COULD " + "NOT BE RETURNED TO THE POOL");
            pc.removeConnectionEventListener(this);
            try {
                pool.invalidateObject(pci.getUserPassKey(), pci);
            } catch (final Exception e3) {
                System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + pci);
                e3.printStackTrace();
            }
        }
    }
}
 
Example 2
Source File: KeyedCPDSConnectionFactory.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * If a fatal error occurs, close the underlying physical connection so as not to be returned in the future
 */
@Override
public void connectionErrorOccurred(final ConnectionEvent event) {
    final PooledConnection pc = (PooledConnection) event.getSource();
    if (null != event.getSQLException()) {
        System.err.println("CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR (" + event.getSQLException() + ")");
    }
    pc.removeConnectionEventListener(this);

    final PooledConnectionAndInfo info = pcMap.get(pc);
    if (info == null) {
        throw new IllegalStateException(NO_KEY_MESSAGE);
    }
    try {
        pool.invalidateObject(info.getUserPassKey(), info);
    } catch (final Exception e) {
        System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + info);
        e.printStackTrace();
    }
}
 
Example 3
Source File: CPDSConnectionFactory.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * This will be called if the Connection returned by the getConnection method came from a PooledConnection, and the
 * user calls the close() method of this connection object. What we need to do here is to release this
 * PooledConnection from our pool...
 */
@Override
public void connectionClosed(final ConnectionEvent event) {
    final PooledConnection pc = (PooledConnection) event.getSource();
    // if this event occurred because we were validating, ignore it
    // otherwise return the connection to the pool.
    if (!validatingSet.contains(pc)) {
        final PooledConnectionAndInfo pci = pcMap.get(pc);
        if (pci == null) {
            throw new IllegalStateException(NO_KEY_MESSAGE);
        }

        try {
            pool.returnObject(pci);
        } catch (final Exception e) {
            System.err.println("CLOSING DOWN CONNECTION AS IT COULD " + "NOT BE RETURNED TO THE POOL");
            pc.removeConnectionEventListener(this);
            try {
                doDestroyObject(pci);
            } catch (final Exception e2) {
                System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + pci);
                e2.printStackTrace();
            }
        }
    }
}
 
Example 4
Source File: CPDSConnectionFactory.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * If a fatal error occurs, close the underlying physical connection so as not to be returned in the future
 */
@Override
public void connectionErrorOccurred(final ConnectionEvent event) {
    final PooledConnection pc = (PooledConnection) event.getSource();
    if (null != event.getSQLException()) {
        System.err.println("CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR (" + event.getSQLException() + ")");
    }
    pc.removeConnectionEventListener(this);

    final PooledConnectionAndInfo pci = pcMap.get(pc);
    if (pci == null) {
        throw new IllegalStateException(NO_KEY_MESSAGE);
    }
    try {
        pool.invalidateObject(pci);
    } catch (final Exception e) {
        System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + pci);
        e.printStackTrace();
    }
}
 
Example 5
Source File: KeyedCPDSConnectionFactory.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
/**
 * This will be called if the Connection returned by the getConnection method came from a PooledConnection, and the
 * user calls the close() method of this connection object. What we need to do here is to release this
 * PooledConnection from our pool...
 */
@Override
public void connectionClosed(final ConnectionEvent event) {
    final PooledConnection pc = (PooledConnection) event.getSource();
    // if this event occurred because we were validating, or if this
    // connection has been marked for removal, ignore it
    // otherwise return the connection to the pool.
    if (!validatingSet.contains(pc)) {
        final PooledConnectionAndInfo pci = pcMap.get(pc);
        if (pci == null) {
            throw new IllegalStateException(NO_KEY_MESSAGE);
        }
        try {
            pool.returnObject(pci.getUserPassKey(), pci);
        } catch (final Exception e) {
            System.err.println("CLOSING DOWN CONNECTION AS IT COULD " + "NOT BE RETURNED TO THE POOL");
            pc.removeConnectionEventListener(this);
            try {
                pool.invalidateObject(pci.getUserPassKey(), pci);
            } catch (final Exception e3) {
                System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + pci);
                e3.printStackTrace();
            }
        }
    }
}
 
Example 6
Source File: KeyedCPDSConnectionFactory.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
/**
 * If a fatal error occurs, close the underlying physical connection so as not to be returned in the future
 */
@Override
public void connectionErrorOccurred(final ConnectionEvent event) {
    final PooledConnection pc = (PooledConnection) event.getSource();
    if (null != event.getSQLException()) {
        System.err.println("CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR (" + event.getSQLException() + ")");
    }
    pc.removeConnectionEventListener(this);

    final PooledConnectionAndInfo info = pcMap.get(pc);
    if (info == null) {
        throw new IllegalStateException(NO_KEY_MESSAGE);
    }
    try {
        pool.invalidateObject(info.getUserPassKey(), info);
    } catch (final Exception e) {
        System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + info);
        e.printStackTrace();
    }
}
 
Example 7
Source File: CPDSConnectionFactory.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
/**
 * This will be called if the Connection returned by the getConnection method came from a PooledConnection, and the
 * user calls the close() method of this connection object. What we need to do here is to release this
 * PooledConnection from our pool...
 */
@Override
public void connectionClosed(final ConnectionEvent event) {
    final PooledConnection pc = (PooledConnection) event.getSource();
    // if this event occurred because we were validating, ignore it
    // otherwise return the connection to the pool.
    if (!validatingSet.contains(pc)) {
        final PooledConnectionAndInfo pci = pcMap.get(pc);
        if (pci == null) {
            throw new IllegalStateException(NO_KEY_MESSAGE);
        }

        try {
            pool.returnObject(pci);
        } catch (final Exception e) {
            System.err.println("CLOSING DOWN CONNECTION AS IT COULD " + "NOT BE RETURNED TO THE POOL");
            pc.removeConnectionEventListener(this);
            try {
                doDestroyObject(pci);
            } catch (final Exception e2) {
                System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + pci);
                e2.printStackTrace();
            }
        }
    }
}
 
Example 8
Source File: CPDSConnectionFactory.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
/**
 * If a fatal error occurs, close the underlying physical connection so as not to be returned in the future
 */
@Override
public void connectionErrorOccurred(final ConnectionEvent event) {
    final PooledConnection pc = (PooledConnection) event.getSource();
    if (null != event.getSQLException()) {
        System.err.println("CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR (" + event.getSQLException() + ")");
    }
    pc.removeConnectionEventListener(this);

    final PooledConnectionAndInfo pci = pcMap.get(pc);
    if (pci == null) {
        throw new IllegalStateException(NO_KEY_MESSAGE);
    }
    try {
        pool.invalidateObject(pci);
    } catch (final Exception e) {
        System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + pci);
        e.printStackTrace();
    }
}
 
Example 9
Source File: KeyedCPDSConnectionFactory.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Closes the PooledConnection and stops listening for events from it.
 */
@Override
public void destroyObject(final UserPassKey key, final PooledObject<PooledConnectionAndInfo> p) throws Exception {
    final PooledConnection pc = p.getObject().getPooledConnection();
    pc.removeConnectionEventListener(this);
    pcMap.remove(pc);
    pc.close();
}
 
Example 10
Source File: MiniConnectionPoolManager.java    From fixflow with Apache License 2.0 5 votes vote down vote up
private synchronized void disposeConnection(PooledConnection pconn) {
	pconn.removeConnectionEventListener(poolConnectionEventListener);
	if (!recycledConnections.remove(pconn)) {
		// If the PooledConnection is not in the recycledConnections list,
		// we assume that the connection was active.
		if (activeConnections <= 0) {
			throw new AssertionError();
		}
		activeConnections--;
		semaphore.release();
	}
	closeConnectionAndIgnoreException(pconn);
	assertInnerState();
}
 
Example 11
Source File: KeyedCPDSConnectionFactory.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
/**
 * Closes the PooledConnection and stops listening for events from it.
 */
@Override
public void destroyObject(final UserPassKey key, final PooledObject<PooledConnectionAndInfo> p) throws Exception {
    final PooledConnection pc = p.getObject().getPooledConnection();
    pc.removeConnectionEventListener(this);
    pcMap.remove(pc);
    pc.close();
}
 
Example 12
Source File: CPDSConnectionFactory.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private void doDestroyObject(final PooledConnectionAndInfo pci) throws Exception {
    final PooledConnection pc = pci.getPooledConnection();
    pc.removeConnectionEventListener(this);
    pcMap.remove(pc);
    pc.close();
}
 
Example 13
Source File: ConnectionPoolingDataSourceIT.java    From snowflake-jdbc with Apache License 2.0 4 votes vote down vote up
@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 14
Source File: CPDSConnectionFactory.java    From commons-dbcp with Apache License 2.0 4 votes vote down vote up
private void doDestroyObject(final PooledConnectionAndInfo pci) throws Exception {
    final PooledConnection pc = pci.getPooledConnection();
    pc.removeConnectionEventListener(this);
    pcMap.remove(pc);
    pc.close();
}