以下是我的助手类来获得DB连接:
我已经使用了C3P0作为连接池描述这里 。
public class DBConnection {
private static DataSource dataSource;
private static final String DRIVER_NAME;
private static final String URL;
private static final String UNAME;
private static final String PWD;
static {
final ResourceBundle config = ResourceBundle
.getBundle("props.database");
DRIVER_NAME = config.getString("driverName");
URL = config.getString("url");
UNAME = config.getString("uname");
PWD = config.getString("pwd");
dataSource = setupDataSource();
}
public static Connection getOracleConnection() throws SQLException {
return dataSource.getConnection();
}
private static DataSource setupDataSource() {
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass(DRIVER_NAME);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
cpds.setJdbcUrl(URL);
cpds.setUser(UNAME);
cpds.setPassword(PWD);
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);
return cpds;
}
}
在DAO我会写这样的事:
try {
conn = DBConnection.getOracleConnection();
....
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
logger
.logError("Exception occured while closing cursors!", e);
}
现在,我的问题是,我应该不屑做任何其他的清理比关闭游标上市(连接/声明/的resultSet / preparedStatement时)其他finally块。
什么是这个清理? 何时何地我应该这样做呢?
如果您发现任何错误在上面的代码,请大家指出。