使用 C3P0 的 JDBC 连接池
以下是我用于获取数据库连接的帮助程序类:
我已按照所述使用了 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);
}
现在,我的问题是,除了关闭 finally 块中列出的游标(connection/statement/resultSet/preparedStatement)之外,我是否应该费心做任何其他清理工作。
什么是此清理?我应该在何时何地执行此操作?
如果您发现上面的代码有什么错误,请指出。
Following is my helper class to get DB connection:
I've used the C3P0 connection pooling as described here.
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;
}
}
in the DAO i'll be writing something like this:
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);
}
Now, my question is should I bother to do any other clean up other than closing the cursors(connection/statement/resultSet/preparedStatement) listed in the finally block.
What is this cleanup?? When and where should I do this?
Should you find anything wrong in the above code, please point out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于池化数据源,池中的连接实际上并未关闭,它们只是返回到池中。然而,当应用程序关闭时,那些与数据库的连接应该正确且实际地关闭,这就是最终清理的切入点。
顺便说一句,c3p0 项目几乎已经停滞不前,我建议您使用 Apache Commons DBCP 相反,它仍在维护中。
With a pooled data source, the connections in the pool are not actually closed, they just get returned to the pool. However, when the application is shut down, those connections to the database should be properly and actually closed, which is where the final cleanup comes in.
Incidentally, the c3p0 project is pretty much dead in the water, I recommend you use Apache Commons DBCP instead, it's still being maintained.
DAO 不应该负责获取与数据库的连接。他们无法知道它们何时被用作更大交易的一部分。您应该将数据源或连接实例传递到 DAO 中。
如果您的finally 块中对close 的任何调用抛出异常,则后面的任何调用都不会被调用。每个都需要位于自己的 try/catch 块中。我将它们作为静态方法放入实用程序类中。
DAOs should not be responsible for acquiring a connection to the database. They have no way to know when they're being used as part of a larger transaction. You should be passing the data source or connection instance into the DAO.
If any of the calls to close in your finally block throw an exception, none of the ones that follow will be called. Each one needs to be in its own try/catch block. I put them into a utility class as static methods.
代码对我来说看起来不错,但我会编写一个辅助方法来执行关闭操作,否则您将在每个 DAO 或方法中得到这个冗长的finally 块。也许您应该在关闭操作周围编写三个单独的 try-catch-块,以确保无论语句和结果集是否引发异常,连接都会关闭。另请注意 javadoc 说
因此,您不需要关闭上面示例中的结果集,但您可以这样做。
链接清理方法用于关闭数据源,这在大多数项目中是不需要的,因为只要您的应用程序在运行,DS 就会存在。
The code looks fine to me, but I would write a helper method that does the close operations or you'll get this verbose finally-block in every DAO or method. Maybe you should write three separate try-catch-blocks around the close Operations, to make sure the connection is closed no matter if the statement and resultset have thrown an exection. Also note that the javadoc says
So you don't need to close the resultset in the above example, but you could.
The linked cleanup method is for closing the datasource, what isn't needed in most projects because the DS lives as long as your app is running.
我使用Play Framework和Scala,因此以下示例是在play项目中。
步骤1.配置
在build.sbt中,如果使用mysql/hive作为数据库,则需要添加这些属性。
步骤2。如何访问它?您需要导入c3p0库。
步骤3.然后你需要创建实例。
第四步。你得到一个连接
I use Play Framework and Scala, so the following example is in play project.
Step1. configuration
In build.sbt, if you use mysql/hive as database, you need to add these properties.
Step2. how to access it? you need to import c3p0 library.
Step3. and then you need to create instance.
Step4. you get a connection