四个流行的Java连接池之Proxool篇(二)
.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
这里需注意三点:
1.别名与proxool中的别名保持一致.
2.路径确保正确
3一般网上只说明了以上两点,hibernate.proxool.existing_pool这个参数很重要
hibernate.proxool.existing_pool:此值设为 false,当 hibernate 开始被调用时,就会初始化 proxool,进行数据库连接等操作;
(3)要让程序直接使用proxool连接池,可以在web.xml中配置初始化servlet,在web容器加载的时候自动加载配置文件
注意:如果同时配置了web.xml和hibernate.cfg.xml会产生错误:
org.logicalcobwebs.proxool.ProxoolException: Parsing failed.
因为同名的proxool连接池已经启动,而hibernate开始运行时会自己启动关联的proxool连接池.
所以此时应改变hibernate.cfg.xml配置为:
(4)这一步是可选的 在应用中实时监控连接池
访问http://localhost:8080/项目名称/admin/proxool即可看到页面
4.错误解析:
常遇到的问题是
org.hibernate.HibernateException:Proxool Provider unable to load JAXPconfigurator file:proxool.xml
这有可能是由于proxool包版本的问题,我先后试过proxool-0.9.0RC1 proxool-0.9.0RC2proxool-0.9.0RC3
proxool-0.9.0 proxool-0.9.1 结果是高版本的0.9.0和0.9.1都会有这个问题,使用RC1或者以下的版本则没有.
基本使用代码,
Connection connection = null;
2.try {
3. Class.forName("org.hsqldb.jdbcDriver");
4. try {
5. connection =DriverManager.getConnection("jdbc:hsqldb:test");
6. } catch (SQLException e) {
7. LOG.error("Problem gettingconnection", e);
8. }
9.
10. if (connection != null) {
11. LOG.info("Gotconnection :)");
12. } else {
13. LOG.error("Didn't getconnection, which probably means that no Driver accepted the URL");
14. }
15.
16.} catch (ClassNotFoundException e) {
17. LOG.error("Couldn't finddriver", e);
18.} finally {
19. try {
20. // Check to see we actuallygot a connection before we
21. // attempt to close it.
22. if (connection != null) {
23. connection.close();
24. }
25. } catch (SQLException e) {
26. LOG.error("Problem closingconnection", e);
27. }
28.}
设定为datasource,
Connection connection = null;
2.try {
3. Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
4. try {
5. connection =DriverManager.getConnection("proxool.example:org.hsqldb.jdbcDriver:jdbc:hsqldb:test");
6. } catch (SQLException e) {
7. LOG.error("Problem gettingconnection", e);
8. }
9.
10. if (connection != null) {
11. LOG.info("Gotconnection :)");
12. } else {
13. LOG.error("Didn't getconnection, which probably means that no Driver accepted the URL");
14. }
15.
16.} catch (ClassNotFoundException e) {
17. LOG.error("Couldn't finddriver", e);
18.} finally {
19. try {
20. // Check to see we actuallygot a connection before we
21. // attempt to close it.
22. if (connection != null) {
23. connection.close();
24. }
25. } catch (SQLException e) {
26. LOG.error("Problemclosing connection", e);
27. }
28.}