MySql 是否支持 Java SE 应用程序的连接池?
从我读过的示例和 MysqlConnectionPoolDataSource 的文档中 http://www.control.auc.dk/~04gr733/filer/javadoc-DB-driver/com/mysql/jdbc/jdbc2/可选/MysqlConnectionPoolDataSource.html< /a> 看来 Java SE 应用程序无法使用连接池。这是真的吗?我可以让连接池适用于 MySql 吗?
对于PreparedStatement池来说也是如此吗?
更新: 我从 MySQL 网站上看到了有关连接池的以下内容。
/*
* Create a JNDI Initial context to be able to
* lookup the DataSource
*
* In production-level code, this should be cached as
* an instance or static variable, as it can
* be quite expensive to create a JNDI context.
*
* Note: This code only works when you are using servlets
* or EJBs in a J2EE application server. If you are
* using connection pooling in standalone Java code, you
* will have to create/configure datasources using whatever
* mechanisms your particular connection pooling library
* provides.
*/
这促使我寻找一种方法来为 Java SE 应用程序实现这一点。但重新阅读它,在我看来,他们正在引用您获取数据源引用的方式。我是否可以不使用依赖项注入框架或手动插入对我的 DAO 对象的数据源的依赖项?我打算尝试一下。
From the examples that I have read and from the documentation on MysqlConnectionPoolDataSource at http://www.control.auc.dk/~04gr733/filer/javadoc-DB-driver/com/mysql/jdbc/jdbc2/optional/MysqlConnectionPoolDataSource.html it seems that Java SE applications cannot use connection pooling. Is this true? Can I get connection pooling to work for MySql?
Does the same go for PreparedStatement pooling?
UPDATE:
I came across the following from the MySQL website with regards to their connection pooling.
/*
* Create a JNDI Initial context to be able to
* lookup the DataSource
*
* In production-level code, this should be cached as
* an instance or static variable, as it can
* be quite expensive to create a JNDI context.
*
* Note: This code only works when you are using servlets
* or EJBs in a J2EE application server. If you are
* using connection pooling in standalone Java code, you
* will have to create/configure datasources using whatever
* mechanisms your particular connection pooling library
* provides.
*/
This is what has made me look at a way to get this right for a Java SE application. But re-re-reading it, it seems to me that they are making reference to the manner in which you obtain a reference to the datasource. Could I not use a dependency injection framework or manual insert a dependency on the datasource for my DAO object? I am going to give that a try.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用一些第三方 JDBC 连接池,例如:
BoneCP , C3P0 或 DBCP
You could use some third party Connection Pool for JDBC such as:
BoneCP , C3P0 or DBCP
MysqlConnectionPoolDataSource 本身不会执行此操作,请使用类似 c3p0 的内容,或者查看示例位于连接池上的SDN。
The MysqlConnectionPoolDataSource won't do it itself, use something like c3p0 for that, or look at the examples at the SDN on Connection Pooling.