Can I reconnect to a JDBC database after accessing a database link of a remote database that has been disconnected? We have an application that uses one connection to the local oracle database, but sometimes reads data from a remote database through a database link ( REMOTE_DB ). The problem is that if the remote database is disconnected for some reason (network disconnection), after accessing the database link, the jdbc connection becomes unusable. I execute the following three SQL statements:
1. SELECT 1 FROM DUAL@REMOTE _DB => ok <<Network failure>> 2. SELECT 1 FROM DUAL@REMOTE _DB => SQLException. 3. SELECT 1 FROM DUAL => SQLException.
A specific Java exception with the JDBC driver ojdbc6.jar having operators 2 and 3,
java.sql.SQLRecoverableException: No more data to read from socket at oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:1185)
The reason I think this behavior is not βby designβ is because the same problem does NOT occur when I execute the same sequence using SQLPlus or Perl DBI . The problem occurs with Oracle 11 with several versions of the thin Oracle JDBC driver. To reproduce the problem, you can use the following java program.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class TestJdbc { private static Connection connect() throws Exception { String jdbcURL = "jdbc:oracle:thin:@localhost:1521:TNSNAME"; String user = "scott" ; String passwd ="tiger"; Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); return DriverManager.getConnection(jdbcURL,user,passwd); } public static void main(String[] args) throws Exception { Connection conn = connect(); PreparedStatement stServer = conn.prepareStatement("SELECT 'server' FROM DUAL@REMOTE _DB"); PreparedStatement stClient = conn.prepareStatement("SELECT 'client' FROM DUAL"); ResultSet resultSet; try { stServer.execute(); resultSet = stServer.getResultSet(); if (resultSet.next()) { System.out.println("server: " + resultSet.getString(1)); } } catch (SQLException e) { System.out.println("exception on server link: " + e); }
Closing and reopening the connection will solve the problem, but it would be preferable not to do this, since we may be in the middle of the transaction when an error occurs.
EDIT: Please note that with SQLPlus I can do the following: the problem of using the JDBC connection pool will not be resolved:
SQL> update my_table set ...; 1 row updated. SQL> select * from dual@REMOTE _DB; D - X <<Network failure>> SQL> select * from dual@REMOTE _DB; select * from dual@REMOTE _DB * ERROR at line 1: ORA-12545: Connect failed because target host or object does not exist SQL> update my_table set ...; 1 row updated. SQL> commit; Commit complete. SQL>
source share