问题
We have several servers that each run an Oracle database 11g Release 11.2.0.1.0 - 64bit. We are connecting via JDBC like this:
public Connection createConnection(String drvClass, String connURL, String user, String pass)
throws ClassNotFoundException, SQLException {
Class.forName(drvClass);
Connection conn = DriverManager.getConnection(connURL, user, pass);
for (SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning()) {
System.out.println("SQL Warning:");
System.out.println("State : " + warn.getSQLState());
System.out.println("Message: " + warn.getMessage());
System.out.println("Error : " + warn.getErrorCode());
}
return conn;
}
drvClass would be oracle.jdbc.OracleDriver
. The program which tries to connect runs on each server. The database is reachable from out of other programs with the exact same connection properties.
It is also possible to let this program run on another server and let it connect to the problematic database. It can establish the connection. The program does not work if it's running on the server locally. We tried both IP and servername.
On one server the code hangs at DriverManager.getConnection()
an I cannot find out why. Does anyone have any idea what could cause this?
There is no entry about this in the DB logs.
Stacktrace of blocking thread:
"pool-27-thread-1" - Thread t@1483
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at oracle.net.ns.Packet.receive(Packet.java:239)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:255)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:973)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:291)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:490)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:202)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:474)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.companyname.DBConnectionInternal.DBConnection.createConnection(DBConnection.java:19)
at com.companyname.exportadapter.ExportCollector.initDatabase(ExportCollector.java:259)
at com.companyname.exportadapter.ExportCollector.run(ExportCollector.java:120)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Locked ownable synchronizers:
- locked <50be77> (a java.util.concurrent.ThreadPoolExecutor$Worker)
If i set DriverManager.setLoginTimeout(10)
then i get
Io exception: Socket read timed out
.
回答1:
you may making some unneccessary connections.
make Connection
class static
,whenever you are creating new connection check older is alive or close then and then you must create new connection other wise return old connection.
like
if(conn!=null & !conn.isClosed()){
// code for create connection
}
It also depends on how the database side is configured, so check it with DBA of your system.
I would like to suggest using Connection pooling.
hope this helps.
回答2:
You might want to enable JDBC debug logging for the ojdbc driver: http://docs.oracle.com/cd/B28359_01/java.111/b31224/diagnose.htm That might give you some information about what the driver is doing.
Have you tried telnet-ing to the database server from the client machine (to assert it's reachable)?
回答3:
The server was misconfigured. For some reason it had a virtual adapter configured which returned an ip to which nothing could connect. From outside the resolving worked correctly. Don't know why it never timed out with the wrong IP though.
来源:https://stackoverflow.com/questions/23868433/oracle-jdbc-drivermanager-getconnection-hangs