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.
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.
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)?
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.
Related
I have a statement that takes about 20 minutes to run, which is of the form:
create table new_table diststyle key distkey(column1) sortkey(column2)
as (select ....);
When I run it using an SQL IDE or with the psql command line client, the statement executes successfully but when I run it from my Java program, the server closes the connection after 10 minutes with the following exception:
org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException for SQL [create table new_table diststyle key distkey(column1) sortkey(column2) as (select ....);];
SQL state [HY000]; error code [600001]; [Amazon](600001) The server closed the connection.;
nested exception is java.sql.SQLException: [Amazon](600001) The server closed the connection.
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84) ~[spring-jdbc-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:419) ~[spring-jdbc-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:538) ~[spring-jdbc-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at com.abc.mypackage.MyClass.myMethod(Myclass.java:123) [classes/:?]
Caused by: java.sql.SQLException: [Amazon](600001) The server closed the connection.
at com.amazon.support.channels.TLSSocketChannel.read(Unknown Source) ~[?:?]
Caused by: com.amazon.support.exceptions.GeneralException: [Amazon](600001) The server closed the connection.
at com.amazon.support.channels.TLSSocketChannel.read(Unknown Source) ~[?:?]
I'm using org.apache.commons.dbcp2.BasicDataSource to create connections. I've tried extending the timeout via defaultQueryTimeout, maxConnLifetimeMillis and socketTimeout but to no avail. The server keeps closing the connection after the same 10 minutes.
dataSource = new BasicDataSource();
dataSource.setUsername(dbUser);
dataSource.setPassword(dbPassword);
dataSource.setUrl(dbUrl);
dataSource.setDefaultAutoCommit(true);
dataSource.setTestOnBorrow(true);
dataSource.setTestOnReturn(true);
dataSource.setDriverClassName("com.amazon.redshift.jdbc41.Driver");
dataSource.setDefaultQueryTimeout(7200);
dataSource.setMaxConnLifetimeMillis(7200000);
dataSource.addConnectionProperty("socketTimeout", "7200");
How do I keep the connection alive for longer?
P.S. I do not have any problems establishing connections and running queries that take less than 10 minutes to finish.
You might want to extend your socket timeout.
Current it is 7200ms only:
dataSource.addConnectionProperty("socketTimeout", "7200");
check if the redshift server have a workload management policy that is timing out queries after 10 minutes.
your java code might be setting this policy
You need to set the tcpKeepAlive time to 1 min or less while getting the connection to redshift cluster.
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
props.setProperty("tcpKeepAlive", "true");
props.setProperty("TCPKeepAliveMinutes", "1");
DriverManager.getConnection("jdbc:redshift://"+endpoint+":"
+port+"/"+database, props);
OP here- I was able to make it work by writing wrappers over BasicDataSource and Connection to poll active connection with isValid(int) every few minutes (any frequency more than once-per-10-minutes works). In hindsight, it seems that most timeout-related properties on BasicDataSource apply to connections which are in the pool but are not being used. setDefaultQueryTimeout and tcpKeepAlive + TCPKeepAliveMinutes did not work.
P.S. It has been a while since I resolved this problem and I do not have the code for the wrappers now. Here's a brief description of the wrappers.
WrappedConnection class takes a Connection object (conn) and a TimerTask object (timerTask) in its constructor and implements the Connection interface by simply calling the methods from conn. timerTask calls this.isValid(100) every few minutes as long as the connection is active. WrappedConnection.close stops timerTask and then calls conn.close.
WrappedBasicDataSource implements the DataSource interface, redirecting methods to a BasicDataSource object. BasicDataSourceWrapper.getConnection gets a connection from the aforementioned BasicDataSource and generates a WrappedConnection using the connection and a new TimerTask object.
I might have missed explaining some details but this is the gist of it.
I wrote this simple class in NetBeans, in order to learn about connecting to and managing a Derby database through Java code, and am using the sample database provided by the IDE as the connection target. I believe it's a rather straightforward database connection example:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MainClass {
public static void main(String[] args) {
//Some code that allows me to start the connection only after keyboard input
System.out.println("Program will now connect to database");
try {
System.in.read();
}
catch (IOException ex) {
Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
}
// Redirecting DriverManager's output to the console's standard output
DriverManager.setLogWriter(new PrintWriter(System.out));
// This is where I connect to the database, execute a query and print a result
// Still barebones, I am learning SQL basics yet
String dbURL ="jdbc:derby:C:\\users\\project2100\\.netbeans-derby\\sample";
try (Connection conn = DriverManager.getConnection(dbURL, "app", "app")) {
System.out.println("Connected");
try (Statement stmt = conn.createStatement()) {
ResultSet output = stmt.executeQuery("SELECT * FROM CUSTOMER");
while (output.next()) {
System.out.println(output.getString("NAME"));
}
}
}
catch (SQLException ex) {
Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
}
// Recently added to keep the application alive
// This way, I can monitor memory usage on Task Manager
try {
System.in.read();
}
catch (IOException ex) {
Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Java's standard output gives me the results I expect, but DriverManager's output tells me something I'm worrying about; here's the full console output from an application run:
run:
Program will now connect to database
DriverManager.getConnection("jdbc:derby:C:\users\project2100\.netbeans-derby\sample")
trying org.apache.derby.jdbc.AutoloadedDriver40
SQLState(08004) vendor code(40000)
java.sql.SQLException: Connection refused : java.lang.OutOfMemoryError
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.<clinit>(Unknown Source)
at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
at org.apache.derby.jdbc.Driver20.connect(Unknown Source)
at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at javadbpg.MainClass.main(MainClass.java:35)
SQLState(08004) vendor code(40000)
java.sql.SQLNonTransientConnectionException: Connection refused : java.lang.OutOfMemoryError
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.<clinit>(Unknown Source)
at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
at org.apache.derby.jdbc.Driver20.connect(Unknown Source)
at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at javadbpg.MainClass.main(MainClass.java:35)
Caused by: java.sql.SQLException: Connection refused : java.lang.OutOfMemoryError
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 11 more
getConnection returning org.apache.derby.jdbc.AutoloadedDriver40
Connected
Jumbo Eagle Corp
New Enterprises
Wren Computers
Small Bill Company
Bob Hosting Corp.
Early CentralComp
John Valley Computers
Big Network Systems
West Valley Inc.
Zed Motor Co
Big Car Parts
Old Media Productions
Yankee Computer Repair Ltd
BUILD SUCCESSFUL (total time: 23 seconds)
NOTE: There is a 1-2 seconds hang between the first two errors.
Just recently, after looking around the Internet for what might be causing these OutOfMemoryErrors, I added that last try block and observed the application's memory footprint. It goes from an initial ~9MB to ~40MB.
Therefore, should I set some memory parameters inside the application, in order to reduce these errors, and eventually gain performance? (If so, could I have some pointers?) Or should i just leave everything as is and let DriverManager work by itself?
2014-08-07 Follow-up:
This time, I ran the application on debug mode, and wrote down the private memory usage step by step. Points of interest are as follows:
Memory amount right before the try (Connection conn = ... statement is 12MB;
Following this, the program hangs for about 5 seconds and fires all the OutOfMemoryErrors described above;
After the 5 seconds have elapsed, the program reserves a total memory of 37MB right before the System.out.println("Connected"); statement;
The executeQuery statement adds up another 7MB, total: 45MB. Execution time is negligible (<1/2sec);
Here's the derby.log file as requested by Bryan Pendleton, its state is after all the previous tests:
----------------------------------------------------------------
Thu Aug 07 10:10:11 CEST 2014:
Booting Derby version The Apache Software Foundation - Apache Derby - 10.10.1.3 - (1557168): instance a816c00e-0147-af84-a381-0000263f2b92
on database directory C:\Users\Project2100\.netbeans-derby\C3Subjects with class loader sun.misc.Launcher$AppClassLoader#58644d46
Loaded from file:/C:/Program%20Files/Java/jdk1.8.0_05/db/lib/derby.jar
java.vendor=Oracle Corporation
java.runtime.version=1.8.0_05-b13
user.dir=C:\Users\Project2100\Documents\NetBeansProjects\JavaDB Proving Grounds
os.name=Windows 7
os.arch=amd64
os.version=6.1
derby.system.home=C:\users\Project2100\.netbeans-derby
Database Class Loader started - derby.database.classpath=''
Yesterday, I kept searching on the Internet about memory management, and attempted to use the CLI arguments -Xms and -Xns to raise the application's starting memory, to no avail; these options manipulate only the application's reserved memory, and leaves the actual memory initially used at the usual 9-10MB. I'm getting the same memory values, errors and hangs anyway.
I guess that, for now, I'm going to chalk this behavior under loading database... and go on with SQL itself, since the system console doesn't list any complaints at all. However, if there is indeed a way to preemptively allocate more memory on database connection, I'd be grateful to get an answer about it.
Many thanks for your help!
There is a static variable in the org.apache.derby.impl.jdbc.EmbedConnection:
/**
* Static exception to be thrown when a Connection request can not
* be fulfilled due to lack of memory. A static exception as the lack
* of memory would most likely cause another OutOfMemoryException and
* if there is not enough memory to create the OOME exception then something
* like the VM dying could occur. Simpler just to throw a static.
*/
public static final SQLException NO_MEM =
newSQLException(SQLState.LOGIN_FAILED, "java.lang.OutOfMemoryError");
When you set up the logwriter for DriverManager, the SQLException will be printed:
public SQLException(String reason, String SQLState, int vendorCode) {
super(reason);
this.SQLState = SQLState;
this.vendorCode = vendorCode;
if (!(this instanceof SQLWarning)) {
if (DriverManager.getLogWriter() != null) {
DriverManager.println("SQLState(" + SQLState +
") vendor code(" + vendorCode + ")");
printStackTrace(DriverManager.getLogWriter());
}
}
}
That's why we have an ugly stack trace.
I tried to connect to my remote MySQL database, but I failed and got this error.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The confusion is that It was effective when I used MySQL-Front tool to connect the remote database, and I can ping to the IP address successfully. but when I used my code, it wound show the error after about ten seconds.
Also when I used the wrong username or password in my code, it wound show the wrong verification immediately. Did that prove it is no problem to set up conection?
Here is my code(It can work on my localhost database):
public static void main(String[] args) {
String url = "jdbc:mysql://(IP address):3306/";
String dbName = "talk";
String driver = "com.mysql.jdbc.Driver";
String userName = "talkroot";
String password = "123456";
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url + dbName,
userName, password);
System.out.println("connect");
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("end");
}
Here is the error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2120)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.test.TestMySQL.main(TestMySQL.java:16)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 21298 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3009)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2895)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3438)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2548)
at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1802)
at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3444)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2062)
... 12 more
Caused by: java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2452)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2906)
... 20 more
Anyone can help me please?
This com.mysql.jdbc.exceptions.jdbc4.CommunicationsException exception occurs if your database connection is idle for long time. This idle connection returns true on connection.isClosed(); but if we try to execute statement then it will fire this exception so I will suggest to go with database pooling.
Usually this error occurs when you don't use right port number. May be the machine which working on this database (client'db) has different port number. (Ex: 3307) . Go task manager==> Services ==> services and check is there other version of mysql. if they use 3306 change your port to 3307.
you can try to replace the mysql-connector-java-version.jar jar file. Some people update the jar file from 5.1.10 to 5.1.14, then it works. Wish you are luck..
update your drive version to:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
Double Check the following things:
Verify whether the IP address/Port Number is correct or not
Ping to the server using ping "IP address" it may tells is the server in startup mode/shutdown mode
If you are getting connection objects from connection pool, verify
once connections availability.
Hope this information helps...
I solved it by removing the port number from the connection URL.
So instead of using connection string as:
jdbc:mysql://12x.xxx.xxx.xxx:1527/yourdbname
use
jdbc:mysql://12x.xxx.xxx.xxx/yourdbname
Also when I used the wrong username or password in my code, it wound show the wrong verification immediately. Did that prove it is no problem to set up conection?
this is probably done locally. so it doesn't relate to why your code isn't working.
There seems to be many ways to go about this, so you can try multiple things; people have solved this link failure in different ways as per their own situations:
Here are some the solutions:
changing "bind-address" attribute
Uncomment "bind-address" attribute or change it to one of the following IPs:
bind-address="127.0.0.1"
or
bind-address="0.0.0.0"
commenting out "skip-networking"
If there is a "skip-networking" line in your MySQL config file, make it comment by adding "#" sign at the beginning of that line.
change "wait_timeout" and "interactive_timeout"
Add these lines to the MySQL config file:
[wait_timeout][1] = number
interactive_timeout = number
connect_timeout = number
check Operating System proxy settings
Make sure the Fire wall, or Anti virus soft wares don't block MySQL service.
change connection string
Check your query string. your connection string should be some thing like this:
dbName = "my_database";
dbUserName = "root";
dbPassword = "";
String connectionString = "jdbc:mysql://localhost/" + dbName + "?user=" + dbUserName + "&password=" + dbPassword + "&useUnicode=true&characterEncoding=UTF-8";
Make sure you don't have spaces in your string. All the connection string should be continues without any space characters.
Try to replace "localhost" with your port, like 127.0.0.1.
Also try to add port number to your connection string, like:
String connectionString = "jdbc:mysql://localhost:3306/my_database?user=root&password=Pass&useUnicode=true&characterEncoding=UTF-8";
Usually default port for MySQL is 3306.
Don't forget to change username and password to the username and password of your MySQL server.
update your JDK driver library file
test different JDK and JREs (like JDK 6 and 7)
don't change max_allowed_packet
"[max_allowed_packet][4]" is a variable in MySQL config file that indicates the maximum packet size, not the maximum number of packets. So it will not help to solve this error.
change tomcat security
change TOMCAT6_SECURITY=yes to TOMCAT6_SECURITY=no
use validationQuery property
use validationQuery="select now()" to make sure each query has responses
AutoReconnect
Add this code to your connection string:
&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
Your MySQL server isn't running. Check the same in the services page of the task manager if you are running Windows.
If you are working with Amazon Ubuntu instance then you have to follow this steps:
Check the amazon instance
Check the security group of instance
Assign the mysql/aura service and add your machine ip here or
service provider ip.
Please update your mysql configuration file in /etc/mysql/my.cnf file
By default, MySQL only allows connections from the localhost address. The configuration file is usually found in /etc/mysql/my.cnf
You will want to comment out the bind-address line by making it look like this:
#bind-address = 127.0.0.1
Next, you will need to restart the database server:
sudo /etc/init.d/mysql restart
I had faced this issue and struggled a lot like a lot. I did everything single thing said in all the portals and not worked for me. The only thing which worked to me was updating my jar file to the current new version . Earlier I was using 5.1.38 and now updated to 8.0.15 and just that my program worked super fine.
I tried to set a connection timeout with the following code:
public class ConnectionTimeout {
public static void main(String[] args) throws Exception {
String entry = "jdbc:oracle:thin:#xxx:1521:xxx";
Connection con=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
DriverManager.setLoginTimeout(1);
con=DriverManager.getConnection(entry,"username","password");
Statement s=con.createStatement();
s.execute("select 1 from dual");
s.close();
con.close();
}
}
The instance xxx is not existing. But I get the following exception:
Exception in thread "main" java.sql.SQLException: E/A-Exception: Socket is not connected
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:439)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at my.package.connection.timeout.ConnectionTimeout.main(ConnectionTimeout.java:22)
How I can implement a timeout to an not existing or available Oracle database instance?
Edit:
If I set the DriverManager.setLoginTimeout(30); to 30 second, the exception happens so fast as before!
Your DriverManager.setLoginTimeout(1); sets the maximum time in seconds for the driver to wait while connecting to the database. In your case, it's set to 1.
To have no limit, set setLoginTimeout(0) where 0 means no limit.
I hope this helps.
Update if your instance xxx doesn't exists, how would you expect your Oracle Driver to connect to the database? It won't make any difference how long you set your loginTimeout there's not "host" to connect to.
Because, in Java doc, it shows: timeout in seconds, but in the implementation of JDBC Oracle, it is milliseconds.
You can try using a measure of milliseconds.
I'm using C3P0 as a connection pool in Tomcat, and I'm seeing very worrying errors:
2010-09-16 13:25:00,160 [Timer-0] WARN com.mchange.v2.async.ThreadPoolAsynchronousRunner - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector#43502400 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
2010-09-16 13:25:01,407 [Timer-0] WARN com.mchange.v2.async.ThreadPoolAsynchronousRunner - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector#43502400 -- APPARENT DEADLOCK!!! Complete Status:
Managed Threads: 10
Active Threads: 0
Active Tasks:
Pending Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask#6e4151a7
Pool thread stack traces:
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#6,5,main]
java.lang.Object.wait(Native Method)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]
java.lang.Object.wait(Native Method)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]
java.lang.Object.wait(Native Method)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]
java.lang.Object.wait(Native Method)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#5,5,main]
java.lang.Object.wait(Native Method)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:534)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#4,5,main]
java.lang.Object.wait(Native Method)
... many more, exact same stack trace
Line 534 is:
while (true) {
Runnable myTask;
synchronized ( ThreadPoolAsynchronousRunner.this ) {
while ( !should_stop && pendingTasks.size() == 0 )
ThreadPoolAsynchronousRunner.this.wait( POLL_FOR_STOP_INTERVAL ); // <- here
if (should_stop) ...
It looks very much like all the threads are idle. They're waiting for work. 0 active threads, and only 1 task to complete. Any clues on what's going wrong?
Here's the configuration:
ds.setUser(userName);
ds.setPassword(password);
ds.setMaxPoolSize(16);
ds.setMaxConnectionAge(1800);
ds.setAcquireRetryAttempts(4);
ds.setMaxIdleTime(900);
ds.setNumHelperThreads(10);
ds.setCheckoutTimeout(1000);
I just experienced a similar issue against an Oracle database, but in my case Managed Thread and Active Thread counts were the same.
Managed Threads: 3
Active Threads: 3
For me it was actually an authentication error but appeared as the APPARENT DEADLOCK error because of the way I was doing login auditing.
2013-08-12 11:29:04,910 [Timer-4] WARN com.mchange.v2.async.ThreadPoolAsynchronousRunner: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector#34996454 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
2013-08-12 11:29:04,914 [Timer-4] WARN com.mchange.v2.async.ThreadPoolAsynchronousRunner: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector#34996454 -- APPARENT DEADLOCK!!! Complete Status:
Managed Threads: 3
Active Threads: 3
Active Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask#6730b844 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask#2f91ad49 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask#507ac05 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
Pending Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask#3aae7ed7
Pool thread stack traces:
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:150)
java.net.SocketInputStream.read(SocketInputStream.java:121)
oracle.net.ns.Packet.receive(Packet.java:300)
oracle.net.ns.DataPacket.receive(DataPacket.java:106)
oracle.net.ns.NetInputStream.getNextPacket(NetInputStream.java:315)
oracle.net.ns.NetInputStream.read(NetInputStream.java:260)
oracle.net.ns.NetInputStream.read(NetInputStream.java:185)
oracle.net.ns.NetInputStream.read(NetInputStream.java:102)
oracle.jdbc.driver.T4CSocketInputStreamWrapper.readNextPacket(T4CSocketInputStreamWrapper.java:124)
oracle.jdbc.driver.T4CSocketInputStreamWrapper.read(T4CSocketInputStreamWrapper.java:80)
oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:1137)
oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:290)
oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192)
oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:380)
oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:760)
oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:401)
oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:150)
java.net.SocketInputStream.read(SocketInputStream.java:121)
oracle.net.ns.Packet.receive(Packet.java:300)
oracle.net.ns.DataPacket.receive(DataPacket.java:106)
oracle.net.ns.NetInputStream.getNextPacket(NetInputStream.java:315)
oracle.net.ns.NetInputStream.read(NetInputStream.java:260)
oracle.net.ns.NetInputStream.read(NetInputStream.java:185)
oracle.net.ns.NetInputStream.read(NetInputStream.java:102)
oracle.jdbc.driver.T4CSocketInputStreamWrapper.readNextPacket(T4CSocketInputStreamWrapper.java:124)
oracle.jdbc.driver.T4CSocketInputStreamWrapper.read(T4CSocketInputStreamWrapper.java:80)
oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:1137)
oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:290)
oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192)
oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:380)
oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:760)
oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:401)
oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:150)
java.net.SocketInputStream.read(SocketInputStream.java:121)
oracle.net.ns.Packet.receive(Packet.java:300)
oracle.net.ns.DataPacket.receive(DataPacket.java:106)
oracle.net.ns.NetInputStream.getNextPacket(NetInputStream.java:315)
oracle.net.ns.NetInputStream.read(NetInputStream.java:260)
oracle.net.ns.NetInputStream.read(NetInputStream.java:185)
oracle.net.ns.NetInputStream.read(NetInputStream.java:102)
oracle.jdbc.driver.T4CSocketInputStreamWrapper.readNextPacket(T4CSocketInputStreamWrapper.java:124)
oracle.jdbc.driver.T4CSocketInputStreamWrapper.read(T4CSocketInputStreamWrapper.java:80)
oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:1137)
oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:290)
oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192)
oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:380)
oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:760)
oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:401)
oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
This sounds like you have already acquired a connection from the pool and do not return it in time.
C3P0 determines "apparent deadlocks" when a connection is acquired but not returned to the pool within the deadlock detection timeout.
If you move the connection acquisition closer to the "action" and immediately return it to the pool after the database work is done, this message will disappear.
This will sort out your problem
ds.setMaxStatements(1000);
ds.setMaxStatementsPerConnection(100); (the maximum number of prepared statments your system can execute on a single connection)
check out : https://forum.hibernate.org/viewtopic.php?t=947246&highlight=apparent+deadlock+c3p0
Remember to close your statements after you are done with them !!
My comment on answer https://stackoverflow.com/a/18192588/1019307 received enough up-votes to suggest it should be an answer.
I received this error because I couldn't get through the firewall to the database server. Check to see if that is your problem.
#eebbesen, I got the same error as you did. I am running Tomcat version 9.0.6. I have hibernate core ver 5.2.10, hibernate c3p0 ver 3.6.3 in my maven project. Mine was not an authentication error, but rather due to me having previously changed the name of my computer. This did not have an immediate effect on tomcat, but upon a restart of my machine, when I tried to bring up tomcat again via eclipse (Oxygen 2), I could no longer start eclipse due exactly to the issue you raised.
I googled this and I found this link, which tipped me off to the issue:
https://community.oracle.com/thread/339825
where it says:
First see if OracleServiceXE and OracleXETNSListener services are
running. Replace 127.0.0.1 in the url, with the IP or the name of your
machine. It must match the host declared in the tnsnames.ora file.
Later it mentions where to find this tnsnames.ora file, and for me it was here:
C:\oraclexe\app\oracle\product\11.2.0\server\network\ADMIN
Looking at this tnsnames.ora file, I saw this:
XE =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = MyMachineName-7)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = XE)
)
)
But I had recently renamed my machine to MyMachineName-5. I changed the 7 to a 5 and saved the file. I checked the "listener.ora" file in this directory, and it had the same issue:
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = MyMachineName-7)(PORT = 1521))
)
)
I changed the 7 to a 5 and saved the file.
Then I opened Task Manager, clicked the "Services" tab, and looked at the "Oracle" services. I did a restart on: OracleXETNSListener, OracleXEClrAgent, OracleServiceXE. I went to restart tomcat in eclipse again, and this time there was now problem.
Appendix:
I also googled this:
https://community.oracle.com/thread/2267906
This lead me to try:
1) Turned off the firewall in Windows Defender (already turned off mcaffee firewall)
2) Started sqlplus to make sure that I could log in with the credentials I was using in my hibernate file: hibernate.cfg.xml
C:\oraclexe\app\oracle\product\11.2.0\server\bin\sqlplus.exe
3) Started the desktop shortcut to Oracle Database 11g
This failed for me even after I fixed the machine name issue though, something I still have to look into.
4) Using dbVisualizer, I tried making a connection to Oracle. This only worked after I resolved the .ora file machine name issue: double click the connection and click the "ping server" button.
I've had the same problem but the cause was a bit hard to spot as it was caused by some simultaneous resources trying to acquire a connection at the same time.
As you can read if the pool had not been initialized the code provided to init it by calling a setup function.
public Connection getConnection() throws SQLException {
if (mCPDS == null) {
setupPool();
}
return mCPDS.getConnection();
}
The problem was that many resources were trying to acquire the connection at the beginning of the program so more than one were instantiating the pool causing your problem after a while.
The solution was just to declare the method synchronized to keep out other resources while one has called the method and it's still inside instantiating the pool for instance.
public synchronized Connection getConnection() throws SQLException {
if (mCPDS == null) {
setupPool();
}
return mCPDS.getConnection();
}
This may be a design error for not using a singleton but fixes the problem lacking some performance.
We ran into this problem and solved it by adding this to the C3P0 config:
<property name="statementCacheNumDeferredCloseThreads" value="1"/>
as per: this from the C3P0 doc
I had the same (was unable to detect) problem solved by correctly closing Statement and Resultset instances (somehow left unclosed):
String SQL = "SELECT 1";
try {
con = DriverManager.getConnection(host, userName, userPassword);
stmt = con.prepareStatement(SQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
try {
rs = stmt.executeQuery(SQL);
try {
rs.next();
// ...
} finally {
rs.close();
}
} finally {
stmt.close();
}
} catch (SQLException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
}
Hi my friend just to comment, I had the same case. I just configured my spring-hibernate eclipse project and showed up the same exception, it should be noted that my project still does not have any query.
I resolved that issue with below steps:
1) Clean project : Project--> Clean...
2) Build project : Project--> Build Project
I hope it works for you.
I've just had the same problem suddenly: after noticing that the deadlock was present only when launching my application in debug mode (I'm using IntelliJ) and it was fine when running with normal run, I started to dig it.
I finally figured out that a breakpoint was blocking the connection: I don't know why Intellij didn't "listen" that the application was passing through that breakpoint, but was hanged somewhere cause of a breakpoint, and this was causing appartent deadlock
After removing all breakpoints in my project, everything started smooth again.
Hope this helps someone
Similar problem was encountered on glassfish4 server while deploying the application. Turned out it was a database configuration issue. Just make sure your database connectivity configurations are proper, verify that the hostname provided in the configuration allows the connection to database. Try connecting to the database manually with the configured username and the hostname/domain. If required, allow the db user to connect from the required domain. Rebuild the application with correct db configurations and then deploy it.