我认为你应该扔或处理
SQLException
在
getInstance()
并使用
尝试使用资源
(自动关闭资源)而不是
finally
块。你应该
不
制作你的
instance
使用构造函数的常量,该构造函数将
Exception
用于初始化。
package com.example.webapp.db;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
public class DatabaseManager {
// not final anymore and null as default
private static DatabaseManager instance = null;
private DatabaseManager() {
PoolProperties p = new PoolProperties();
p.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
p.setDriverClassName("oracle.jdbc.driver.OracleDriver");
p.setUsername("scott");
p.setPassword("tiger");
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
+ "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
javax.sql.DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
// use a try-with resource to get rid of the finally block...
try (Connection con = datasource.getConnection()) {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from user");
int cnt = 1;
while (rs.next()) {
System.out.println((cnt++) + ". Host:" + rs.getString("Host")
+ " User:" + rs.getString("User")
+ " Password:" + rs.getString("Password"));
}
rs.close();
st.close();
// ... and handle the exception
} catch (SQLException e) {
System.err.println("SQLException while constructing the instance of DatabaseManager");
e.printStackTrace();
}
}
public static DatabaseManager getInstance() {
// check for null here:
if (instance == null) {
instance = new DatabaseManager();
}
return instance;
}
}
也许,为数据库连接创建初始化方法比初始化构造函数中的所有内容更好,但这是基于观点的。