Here is my code for c3p0 configuration... when app server is being started, i am initializing the datasources that came from dbDef(db definitions table)
SessionFactory sessionFactoryByServer;
Connection jdbcConnectionC3P0;
List<Map<String, Object>> connectionSourceList; // object combopooleddatasource
@PostConstruct
public void init() {
List <DBDef> dbDefs = new ArrayList<DBDef>();
try {
dbDefs = getAllServerIpAddresses();
} catch (SQLException e) {
e.printStackTrace();
}
connectionSourceList = new ArrayList<Map<String, Object>>();
for (int i = 0; i < dbDefs.size(); i++) {
try {
Map<String, Object> cpdsMap = new HashMap<String, Object>();
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDebugUnreturnedConnectionStackTraces(false);
cpds.setDriverClass(dbDefs.get(i).getDriver());
cpds.setJdbcUrl(dbDefs.get(i).getConnectionURL());
cpds.setUser(dbDefs.get(i).getUserName());
cpds.setPassword(dbDefs.get(i).getPassword());
cpds.setDataSourceName(dbDefs.get(i).getIpAddress());
cpds.setAcquireIncrement(5);
cpds.setIdleConnectionTestPeriod(1800);
cpds.setMinPoolSize(10);
cpds.setAcquireRetryAttempts(1);
cpds.setPreferredTestQuery("SELECT 1");
cpds.setMaxPoolSize(50);
Configuration cfg = new Configuration();
cfg.configure(dbDefs.get(i).getHbmCfgXmlPath());
cfg.setProperty("connection.provider_class", dbDefs.get(i).getConnectionProvideClass())
.setProperty("hibernate.c3p0.acquire_increment", dbDefs.get(i).getAcquireIncrement())
.setProperty("hibernate.c3p0.max_size", dbDefs.get(i).getMaxSize())
.setProperty("hibernate.c3p0.min_size", dbDefs.get(i).getMinSize())
.setProperty("hibernate.c3p0.timeout", dbDefs.get(i).getTimeout())
.setProperty("hibernate.c3p0.max_statements", dbDefs.get(i).getMaxStatements())
.setProperty("hibernate.c3p0.idle_test_period", dbDefs.get(i).getIdleTestPeriod())
.setProperty("hibernate.show_sql", "true");
ServiceRegistryBuilder ssrb = new ServiceRegistryBuilder().applySettings(cfg.getProperties());
SessionFactory sessionFactory = cfg.buildSessionFactory(ssrb.buildServiceRegistry());
cpdsMap.put("serverIp", dbDefs.get(i).getIpAddress());
cpdsMap.put("dataSource", cpds);
cpdsMap.put("sessionFactory", sessionFactory);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
}
after initialize c3p0 datasources to List<Map<String, Object>> connectionSourceList;
, when i want to gether some data from 3 databases sequentially, in this list the third one forget some of its attributes i think. here is the problem where occured.
private SessionFactory retCurSessionFactory(String ipAddress) throws SQLException {
SessionFactory currentSessionFactory = null;
for (Map<String, Object> cpdsMap : connectionSourceList) {
sessionFactoryByServer = (SessionFactory)cpdsMap.get("sessionFactory");
jdbcConnectionC3P0 = ((ComboPooledDataSource)cpdsMap.get("dataSource")).getConnection();//getConnection brings null here, but only in app server not in my local machine.
currentSessionFactory = (SessionFactory)cpdsMap.get("sessionFactory");
}
}
return currentSessionFactory;
}
getConnection method brings null when program calls this method when running on app server. Tomcat is same as at development envirement.(copy-pasted) the internet connection between my place and app server sometimes cut off for miliseconds and before i found this getConnection is null i get this stacktrace below...
2016-03-28 19:00:21 INFO AbstractPoolBackedDataSource:2016-03-28 19:00:22 WARN BasicResourcePool:org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "xx.xxx.xx.x", user "username ", database "xxxx", SSL off
AND
java.sql.SQLException: Connections could not be acquired from the underlying database!
EDIT: The hba configuration content is added.
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host raritan +odbc 0.0.0.0/0 md5
I found a mistake while initiating ComboPooledDataSource object from dbDef object. I created a new row on database where I get connection properties. There must be a space char in the definition at DB row. So this code works fine for me to initiate db connections dynamically.