Does anybody know how to avoid the Postgres driver from loading all metadata when connecting to a database?
It takes about a minute to connect with the driver, and from the console it takes like 1ms.
I found this answer but for JPA, I need something like this for JDBC: Hibernate Slow to Acquire Postgres Connection
This are my connection manager methods related to the question. I use a HashMap which holds a BasicDataSource per Database, a method to initialise the BasicDataSource for each DB, and another to get a connection from the corresponding DB datasource.
The delay of over a minute occurs on 'dataSource.getConnection()'
private HashMap<Database, BasicDataSource> dataSources = new HashMap<Database, BasicDataSource>();
private void initializeConnectionPools() throws FileNotFoundException, ReaderException, InterruptedException
{
if (dataSources.isEmpty())
{
Reader data = ReaderFactory.getReader(Constants.DATA_FILE);
DatabaseConnectionData connectionData = data.getDatabaseConnectionData();
String url = Constants.POSTGRESQL_JDBC_PREFIX + connectionData.getHost();
logger.info("INITIALIZING CONNECTIONS POOL [@" + connectionData.getHost() + "]");
BasicDataSource dataSource = null;
for (Database database : Database.values())
{
dataSource = new BasicDataSource();
dataSource.setDriverClassName(Constants.POSTGRESQL_DRIVER_CLASS);
dataSource.setUrl(url + database.toString().toLowerCase());
dataSource.setUsername(connectionData.getUser());
dataSource.setPassword(connectionData.getPassword());
// Initial size is:
// The number of browsers multiplied per number of instances
// allowed for each browser on GRID nodes
dataSource.setInitialSize(AppnameStation.values().length * 5);
dataSource.setMaxTotal(-1);
dataSources.put(database, dataSource);
}
logger.info("CONNECTIONS POOL INITIALIZED [@" + connectionData.getHost() + "]");
}
}
public Connection getConnection(Database database)
throws SQLException, FileNotFoundException, ReaderException, ClassNotFoundException
{
Connection connection = null;
BasicDataSource dataSource = dataSources.get(database);
logger.info("Getting " + database + " database connection");
connection = dataSource.getConnection();
logger.info("Connected to " + database + " database");
return connection;
}
Thanks!