错误提示:Cannot open connection Exception

使用hibernate3+spring+sqlserver2000sp3

运行提示Cannot open connection Exception

原因:当j2ee读取这些<bean>时,它会先查找父级的,然后才是子级的,但是有的时候读取子级可能会因为项目的原因,延迟或者是其他,导致读取不到子级的东西,导致connection无法建立,报了如此Exception

处理方式:

修改applicationContext.xml连接数据库

原自动生成

<bean id="datasource"
  class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName">
   <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
  </property>
  <property name="url">  

      <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ddyweb</value> 
  </property>
  <property name="username">  

          <value>sa</value> 
  </property>
  <property name="password" >

       <value>111</value> 
  </property>
 </bean>

修改成:

<bean id="datasource"
  class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName">
   <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
  </property>
  <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ddyweb">   
  </property>
  <property name="username" value="sa">   
  </property>
  <property name="password" value="111">  

  </property>
 </bean>

已标记关键词 清除标记
<!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="hiberbate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1:1433/jsp</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="c3p0.max_size">2</property> <property name="c3p0.min_size">2</property> <property name="hbm2ddl.auto">create</property> <mapping resource="com/test/hibernate/TestUser.hbm.xml" /> </session-factory> </hibernate-configuration> Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97) 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.test.h1.Base.main(Base.java:19) Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.net.SocketException MESSAGE: Connection reset STACKTRACE: java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:168) at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113) at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160) at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188) at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1931) at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:483) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:992) at com.mysql.jdbc.Connection.createNewIO(Connection.java:2568) at com.mysql.jdbc.Connection.<init>(Connection.java:1485) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:154) at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133) at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446) 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.test.h1.Base.main(Base.java:19) ** END NESTED EXCEPTION ** Last packet sent to the server was 1 ms ago. at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:554) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:992) at com.mysql.jdbc.Connection.createNewIO(Connection.java:2568) at com.mysql.jdbc.Connection.<init>(Connection.java:1485) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:154) at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133) at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446) ... 5 more
HTTP Status 500 - Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 加载jdbc.properties文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <!-- 定义数据源 --> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" ></property> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.autoReconnect">true</prop> </props> </property> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="query*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 那些类的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* cn.ittx.service.impl.*.*(..))" /> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" /> </aop:config> </beans>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页