Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-establish the connection after network failure in JBoss AS 7.x -JPA

My application is using JPA(Hbernate ORM) to connect to the SQL server 2008 , Which was deployed in JBoss AS 7.x server. If the network goes down and came up again , i am getting the following exception

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)...

I googled out this issue and found that this issue needs a change in the connection pool configuration to reconnect again using autoReconnect attribute ,but didn't found any example ,how to embed the autoReconnect with my Connection pool settings.Below is my Connection pool settings in the Standalone.xml file

<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>

Can you please let me know ,how to re-connect to the database , if Network goes down and reconnected again ?

like image 598
Chandu Avatar asked Dec 06 '25 09:12

Chandu


2 Answers

you could add

<check-valid-connection-sql>select 1 </check-valid-connection-sql>

to your data source configuration, or any other sql statement you want. This sql statement will be executed every time connection is checkout from the connection pool and if statement would fail, i.e. connection is closed you get, it would be destroyed and recreated/reconnected to sql server. That will make sure that your application (hibernate) always gets working sql connection.

like image 173
Tomaz Cerar Avatar answered Dec 08 '25 22:12

Tomaz Cerar


Also had hard time finding example for similar problem. For jboss7 added following lines to datasource configuration

<datasource>
  ...
  <validation>
    <check-valid-connection-sql>select 1 from dual</check-valid-connection-sql>
    <validate-on-match>false</validate-on-match>
    <background-validation>true</background-validation>
  </validation>
  ...
</datasource>
like image 43
andro83 Avatar answered Dec 09 '25 00:12

andro83