部分错误信息如下:
Struts has detected an unhandled exception:
Messages:
A ResourcePool could not acquire a resource from its primary factory or source.
Connections could not be acquired from the underlying database!
Could not open connection
Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
File: com/mchange/v2/resourcepool/BasicResourcePool.java
Line number: 1,418
Stacktraces
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:544)
org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:457)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy22.validateMailAndPass(Unknown Source)
com.smart.agriculture.action.UserAction.login(UserAction.java:82)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
这个Bug花了我整个周末,直到周日晚上才解决,其实也没有解决,只是绕开了。
看到这个Bug我第一反应就是数据库可能没有正确连接,于是坚持了用户名、密码、数据库名,包括有没有非法字符空格之类的,之后检查相应的数据库驱动包有没有加入build path,果然tomcat下的lib目录里没有相应的jar。
折腾了一番还是没有结果,相应的c3p0 jar包也导入进去了,但是数据库总是连接不上。
而且用c3p0数据库连接池,项目发布的时候,特别卡慢,一度把tomcat的timeout增大到450s,
最后没办法了,我把c3p0换成dpcp的了,项目就正常可以运行了
c3p0连接配置
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/smart_argriculture_system"/>
<property name="user" value="root"/>
<property name="password" value="root1314"/>
<property name="maxPoolSize" value="50"/>
<property name="minPoolSize" value="3"/>
<property name="maxIdleTime" value="30"/>
<property name="initialPoolSize" value="3"/>
</bean> -->
改为dpcp配置
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/smart_argriculture_system"></property>
<property name="username" value="root"></property>
<property name="password" value="root1314"></property>
</bean>