我的应用程序使用 JPA(Hbernate ORM) 连接到部署在 JBoss AS 7.x 服务器中的 SQL server 2008 。如果网络出现故障并再次出现,我会收到以下异常
14:59:27,996 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: The connection is closed.
14:59:28,002 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361)
14:59:28,012 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289)
14:59:28,020 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:261)
14:59:28,025 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at com.honeywell.domoweb.dataservice.dao.impl.UserDaoImpl.getUsers(UserDaoImpl.java:372)
14:59:28,030 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:59:28,034 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
14:59:28,039 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
14:59:28,044 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at java.lang.reflect.Method.invoke(Method.java:597)
14:59:28,047 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
14:59:28,052 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
14:59:28,058 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
14:59:28,064 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
14:59:28,069 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
14:59:28,075 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
14:59:28,080 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at $Proxy66.getUsers(Unknown Source)
14:59:28,083 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at com.honeywell.domoweb.dataservice.dao.impl.TemplateDaoImpl.getTemplate(TemplateDaoImpl.java:44)
14:59:28,089 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)...
我用谷歌搜索了这个问题,发现这个问题需要更改连接池配置才能使用 autoReconnect 属性重新连接,但没有找到任何示例,如何将 autoReconnect 嵌入到我的连接池设置中。下面是我的连接池设置在 Standalone.xml 文件中
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/DataServiceDS" pool-name="dataServicePool" enabled="true" use-java-context="true">
<connection-url>jdbc:sqlserver://ipaddress:1433;databaseName=myDataBase</connection-url>
<driver>sqlserver</driver>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>100</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>usename</user-name>
<password>password</password>
</security>
</datasource>
<drivers>
<driver name="sqlserver" module="com.microsoft.sqlserver">
<xa-datasource-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
你能告诉我,如果网络出现故障并重新连接,如何重新连接到数据库?