I have a problem when I get tomcat exceptions after I reset my MySQL database at the request of the user through the tomcat web application. I tried to break it down into setup, problem and my analysis to help everyone who is trying to read this.
Customization
reset consists mainly of calling a bash script from java code:
- Removing mysql root password
- Download in the old version of the database
- Running some scripts on it
- Recover all passwords
A user-initiated procedure usually restores the database to its previous state, but is also used to import the database from another system. Once everything is completed, the user will then try to access another part of the web application (i.e., with the same session without logging out / logging in), which executes a database query to obtain some data.
Problem
After querying the database using the tomcat application, an exception occurs:
Dec 29, 2014 3:49:50 PM ERROR BasicSecurityRealm:216 -
ERROR: ----- SQLException -----
Dec 29, 2014 3:49:50 PM INFO BasicSecurityRealm:218 - Exceptioncom.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 234,810 milliseconds ago. The last packet sent successfully to the server was 12 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
...
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2540)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2990)
Even if the user logs out and back, I will see this exception. If I refresh the page four times, the page will load a little more each time with some different exceptions (all the options above are CommunicationException caused by "EOFException: Unable to read response from server"). The last time everything works fine.
, , , - tomcat. , , , , , tomcat , . / , .
, , , JDBC. JNDI :
server.xml
<GlobalNamingResources>
<Resource name="jdbc/mydb"
auth="Container"
type="javax.sql.DataSource"
maxActive="30" maxIdle="30" maxWait="2147483647"
username="x" password="x"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb?autoReconnect=true"/>
web.xml:
<resource-ref>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
Java:
// Get connection to specified database
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/mydb");
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("...");
, / . , ds.getConnection, . , - reset ( , autoReconnect=true, ( )). ( , 4 5) , , reset. reset, .
?
autoReconnect=true, , , , . , , . , .
, EVERYWHERE. , , , . , , , . , .
reset/ . (.. java-, bash script), bash script (, - ). , , , .
Interceptors, , . .
!