java.sql.SQLRecoverableException - reconnect from jdbc

旧城冷巷雨未停 提交于 2019-12-10 11:37:33

问题


In my application which uses Apache JDBC connectivity to connect to a database, I am occasionally getting "java.sql.SQLRecoverableException". After searching on the root cause, I was able to learn it might be caused due the termination of connection from the oracle server end. I am using oracle 11G version.

Now my requirement is that I have to make sure the connection is still valid and not terminated before executing any query. I need to establish a new connection if the connection was terminated from oracle end.

I am not sure how I can achieve this, or to test it. I have tried getting the session from below query:

select * from v$session where username is not null;

It displays a single result when I have SQL Developer open and the applications (2 applications using same user credentials) are open too. I want to know how to terminate the connection from SQL and reproduce the "SQLRecoverableException" for testing and also reconnect to the database after the issue occurs.

Since I am new to database connections, I am not sure what to do or what to look into in order to achieve this. Kindly help me out with this.

My JDBC connectivity in spring-servlet.xml is given below:

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${myjdbc.driverClassName}" />
    <property name="url" value="${myjdbc.url}" />
    <property name="username" value="${myjdbc.username}" />
    <property name="password" value="${myjdbc.password}" />
</bean>
<context:property-placeholder location="classpath:myjdbc.properties" />

回答1:


It seems the connection is being dropped by some firewall or other activity. We have faced similar issue where the database was terminating the connections which were idle for 30 mins.

To Overcome the issue we have tuned database pool by specifying the following properties

testOnBorrow:-Setting it true will force the pooling provider to run the validation query while handing out the connection to the application.
testWhileIdle:-Setting it true will enable the validation when the connection is sitting idle in the pool.
timeBetweenEvictionRunsMillis:- Setting this property to non-zero will allow the evictor thread to run,which will test the idle connections.

Reproducing the issue will require to kill the connection on the database side.We have performed a small test using mssql wherein we can terminate the connection using the Server Tools and the pool was establishing the connection again.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${myjdbc.driverClassName}" />
    <property name="url" value="${myjdbc.url}" />
    <property name="username" value="${myjdbc.username}" />
    <property name="password" value="${myjdbc.password}" />
    <property name="testOnBorrow" value="true" />
    <property name="testWhileIdle" value="true" />
    <property name="timeBetweenEvictionRunsMillis" value="3000" />
</bean>

Note the timeBetweenEvictionRunsMillis is in millisonds.

The above configuration will checks the invalid connection and drops them from the pool if they are abruptly closed by the database or the firewall.



来源:https://stackoverflow.com/questions/56111248/java-sql-sqlrecoverableexception-reconnect-from-jdbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!