java.sql.SQLRecoverableException: IO Error: Socket read timed out - spring

I am using Spring JdbcTemplate class for performing database operations. My server is Tomcat 7.
When I click the button of my application to perform some DB operations I get the following error
org.springframework.dao.DataAccessResourceFailureException: PreparedStatementCallback; SQL [XXXXX (my query) ];
IO Error: Socket read timed out; nested exception is java.sql.SQLRecoverableException: IO Error: Socket read timed out
Strange Behaviour:- This is happening only for the very first time. When I click the button next time everything is fine.
I even tried accessing the DB directly from toad and I am not getting any connection time out there.
DB Properties in context.xml:-
Resource name="jdbc/dsStaloneTelefoni"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#someIP:XXXXX"
username="XXXX"
password="XXXXXXXXXX"
testOnBorrow="true"
testOnReturn="true"
validationQuery="select 1 from dual"
maxActive="20"
maxIdle="30"
maxWait="-1"/>
Please help.

Related

Oracle DB connections not releasing from connection pool in Tomcat 8

We are migrating Tomcat6, java 6 and Oracle 10g web-applications to Tomcat 8, Java 8 and Oracle 10g. Our applications working fine after migrated, but initial connections (initialSize="5") available in connection pool not released after Tomcat shut down. When second time starting tomcat, its creating 5 more initial connections to pool. I am using below resource configuration in server.xml
<Resource name="TestAppDataSource"
auth="Container"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
initialSize="5"
maxActive="40"
maxIdle="40"
minIdle="5"
timeBetweenEvictionRunsMillis="30000"
minEvictableIdleTimeMillis="30000"
maxWait="10000"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1 from dual"
validationInterval="30000"
logAbandoned="true"
removeAbandonedTimeout="30"
removeAbandonedOnBorrow="true"
removeAbandonedOnMaintenance="true"
suspectTimeout="300"
maxAge="60000"
url="jdbc:oracle:thin:#//IP_ADDRESS:1521/SCHEMA_NAME"
username="USER_NAME"
password="PASSWORD" />
And below resource link configuration in application META_INF/context.xml
<ResourceLink
name="APP_TEST"
global="TestAppDataSource"
type="javax.sql.DataSource"
/>
I am using ojdbc7.jar for oracle driver. Please help whether i missed any configuration..
try with the follow option:
removeAbandoned = true
(boolean) Flag to remove abandoned connections if they exceed the
removeAbandonedTimeout. If set to true a connection is considered
abandoned and eligible for removal if it has been in use longer than
the removeAbandonedTimeout Setting this to true can recover db
connections from applications that fail to close a connection. See
also logAbandoned The default value is false.
Tomcat now use JDBC Connection Pool org.apache.tomcat.jdbc.pool that is a replacement or an alternative to the Apache Commons DBCP connection pool.
removeAbandoned is a option for JDBC Connection Pool
https://tomcat.apache.org/tomcat-8.0-doc/jdbc-pool.html
You have to add closeMethod="close" to your JDBC resource in context.xml. That way, Tomcat properly releases pending connections to the database.
Nitpick: when tomcat is shut down, the JVM is shut down, hence all of its resources are "released" too, and there is no more connection pool - what you meant is that the connections are not properly being shut down, so the DB is not being notified that they're closed, and hence the sessions there are not being ended. This is either because the pool is not getting a shutdown command, or because something else is hanging in tomcat during shutdown and hence it's not getting to the point of shutting down the pool, being force-killed by the shutdown script after a wait timeout has expired. You can take thread dumps during shutdown to see what it's waiting on, and look at catalina.out for messages about leaked threads (...has started a thread ... that's not been shut down ...). It is a common problem that webapps will launch long-running threads without daemonizing them - such webapps need to implement a ServletContextListener that will stop this thread/resource when the ServletContext is stopped.

Spring Hibernate Connection from database not released

I am getting the following error below
Unexpected error
org.springframework.transaction.CannotCreateTransactionException:
**CannotCreateTransactionException:** Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBC
Exception: Could not open connection
Here is the scenario in which it occurs :
I have recently moved some table from mysql to mongo. The code is written in such a way that either data would be taken from mongo/mysql.
The code is written in a method block which is annotated with #Transactional provided by spring framework.
There is hibernate layer which is using transaction provided by spring. c3p0 is the connection pool.
The parameter of connection pool is
hibernate.c3p0.min_size=5
hibernate.c3p0.timeout=1200
hibernate.c3p0.max_size=35
hibernate.c3p0.max_statements=50
The problem comes when we try to pull the data from mongo.Looks like the transaction is not getting closed because of mongo operation.The database connection is not getting released .It reaches the max size defined in the pool.
Tried the query in DB to find out the connection
show status like '%onn%';
Any suggestion to resolve this would really help.
Thanks

JDBC Connection pool configuration in Tomcat 7

I added following properties in our context.xml file, under tomcat 7x
validationQuery="SELECT 1 FROM DUAL"
testOnBorrow="true"
logValidationErrors="true"
driverClassName="oracle.jdbc.driver.OracleDriver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
logAbandoned="true"
maxActive="50"
maxIdle="20"
removeAbandoned="true"
removeAbandonedTimeout="180"
type="javax.sql.DataSource"
and I had shut down my Oracle database, and tried to connect the database through the application and I was getting exceptions as
### Error updating database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException:Could not get JDBC Connection; nested exception is java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
Which I think is an expected error.
After that, I brought back the Oracle and tried to connect the database through application, with out restarting tomcat. My expectation is, the application should be able to get the connection from the pool and work as normal. However, I saw
### Cause: java.sql.SQLRecoverableException: Closed Connection
; SQL []; Closed Connection; nested exception is java.sql.SQLRecoverableException: Closed Connection
Is the SQLRecoverableException, expected here. Do I need to restart tomcat, to establish a successfull connection back to database? In a typical production environments, if DB is shutdown for any reason and was brought back, should the application servers also need to be restarted. I think, it is not required.
Did I miss any other property in Context.xml that enables this ?
Thanks
you need to specify a url attribute that allows the connections to connect to the DB

connection from "spring data solr" to solr in openshift timeout

I am facing an issue connecting spring data solr to solr running on openshift.
<solr:solr-server id="solrServer"
url="http://solr-dashapramathi.rhcloud.com/" />
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"
scope="singleton">
<constructor-arg ref="solrServer" />
</bean>
is my configuration. i have also tried the url as "http://solr-dashapramathi.rhcloud.com/#/dashapramathi" I am running Solr 4.10.1 on openshift.
The error is as below:
IOException occured when talking to server at: http://solr-dashapramathi.rhcloud.com/dashapramathi; nested exception is org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://solr-dashapramathi.rhcloud.com/dashapramathi
Caused by:
org.springframework.data.solr.UncategorizedSolrException: IOException occured when talking to server at: http://solr-dashapramathi.rhcloud.com/dashapramathi; nested exception is org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://solr-dashapramathi.rhcloud.com/dashapramathi
at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:136)
at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:175)
at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:169)
at org.springframework.data.solr.repository.support.SimpleSolrRepository.save(SimpleSolrRepository.java:149)
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to solr-dashapramathi.rhcloud.com:80 timed out
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:129)
Could anyone please help me?
Judging by the documentation listed here http://docs.spring.io/spring-data/solr/docs/1.2.4.RELEASE/reference/html/solr.repositories.html It looks like it tries to connect through port 8993 which is why would would be receiving the timeout.
OpenShift only allows external connections to be made to 80,8080,443,8443. For inside the gear connections you can alter things to connect to more ports as per the following OpenShift Doc https://help.openshift.com/hc/en-us/articles/202185874.
I had the same problem, and I've managed to resolve it just by changing the timeout in the definition of the Solr server.
So, for your case it would be:
> <solr:solr-server id="solrServer"
> url="http://solr-dashapramathi.rhcloud.com/" timeout="1000"/>
or you can set a higher value for the timeout, depending on your connection.
In the documentation of the Spring Data Solr the 8993 port it is probably the port that the application server which hosts Solr runs on.

Connection to Oracle 11g XE failed due to IO Error: The Network Adapter could not establish the connection

I have a Dynamic Web Project in Eclipse and is trying to connect through JDBC to Oracle 11g Release 2 Express Edition. But I am getting the error with the following message :
Could not connect to Oracle 11g XE Conn.
Error creating SQL Model Connection connection to Oracle 11g XE Conn. (Error: IO Error: The Network Adapter could not establish the connection)
IO Error: The Network Adapter could not establish the connection
Error creating JDBC Connection connection to Oracle 11g XE Conn. (Error: IO Error: The Network Adapter could not establish the connection)
IO Error: The Network Adapter could not establish the connection
To add, since I am using Apache Tomcat 6.0.35, I have added the Resource and ResourceParams (for the database that I am trying to access) in the server.xml file of
\conf.
<Resource name="jdbc/FlightDB" scope="Shareable" type="javax.sql.DataSource"
auth="Container" description="Home Oracle 11g Express Edition"/>
<ResourceParams name="jdbc/FlightDB">
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:#server:1521:flightDB</value>
</parameter>
<parameter>
<name>username</name>
<value>system</value>
</parameter>
<parameter>
<name>password</name>
<value></value>
</parameter>
</ResourceParams>
Also, I have added the resource-ref element in web.xml of my current web application.
<resource-ref>
<res-ref-name>jdbc/FlightDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
But it is no help as the problem recurs.
Any help will be greatly appreciated. Look forward to replies.
Thanks,
Somnath
Try following url
jdbc:oracle:thin:#localhost:1521:xe
Last part is SID of server, not database name. Database (or Schema) name is determined by user name during log in. So if your database name is flightDB then there is also user called flightDB. Also check that user has sufficient privileges.

Resources