I'm using some code for multi threaded MySQL system. It's working fine and will put entry's into my database in under half a second. My problem is, the rate at which my program will successfully connect and write to the database, is not great. Some of the time it will work fine and other times I'll receive this error, or similar:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 27,697 milliseconds ago. The last packet sent successfully to the server was 1 milliseconds 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:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3670)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3559)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4110)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
at MySQL.putEntry(MySQL.java:46)
at MySQL.main(MySQL.java:105)
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:3119)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3570)
... 9 more
I'm really not sure why this happens? Executing too many statements from one address??
Anywho, here's my code:
private final ThreadedSQL sql;
private final DatabaseConnection connection;
private PreparedStatement statement;
public Hiscores() {
try {
loadConfig();
} catch (IOException e) {
e.printStackTrace();
}
sql = new ThreadedSQL(config);
connection = sql.getConnectionPool().nextFree();
}
public void putEntry(final Player player) {
try {
statement = connection.getConnection().prepareStatement(QUERY(player));
statement.execute();
statement.closeOnCompletion();
} catch(Exception e) {
e.printStackTrace();
}
}
The code I've not included is irrelevant, uses java.sql.Connection. Does anyone have any idea why this is occuring? Thanks.
You should close your statement and connection, once you are done, otherwise resource pool may be exhausted or your connection may time out. I recommend to obtain new connection from some JDBC pool. I used to use proxool few years ago in servlets environment. If you are in J2EE, then application server shall do it for you, but you have to use DataSource.
Related
my java code is like:
logger.info("start");
getJdbcTemplate().execute("call " + procedureName + "()");
and I got the exception:
org.springframework.dao.DataAccessResourceFailureException: StatementCallback; SQL [call PRMI_UPDATE_USER_LOGIN_INFO()]; Io ERROR: Connection reset; nested exception is java.sql.SQLException: Io ERROR: Connection reset
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:257)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:407)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:428)
Maybe it's caused by the long time waiting. I found that it printed "start" in log and after about 5 minutes I got the exception.
update at 2013-03-13:
I got that exception not only at calling oracle stored procedure but at druid's 'JdbcUtil.close(...)':
com.alibaba.druid.util.JdbcUtils.close:81 - close connection error
java.sql.SQLRecoverableException: Io Error: Connection reset
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:101)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:263)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:521)
at oracle.jdbc.driver.T4CConnection.logoff(T4CConnection.java:500)
at oracle.jdbc.driver.PhysicalConnection.close(PhysicalConnection.java:3509)
at com.alibaba.druid.filter.FilterChainImpl.connection_close(FilterChainImpl.java:167)
at com.alibaba.druid.filter.stat.StatFilter.connection_close(StatFilter.java:254)
at com.alibaba.druid.filter.FilterChainImpl.connection_close(FilterChainImpl.java:163)
at com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl.close(ConnectionProxyImpl.java:115)
at com.alibaba.druid.util.JdbcUtils.close(JdbcUtils.java:79)
at com.alibaba.druid.pool.DruidDataSource.shrink(DruidDataSource.java:1876)
at com.alibaba.druid.pool.DruidDataSource$DestroyConnectionThread.run(DruidDataSource.java:1694)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:96)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at oracle.net.ns.DataPacket.send(DataPacket.java:150)
at oracle.net.ns.NetOutputStream.flush(NetOutputStream.java:180)
at oracle.net.ns.NetInputStream.getNextPacket(NetInputStream.java:169)
at oracle.net.ns.NetInputStream.read(NetInputStream.java:117)
at oracle.net.ns.NetInputStream.read(NetInputStream.java:92)
at oracle.net.ns.NetInputStream.read(NetInputStream.java:77)
at oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:1034)
at oracle.jdbc.driver.T4CMAREngine.unmarshalSB1(T4CMAREngine.java:1010)
at oracle.jdbc.driver.T4C7Ocommoncall.receive(T4C7Ocommoncall.java:97)
at oracle.jdbc.driver.T4CConnection.logoff(T4CConnection.java:487)
The druid's JdbcUtil.close method is quite simple:
public static void close(Connection x) {
if (x == null) {
return;
}
try {
x.close();
} catch (Exception e) {
LOG.debug("close connection error", e);
}
}
the source code is :
https://github.com/alibaba/druid/blob/master/src/main/java/com/alibaba/druid/util/JdbcUtils.java
It should wait as long as it is needed. Forget about various hacks which try to "detect" deadlock based on timeout delay.
you should find also some ORA-XXXX error. Io ERROR: Connection reset does not look like Oracle error message, there should be some error number attached to it
the timeout 5 minutes is very strange value. Theoretically this can be setup also on database side. As profile parameter CPU_PER_CALL but in such a case you should get an error: ORA-02393: exceeded call limit on CPU usage. And you connection should NOT be lost
theoretically you can also have problems which dead connection detection, but 5 minutes timeout is too short for that
another possible source can be ORA-600 error. Oracle internal error, maybe your session process crashed and therefore TCP connection was lost
you should contact your local DBAs and ask then for cooperation. They should help you better than anonymous people on the Internet forum.
Maybe it's caused by the long time waiting
No it is not caused due to that
As Java Doc says about DataAccessResourceFailureException
Data access exception thrown when a resource fails completely: for
example, if we can't connect to a database using JDBC.
I read quite a few articles here about how to connect from java to a mysql database. Somehow, this does not work for me, and I cannot find out what I do wrong. First, here is my code:
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
class sqltest{
private static int uid = -1;
protected static Connection dbConn = null;
public static void main (String args[]){
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResultSet rs = null;
try{
dbConn = DriverManager.getConnection("jdbc:mysql://XXX.XXX.XX.XX:XXXX/peXXXXX","XXXXXX","XXXXXX");
PreparedStatement ps = dbConn.prepareStatement("select id from supervisor where username = philipp");
rs = ps.executeQuery();
uid = rs.getInt("id");
}catch(SQLException e){
e.printStackTrace();
}
System.out.println(uid);
}
}
The connection details (user, password, adress) are correct. There is only one thing I don't understand: There is already a java-web-applet which is establishing such a connection, and it uses no port (is this possible?).
Now these are my errors that I get:
When I use no port or the default 3306 port, i get this error:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection timed out: connect
STACKTRACE:
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:173)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:268)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2745)
at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at sqltest.main(sqltest.java:27)
** END NESTED EXCEPTION **
Last packet sent to the server was 28 ms ago.-1
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2820)
at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at sqltest.main(sqltest.java:27)
I assume that this is caused by a wrong port. Is this right?
When I use the port that i assume is the right one, I get no error, but the program just runs endlessly and will not come to any result.
Thx for any help!
Make sure you're connecting to the right host/port and that these are accessible.
To do this, e.g. open a terminal and type telnet <host> <port> from the same
machine on which you're running the Java code.
I did a quick online port scan for that address and it looks like the server side is not listening on port xxxxx. Maybe firewall configuration? Or just stopped server/wrong address.
Yes,
a wrong port may cause this exception. The statement looks wrong. If you have more that a million records in the table you might wait days.
Running endlessly seems very unusual given there are no loops in your code. My guess is the SQL query is taking a very long time.
Warning: this code is wrong!
PreparedStatement ps = dbConn.prepareStatement("select id from supervisor where username = philipp");
I guess you'd need to test this against MySQL to be sure, but in most databases I have worked with, an unquoted string literal like that will throw an error.
Also, runtime variables in a query ought to use bind variables with a prepared statement.
In regard to the connection failure, I would say that if specifying the correct port number yields a different error than not specifying a port number, then the default port number is not the same as what you are using. That is surprising. I think 3306 is fairly well known as MySQL's default port. I am not familiar with this driver: org.gjt.mm.mysql.Driver. Try the standard MySQL driver first.
Next, since using the right port number connects, but you say it sits there, My guess is select id from supervisor where username = philipp is running long. I am still surprised it compiled given what I said earlier, but you can try running it in MySQL yourself. Bring up the mysql command-line client and run it.
If it runs slow there too, then you know it is not a Java issue. Do some tuning on it. Perhaps the username column is not indexed and you have millions of rows. Check the execution plan to know for sure.
[Edit 1]
A good technique when a program seems "stalled" is to add print statements at various points. I suggest putting a bunch of System.out.println calls to see between which lines of code are we hung? That will help you narrow the problem space.
Ok this question is answered:
The SQL-Database can be contacted from outside my university environment. But it is located on a server which is blocking my communication. Unfortunately, this was not written into the server's log-file.
Thank you guys for your help!
I am working on Java EE JSF application using Hibernate with C3P0 connection pool. I have tried to search anything possible and impossible and tried many things, but couldnt figure this out.
The problem is handling database connection fail, for example when database is shut down. I couldn't find a way how to catch ecfeption which I could use to show error status on user interface. The only thing I could do is see some exceptions in console:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
at sun.reflect.GeneratedConstructorAccessor408.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2404)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2325)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.GeneratedConstructorAccessor407.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:195)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:184)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:200)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1086)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1073)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:44)
at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1810)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.GeneratedConstructorAccessor404.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1129)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:358)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2343)
... 18 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:241)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:308)
... 20 more
]]
But I can't catch this, it's just on console and it's not even printed in web browser response (client thread does not receive any exception, it's just hanging while trying to load the page for infinity).
Here is my C3P0 configuration:
c3p0.testConnectionOnCheckout=true
c3p0.idleConnectionTestPeriod=60
c3p0.acquireIncrement=1
c3p0.preferredTestQuery=SELECT 1
c3p0.acquireRetryAttempts=1
My question is: How to handle case of database connection fail in the user-friendly way?
You could use the Omnifaces FullAjaxExceptionHandler to display whatever you want for the exception. See the following link to documentation:
http://showcase.omnifaces.org/exceptionhandlers/FullAjaxExceptionHandler
OK, after of few days of work I have constructed a solution.
The problem was that just typical Hibernate session did not throw any exception while using it without database connected. But this piece of code does throw an exception when database not connected (so it can be used as a test):
Properties p = hibernateConnection.getCfg().getProperties();
String url = p.getProperty("hibernate.connection.url");
String user = p.getProperty("hibernate.connection.username");
String password = p.getProperty("hibernate.connection.password");
DriverManager.getConnection(url, user, password).close();
So, simple try-catch can be used. If an exception is caught, connection is dead.
I used this for regular checking of database connection. On application deploy, a TimerTask is scheduled to run every minute. When it catches an exception, it sets a static variable dbAvailable to false (otherwise to true). This variable is being checked on every HTTP client request and if it's true, error 503 is sent back in response.
For scheduling the timer I used ServletContextListener. C3P0 configuration mentioned in the question.
I have an application set up to pool mysql connections with BoneCP. Right now, the application isn't getting a ton of use, so the connections aren't used as frequently. After a certain amount of time, queries that once worked, start to fail, and I get messages similar to this:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 2,618,063 milliseconds ago. The last packet sent successfully to the server was 44,734 milliseconds 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:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3567)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3456)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
at com.jolbox.bonecp.PreparedStatementHandle.execute(PreparedStatementHandle.java:138)
I am setting up BoneCP like this:
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl("jdbc:mysql://" + hostname + "/" + database);
config.setUsername(username);
config.setPassword(password);
config.setMinConnectionsPerPartition(minPoolSize);
config.setMaxConnectionsPerPartition(maxPoolSize);
config.setIdleConnectionTestPeriodInMinutes(60);
config.setIdleMaxAgeInMinutes(240);
config.setPartitionCount(1);
connectionPool = new BoneCP(config);
I'm unsure of how to find out what the timeout is for the mysql server (it hasn't been changed from whatever the default is), but I get this error after around 5 or 10 mins of no connection pool activity, which seems extremely short.
Either you'll have to configure mysqls idle timeout (setting wait_timeout = X / interactive_timeout = X), or you could configure the connection pool to issue keep-alive statements:
config.setIdleConnectionTestPeriodInMinutes(10);
config.setConnectionTestStatement("/* ping */ SELECT 1"):
I am having issues trying to get a database connection using the code below:
try {
Class.forName("com.mysql.jdbc.Driver");
Properties p = new Properties();
p.put("user", user_name);
p.put("password", password);
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1/jsp_test", p);
} catch (SQLException ex) {
// handle any errors
ex.printStackTrace();
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
return false;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
The error message that is outputted is:
/usr/lib/jvm/java-6-openjdk/bin/java
-Didea.launcher.port=7532 -Didea.launcher.bin.path=/usr/bin/idea/bin
-Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-6-openjdk/jre/lib/jce.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/about.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/resources.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/management-agent.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/jsse.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/charsets.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/dnsns.jar:/home/bedtimes/Java
Projects/db_demo/out/production/db_demo:/opt/java/jre/lib/ext/mysql-connector-java-5.1.10-bin.jar:/usr/bin/idea/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain
Main
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
The last packet sent successfully to
the server was 0 milliseconds ago. The
driver has not received any packets
from the server. at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at
java.lang.reflect.Constructor.newInstance(Constructor.java:532)
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:2214)
at
com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:781)
at
com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:46)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at
java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at
com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at
com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352)
at
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284)
at
java.sql.DriverManager.getConnection(DriverManager.java:620)
at
java.sql.DriverManager.getConnection(DriverManager.java:169)
at
database.Database.connect(Database.java:80)
at Main.main(Main.java:13) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at
java.lang.reflect.Method.invoke(Method.java:616)
at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
Caused by:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
The last packet sent successfully to
the server was 0 milliseconds ago. The
driver has not received any packets
from the server. at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at
java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at
com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at
com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at
com.mysql.jdbc.MysqlIO.(MysqlIO.java:343)
at
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2137)
... 18 more Caused by:
java.net.ConnectException: Connection
refused at
java.net.PlainSocketImpl.socketConnect(Native
Method) at
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:310)
at
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:176)
at
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:163)
at
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
at
java.net.Socket.connect(Socket.java:542)
at
java.net.Socket.connect(Socket.java:492)
at
java.net.Socket.(Socket.java:389)
at
java.net.Socket.(Socket.java:232)
at
com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
at
com.mysql.jdbc.MysqlIO.(MysqlIO.java:292)
... 19 more SQLException:
Communications link failure
The last packet sent successfully to
the server was 0 milliseconds ago. The
driver has not received any packets
from the server. SQLState: 08S01
VendorError: 0
Process finished with exit code 0
I've, literally, no idea how to troubleshoot this error message. The database exists. The username and password exists. I've currently not added any tables to the database but I don't think that can be the issue, since I'm only making a connection after all...
I can provide extra information if needs be. I feel like I've tried a lot. Does anybody know any ways of getting further information on how and why it's failing?
Thanks for your help! :)
From your connection string, this database should be on the local machine. Can you try running this command to ensure the socket is open for connections?
telnet 127.0.0.1 3306
and ensure that it connects? You may not have configured your mysql instance to listen for connections from this machine or on this interface address. If the connection fails, you need to modify your mysql config, for example:
http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
I had the same symptoms but for me the solution was to change the bind-address to 0.0.0.0 in /etc/mysql/my.cnf
(Just posting this for others seeking an answer to this question)
You can try this instead:
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_test","user_name","password");
you can try one of the following
"jdbc:mysql://localhost:3306/jsp_test","username","password"
or
"jdbc:mysql://'your machines ip (without quote)':3306/jsp_test","username","password"
or
"jdbc:mysql://127.0.0.1:3306/jsp_test","username","password"
as a connection string.