我正在一個webapp,我手動創建我的數據源。 (另請參閱我的其他問題爲什麼:How to use Spring to manage connection to multiple databases),因爲我需要連接到其他數據庫(dev,prod,qa,test)。Java數據源,如何處理它
現在我已經解決了它來選擇和切換數據庫之間。但是,如果用戶註銷我的應用程序。他想嘗試連接到其他數據庫。他仍然連接到相同的數據源,因爲在運行時myDs不爲空。如何在用戶註銷時正確處理此數據源?我不希望用戶在每次查詢數據庫時都創建數據源。
private DataSource createDataSource(Environment e) {
OracleDataSource ds = null;
String url = null;
try {
if (myDs != null) {
logger.info("myDs connection: " + etmetaDs.getConnection().getMetaData().getURL());
url = myDs.getConnection().getMetaData().getURL();
}
} catch (SQLException exc) {
// TODO Auto-generated catch block
exc.printStackTrace();
}
if (myDs == null) {
try {
ds = new OracleDataSource();
} catch (SQLException ex) {
ex.printStackTrace();
}
ds.setDriverType("oracle.jdbc.OracleDriver");
ds.setURL(e.getUrl());
try {
Cryptographer c = new Cryptographer();
ds.setUser(c.decrypt(e.getUsername()));
ds.setPassword(c.decrypt(e.getPassword()));
} catch (CryptographyException ex) {
logger.error("Failed to connect to my environment [" + e.getName() + "]");
ex.printStackTrace();
return null;
}
logger.info("Connecting to my environment [" + e.getName() + "]");
myDs = ds;
} else if (url.equals(e.getUrl())) {
} else {
}
return myDs;
}
您是否嘗試過使用Singleton作爲數據源? – Crowlix 2013-04-29 08:50:10