Spring2.5.6整合hibernate3遇到的异常3

来源:互联网 发布:c语言如何判断闰年 编辑:程序博客网 时间:2024/05/22 07:03

最近做ssh的整合,第一次整合,不出意外的碰到了很多问题。

碰到了很多异常,为了以后查找方便,还有其他朋友碰到时查找的方便,我直接把异常复制。然后解决方法也写在下面。


关键异常描述,用红色加粗表示。其他的基本不用看。


org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354)
at com.william.dao.impl.UserDaoImpl.save(UserDaoImpl.java:34)
at com.william.service.impl.UserServiceImpl.add(UserServiceImpl.java:32)
at com.william.service.UserServiceTest.testAdd(UserServiceTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.SQLException: An SQLException was provoked by the following failure: com.mchange.v2.resourcepool.ResourcePoolException: Attempted to use a closed or broken resource pool
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:65)
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:62)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:531)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:82)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 30 more
Caused by: com.mchange.v2.resourcepool.ResourcePoolException: Attempted to use a closed or broken resource pool
at com.mchange.v2.resourcepool.BasicResourcePool.ensureNotBroken(BasicResourcePool.java:1632)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:524)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
... 33 more

===========================================================================================

org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354)
at com.william.dao.impl.UserDaoImpl.save(UserDaoImpl.java:34)
at com.william.service.impl.UserServiceImpl.add(UserServiceImpl.java:32)
at com.william.service.UserServiceTest.testAdd(UserServiceTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:82)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 30 more
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
... 33 more

问题原因以及解决办法:


这两个异常非常的诡异!居然是因为配置中的</value>另起了一行。 等我吐一口血先。


今天配置spring配置c3p0 datasource时老报错(如题),经多次测试发现原因

   

    原配置:

Java代码  收藏代码
  1. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  2.         <!-- 指定连接数据库的JDBC驱动 -->  
  3.         <property name="driverClass">  
  4.             <value>com.mysql.jdbc.Driver</value>  
  5.         </property>  
  6.         <!-- 连接数据库所用的URL -->  
  7.         <property name="jdbcUrl">  
  8.             <value>jdbc:mysql://localhost:3306/activiti?autoReconnect=true  
  9.             </value>  
  10.         </property>  
  11.         <!-- 连接数据库的用户名 -->  
  12.         <property name="user">  
  13.             <value>sa </value>  
  14.         </property>  
  15.         <!-- 连接数据库的密码 -->  
  16.         <property name="password">  
  17.             <value></value>  
  18.         </property>  
  19.         <!-- 设置数据库连接池的最大连接数 -->  
  20.         <property name="maxPoolSize">  
  21.             <value>20</value>  
  22.         </property>  
  23.         <!-- 设置数据库连接池的最小连接数 -->  
  24.         <property name="minPoolSize">  
  25.             <value>2</value>  
  26.         </property>  
  27.         <!-- 设置数据库连接池的初始化连接数 -->  
  28.         <property name="initialPoolSize">  
  29.             <value>2</value>  
  30.         </property>  
  31.         <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->  
  32.         <property name="maxIdleTime">  
  33.             <value>20</value>  
  34.         </property>  
  35.     </bean>  

  

    配置数据都无问题的情况下,发现以下情况会产生错误:

 

    情况1:value元素值之间出现空格(人为添加)

   

Java代码  收藏代码
  1. <property name="driverClass">  
  2.       <value>com.mysql.jdbc.Driver </value>  
  3. </property>  

 

    情况2:value结束标签换行(当value值比较长时,Ctrl+Shift+F的时就会换行)

   

Java代码  收藏代码
  1. <property name="driverClass">  
  2.        <value>com.mysql.jdbc.Driver  
  3.        </value>  
  4. </property>  

  

   今天产生错误属于情况2,因此建议配置datasource使用下面格式配置:

  

Java代码  收藏代码
  1. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  2.         <!-- 指定连接数据库的JDBC驱动 -->  
  3.         <property name="driverClass" value="com.mysql.jdbc.Driver">  
  4.         </property>  
  5.         <!-- 连接数据库所用的URL -->  
  6.         <property name="jdbcUrl"  
  7.             value="jdbc:mysql://localhost:3306/activiti?autoReconnect=true">  
  8.         </property>  
  9.         <!-- 连接数据库的用户名 -->  
  10.         <property name="user" value="sa">  
  11.         </property>  
  12.         <!-- 连接数据库的密码 -->  
  13.         <property name="password" value="">  
  14.         </property>  
  15.         <!-- 设置数据库连接池的最大连接数 -->  
  16.         <property name="maxPoolSize" value="20">  
  17.         </property>  
  18.         <!-- 设置数据库连接池的最小连接数 -->  
  19.         <property name="minPoolSize" value="2">  
  20.         </property>  
  21.         <!-- 设置数据库连接池的初始化连接数 -->  
  22.         <property name="initialPoolSize" value="2">  
  23.         </property>  
  24.         <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->  
  25.         <property name="maxIdleTime" value="20">  
  26.         </property>  
  27.     </bean>  

参考文章:http://aaron81939097.iteye.com/blog/1144642

原创粉丝点击