Change connection pool at runtime


Retrieve pool data source, from spring context.

	com.mchange.v2.c3p0.ComboPooledDataSource pool = (com.mchange.v2.c3p0.ComboPooledDataSource) context.getBean("idDataSource");
	pool.setJdbcUrl("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=pepe.host.es) (PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)))");
	try {
		Connection con = pool.getConnection();
		if(con != null){
 			// TODO check database connection
		}else{
			throw new ConnectionException("Can't load new URL database connection");
		}
		
	} catch (SQLException e) {
		e.printStackTrace();
	}

The method setJdbcUrl(), do resetPoolManager. The true atribute means that not broke the actual connections.

    public void setJdbcUrl( String jdbcUrl )
    { 
        dmds.setJdbcUrl( jdbcUrl ); 
        this.resetPoolManager( false );
//      System.err.println("setting jdbcUrl: " + jdbcUrl + " [dmds@" + C3P0ImplUtils.identityToken( dmds ) + "]"); 
//      if (jdbcUrl == null)
//      new Exception("*** NULL SETTER ***").printStackTrace();
    }
Anuncios
Change connection pool at runtime