Communications link failure due to: java.io.EOFException - jdbc

My webapp is running on Tomcat 5.5, I declared the datasource in web.xml:
<resource-ref>
<res-ref-name>jdbc/OrdiniWebDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
In context.xml (tomcat conf):
<Resource
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
maxActive="100"
maxIdle="30"
maxWait="10000"
name="jdbc/OrdiniWebDS"
password="[mypassword]"
type="javax.sql.DataSource"
url="jdbc:mysql://[myHost:port]/ordiniweb"
username="[myusername]"
/>
The database is a MySql 5.0.
Everything works well except that sometimes, after several hours of "unuse", at first access I got this Exception:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.io.EOFException
STACKTRACE:
java.io.EOFException
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1956)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2368)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2867)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3255)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1293)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1428)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
at com.blasetti.ordiniweb.dao.OrdiniDAO.caricaOrdine(OrdiniDAO.java:263)
...
** END NESTED EXCEPTION **
Last packet sent to the server was 0 ms ago.
com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2579)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2867)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708)
com.mysql.jdbc.Connection.execSQL(Connection.java:3255)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1293)
com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1428)
org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
com.blasetti.ordiniweb.dao.OrdiniDAO.caricaOrdine(OrdiniDAO.java:263)
...
I need to refresh and it works well again. Any idea? Thanks.

MySQL drops unused connections after a while because it assumes that the other side forgot to close it.
What you need to do is to configure a check that Tomcat should apply before it reuses a pooled connection. To do this, add ?autoReconnect=true&useUnicode=true&characterEncoding=utf8 to the end of the URL and add validationQuery="Select 1" to the Resource element:
<Resource
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
maxActive="100"
maxIdle="30"
maxWait="10000"
name="jdbc/OrdiniWebDS"
password="[mypassword]"
type="javax.sql.DataSource"
url="jdbc:mysql://[myHost:port]/ordiniweb?autoReconnect=true&useUnicode=true&characterEncoding=utf8"
username="[myusername]"
validationQuery="Select 1"
/>
[EDIT] This page gives more details: Configuring a MySQL Datasource in Apache Tomcat

Related

DB2 table not found when schema is mentioned in Jndi, but same works from normal db connection

When I am trying to connect to DB2 using schema definition in connection url it works fine
spring.datasource.url=jdbc:db2:/server:xxxx/ins:currentSchema=DEVSCHEMA;
but when the same is tried from tomcat jndi using
<Resource
auth="Container"
defaultAutoCommit="false"
driverClassName="com.ibm.db2.jcc.DB2Driver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
initialSize="10"
maxActive="100"
maxIdle="80"
minEvictableIdleTimeMillis="55000"
minIdle="30"
name="jdbc/connDB"
password="xxxxxxx"
removeAbandoned="true"
removeAbandonedTimeout="55"
testOnBorrow="true"
timeBetweenEvictionRunsMillis="34000"
type="javax.sql.DataSource"
url="jdbc:db2:/server:xxxx/ins:currentSchema=DEVSCHEMA;"
username="xxxxx"
validationInterval="34000"
validationQuery="select 1 from sysibm.sysdummy1"/>
I get error
DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=DEVSCHEMA.tablename, DRIVER=4.31.10
The jar used in the classpath is jcc-11.5.7.0.jar, ojdbc6-11.2.0.4.jar, for both instances.
Using springboot version 2.7.4
Is there any configuration that needs to be added to fix the issue.

Issue in starting Tomcat after migrating to Tomcat 9 from Tomcat 7

I have upgraded Tomcat from 7 to 9. I am getting exception while start up. It is throwing below exception:
oracle.jdbc.xa.client.OracleXADataSource cannot be cast to java.sql.Driver
context.xml has following :
<Resource auth="Container" driverClassName="oracle.jdbc.xa.client.OracleXADataSource"
logAbandoned="true" initialSize="1" maxActive="2" maxIdle="2"
minIdle="2" timeBetweenEvictionRunsMillis="34000"
inEvictableIdleTimeMillis="55000" validationQuery="SELECT 1 FROM DUAL"
validationInterval="34" testOnBorrow="true" removeAbandoned="true"
removeAbandonedTimeout="600" name="jdbc/regionalDS" password="XXX"
type="javax.sql.DataSource" url="jdbc:oracle:thin:#HOSTURL"
username="XXX" />
Please let me know, what changes I need to do.
Two things you can try :
1. Change the type to type="javax.sql.XADataSource"
2. Check that the url is in the form of url="jdbc:oracle:thin:#your_host_name:port_say_1521:service_or_sid"

Tomcat Connection Pool's attribute setting

My web application connect DB by Tomcat jdbc pool.
this is my Tomcat context.xml
<Resource
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
name="jdbc/OracleDB"
auth="Container"
type="javax.sql.DataSource"
initialSize="70"
maxActive="100"
maxIdle="30"
minIdle="10"
maxWait="5000"
removeAbandoned="true"
removeAbandonedTimeout="60"
username="XXOO"
password="XXOO"
driverClassName="oracle.jdbc.driver.OracleDriver"
validationQuery="SELECT 'OK'"
testWhileIdle="true"
testOnBorrow="true"
numTestsPerEvictionRun="5"
timeBetweenEvictionRunsMillis="30000"
minEvictableIdleTimeMillis="60000"
url="jdbc:oracle:thin:#XXOO:testdb"/>
connection timeout will be happened 1min50sec
If I use JDBC to connect DB,it would not happen.
So what's setting do I lose or modify?

JNDI DataSource: migrating from Tomcat to jBoss/Wildfly

I have a web application that uses Tomcat 7, Spring MVC 4.0, and JPA (Hibernate implementation). I am migrating this application to jBoss/Wildfly application server.
Currently, the DataSource is injected in the application with JNDI in a Spring configuration file:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MY_DB" expected-type="javax.sql.DataSource" />
The data source itself is defined in $CATALINA_HOME/conf/context.xml in the following way:
<Context>
<Resource name="jdbc/MY_DB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/MY_DB?useUnicode=yes&characterEncoding=UTF-8"
username="user1"
password=""
validationQuery="select 1"
removeAbandoned="true"
removeAbandonedTimeout="120"
maxWait="60"
maxActive="20"
maxIdle="10" />
</Context>
How can I define this JNDI DataSource in JBoss/Wildfly?
First, you must make the JDBC driver available to the application server; then you can configure the data source itself.
See more details in Data Source Configuration in AS 7 and DataSource configuration

How to set Oracle's fixedString property in Orion Application Server?

I want to set the fixedString=true connection property for the Oracle JDBC driver in the datasource definition in Orion Application Server 2.0.7 (latest stable release).
I've tried the following but didn't work:
<data-source
class="com.evermind.sql.DriverManagerDataSource"
name="datasource_name" location="jdbc/datasource_location"
connection-driver="oracle.jdbc.OracleDriver"
username="user" password="pass"
url="jdbc:oracle:thin:#//database_host:1521/XE" >
<property name="fixedString" value="true" />
</data-source>
Note: I've managed to set the property in Tomcat 6.0 this way (using the same Oracle JDBC driver, of course):
<Resource name="jdbc/datasource_name" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="user" password="pass" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#//database_host:1521/XE"
connectionProperties="fixedString=true;"
/>
But I need to set this in Orion. How can I do that?
It is a bug in the oracle application server you have to install OAS 10.1.2.3 has fix for that

Resources