how to handle java.sql.SQLRecoverableException - java

We are getting the exception of java.sql.SQLRecoverableException: No more data to read from socket in our application, which is making the connection corrupted. After this, we are not able to fetch any connection from the connection pool and it is displaying the error as Connection closed. Now we are mitigating this issue by restarting the server. We found the root cause of this issue in one area of our application, we are calling a procedure which is calling an external db link and if that external db is down, we get this 'java.sql.SQLRecoverableException: No more data to read from socket` which is an expected behavior.
Here my problem is: As this procedure is calling from only a part of the application, it is affecting the whole connections in the connection pool. Is there a way to handle this exception in that specific part (where this exception is thrown) so that this should not affect the connection pool?
Note: Code for closing the connection is properly implemented in the code.

Related

How to handle MySQL CommunactionException which occurs when database turns off when application is running

Spring Boot Application uses MySQL + C3p0 DataSource.
This DataSource I use to work with JDBC and JPA(provider Hibernate).
In every JDBC DAO layer method I have try/catch SQLException. And this catch block realy catch this Communication Exception but..
But when I try so send requests more and more I start to have this stacktrace.
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
Caused by: java.net.ConnectException: Connection refused: connect
+ many details ..
And finally I catch in SQLException catch block:
java.sql.SQLException: Connections could not be acquired from the underlying database!
Before this message application looked like It can't connect and It know It.
It gives responses with 500 status code very fast.
After this message application looks like It try to connect and can't understand that It can't. In this case It gives responses very slowly.
After switching on the database, application works without restart.
The big stack trace might be printed by the web server on which your application is running. May not be from your code. You need to read about your web server on how to avoid such stack trace printing.

Connection reset by peer by oracle on processing of java application

I am trying to insert the tens of millions of data into oracle database by through java application but after 850k data inserted I am getting the error:
java.sql.exception: IO Exception :Connection reset by peer socket
write error.
I have a similar issue in my aplication.
In my case, after a few minutes in which the connection has not used, oracle server resets the connection and when I had to use it again, this exception was thrown.
I found that the problem was that oracle sets the timeout for connection at 10 minutes in sqlora.net file.
Maybe you can try to increase this time interval or check if the connection is still valid instead to check if it is closed (a connection can be not valid even if it is not closed).
The last option works fine for me.
I hope this helps.

How to fix error: [BEA][SQLServer JDBC Driver]No more data available to read

My java application does use DB Connection pooling. One of the functionality started failing today with this error:
[BEA][SQLServer JDBC Driver]No more data available to read
This doesn't occur daily. Once I restart my application server things look fine for some days and this error comes back again.
Anyone encountered this error? Reasons might vary, but I would like to know those various reasons to mitigate my issue.
Is it possible that the database or network connection has briefly had an outage? You might expect any currently open result sets then to become invalid with resulting errors.
I've never seen this particular error, but then I don't work with BEA or SQL Server, but a quick google does show other folks suggesting such a cause.
When you're using a connection pool, if you do get such a glitch, then all connections in teh pool become "stale" or invalid. My application server (WebSphere) has the option to discard the entire connection pool after particular errors are detected. The result then is that one unlucky request sees the error, but then subsequent requests get a new connection and recover. If you don't discard the whole pool then you get a failure as each stale connection is used and discarded.
I suggest you investigate to see a). whether your app server has such a capability b). how you application responds if the database is bounced, if this replicates the error then maybe you've found the cause.

Spring UncategorizedSQLException: ORA-01012

I am trying to retrieve data form an Oracle database using jdbc (ojdbc14.jar). I have a limited number of concurrent connections when connecting to the database and these connections are managed by Websphere connection pool.
Sometimes when I make the call I see an UncategorizedSQLException exception thrown in my logs with one of the following oracle codes:
ORA-01012 (not logged in) exception
ORA-17410 (connection timed out, socket empty),
ORA-02396 exceeded maximum idle time, please connect again
Other times I get no exceptions and it works fine.
Anyone understand what might be happening here?
In Websphere I have my cache statement size set to 10. Not sure if it is relevant in this situation, when it looks like the connection is being dropped.
It looks like the database is deciding to drop the connection. It's a good idea to write your code in a way that doesn't require that a connection be held forever. A better choice is to have the program connect to the database, do its work, and disconnect. This eliminates the problem of the database deciding to disconnect the application due to inactivity/server overload/whatever, and the program needing to figure this out and make a reasonable stab at reconnecting.
I hope this helps.

Why does autoReconnect=true not seem to work?

I am using JDBC to connect to a MySQL server (no connection pooling I think). In the connection URL I have autoReconnect=true
But my connection still times out. I've even checked conn.isClosed() and its false. But when I try to use the connection I get the following exception.
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: Software caused connection abort: socket write error
STACKTRACE:
java.net.SocketException: Software caused connection abort: socket write error
...
I know in Java 1.6 you can use conn.isValid(0) to check the connection, but I am using Java 1.5
Is there a way to either ensure it doesn't time out? Or am I going to have to upgrade to Java 1.6?
I had the same issue and it was absolutely maddening. Here's what the docs say on the MySQL website (emphasis mine)
Should the driver try to re-establish stale and/or dead connections? If enabled the driver will throw an exception for a queries issued on a stale or dead connection, which belong to the current transaction, but will attempt reconnect before the next query issued on the connection in a new transaction. The use of this feature is not recommended, because it has side effects related to session state and data consistency when applications do not handle SQLExceptions properly, and is only designed to be used when you are unable to configure your application to handle SQLExceptions resulting from dead and stale connections properly. Alternatively, investigate setting the MySQL server variable "wait_timeout" to some high value rather than the default of 8 hours.
In my experience, it doesn't seem like the "reconnect on the next query" functionality worked either, but I was using MySQL 4.0, which may have been the reason for that.
I ended up writing a mini-framework that catches the exceptions, checks for that specific error, and attempts to reconnect and retry the query if possible.
ETA: This link provides a bit more information, and indicates that autoReconnect will probably be removed in the future anyways.
autoReconnect still throws the exception so you can choose to do something about the situation if you like. If you catch it, you should find that the connection is there again afterward. (There's some more complexity if you're in a transaction -- your current transaction is pretty much dead.)

Categories