我编写了一个为网站和多个移动客户端提供支持的服务器端应用程序。我使用 Hibernate 进行数据访问。后来我发现应用程序在一天后失败了!当我在网上查看时,我发现 MySQL 在 8 小时后终止“陈旧”连接是一个众所周知的问题。为了避免这种情况,我找到了很多建议,比如 include ?autoReconnect=true
, usingc3P0
等。由于autoReconnect
官方不鼓励(尤其是在生产环境中),而且因为我应用它时没有任何效果,所以我决定去c3p0
. 不幸的是,在我的文件中引入c3p0
配置后,应用程序开始在我调用的代码中hibernate.cfg.xml
抛出一个地方,
这意味着实际返回NullPointerException
dbSession.close()
HibernateUtil.getSessionFactory()
null
. 我已经添加了所需的罐子(c3p0-0.9.2-pre2.jar、hibernate-core-3.3.1.GA.jar、hibernate-c3p0-3.3.2.GA.jar、mchange-commons-java-0.2.1 .jar 和 c3p0-oracle-thin-extras-0.9.2-pre2.jar),尽管我不认为它们都是必需的。我浏览了许多讨论此问题的页面,但我仍然无法正确设置它。请帮助我使用“初学者友好”的易于实现的分步程序来设置c3p0
Hibernate。我在 JDK 1.6、MySQL 5.5 上使用 Hibernate 3.3.6,并且在 Netbeans 7.0 中进行开发。
这是我的hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/religion_app</property>
<property name="hibernate.connection.username">*****</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>
<!-- configuration pool via c3p0-->
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">120</property> <!-- seconds -->
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">180</property> <!-- seconds -->
<property name="c3p0.preferredTestQuery">select 1;</property>
<!--Mappings go here-->
</session-factory>
</hibernate-configuration>
这是我添加 c3p0(没有 slf4j)后得到的:
May 23, 2012 2:42:14 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet LoginChurch threw exception
java.lang.NullPointerException
at com.pacesolutions.religionapp.services.LoginChurch.processRequest(LoginChurch.java:109)
at com.pacesolutions.religionapp.services.LoginChurch.doPost(LoginChurch.java:138)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:722)
May 23, 2012 2:45:13 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet LoginChurch threw exception
java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361)
at org.hibernate.connection.C3P0ConnectionProvider.<clinit>(C3P0ConnectionProvider.java:52)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:73)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:414)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
at com.pacesolutions.religionapp.HibernateUtil.<clinit>(HibernateUtil.java:23)
at com.pacesolutions.religionapp.services.LoginChurch.processRequest(LoginChurch.java:68)
at com.pacesolutions.religionapp.services.LoginChurch.doPost(LoginChurch.java:143)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.26 logs.
我的大部分 Hibernate 配置文件都是由 Netbeans 生成的。我有数据库,然后我使用 Netbeans 中的 Hibernate 向导从中生成配置文件、实体类和映射文件。在我介绍 c3p0 之前,它们工作正常(但它们在 8 小时后失败)。我在我的项目中使用了 Mecurial,并将整个项目恢复到我引入 c3p0 之前的时间,它再次工作(8 小时)。甚至手动删除 hibernate.cfg.xml 中的所有 c3p0 配置也会使应用程序再次运行。我可能做错了什么?