MySQL - Java SSL connection throwing error - java

I am trying to connect to MySQL DB from Java over SSL using the following program.
public class MySQLJDBC {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String m_host = "localhost";
String m_port = "3306";
String m_db = "test";
boolean isSSL = true;
String dbUrl = "jdbc:mysql://" + m_host + ":" + m_port + "/" + m_db + "?noDatetimeStringSync=true&verifyServerCertificate=false&useSSL=" +String.valueOf(isSSL);
String m_user = "root";
String m_pass = "Pass";
System.setProperty("javax.net.ssl.keyStore","D:/content/dev/connectors/Database (JDBC)/2011.1/src/mykeystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword","password");
System.setProperty("javax.net.ssl.trustStore","D:/content/dev/connectors/Database (JDBC)/2011.1/src/mystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword","password");
DriverManager.getConnection(dbUrl, m_user, m_pass);
System.out.println(System.getProperty("javax.net.ssl.keyStore"));
}
}
If I have the client keystores installed, this will throw the following exception.
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 328 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:2104)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
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:283)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MySQLJDBC.main(MySQLJDBC.java:20)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 328 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.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:102)
at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4357)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1302)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2032)
... 12 more
Caused by: java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at com.sun.net.ssl.internal.ssl.OutputRecord.writeBuffer(Unknown Source)
at com.sun.net.ssl.internal.ssl.OutputRecord.write(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecordInternal(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.sendChangeCipherSpec(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.sendChangeCipherAndFinish(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverHelloDone(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:87)
... 15 more
If I comment out the keystore lines, the connection happens.
//System.setProperty("javax.net.ssl.keyStore","D:/content/dev/connectors/Database (JDBC)/2011.1/src/mykeystore.jks");
//System.setProperty("javax.net.ssl.keyStorePassword","password");,
then the conection happens fine.
My Questions are,
Any idea why I am getting this error with this keystore specified?
I dont want a mutual authentication. Hence I want to say to the server not to validate the client certificate. Is there any property for this?
Any answers are appreciated.

If it works without the keystore and not with it, (a) there is something wrong with what's in it, and (b) you don't need it anyway.
If you don't want to send a client certificate, don't specify a keystore that contains one.

Related

Eclipse IDE - Unable to connect to mysql database

I tried to map the driver connector file from the reference library to the project file, but still gives the error.
Please could you help me connected to mysql database?
MySQL Connector for Java: mysql-connector-java-8.0.30.jar (added to the Java Build Path correctly)
JRE: jre-7u51-windows-i586.exe
PS: the DB is running perfectly in the MySQL workbench.
SC:
public static void connect() {
try{
Class.forName("com.mysql.jdbc.Driver");
dbURL = "jdbc:mysql://";
adresseServeur="localhost:3306/";
bddNAME="School_DB";
String username="root";
String pw="root";
con = DriverManager.getConnection(dbURL + adresseServeur + bddNAME , username , pw);
}catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"ERROR 01" + "\n", "Erreur système!",JOptionPane.ERROR_MESSAGE);
}
}
ERROR MSG:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
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:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:870)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2311)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
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:377)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at pack_School.School_Main.connect(School_Main.java:46)
at pack_School.School_Main.main(School_Main.java:23)
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2983)
at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1873)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1802)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1206)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2234)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
... 14 more

Experiencing weird issue while making FTP connection using hxtt diver

I am trying to access one file via FTP using hxtt driver, but I am able to make a connection. If I pass username and password in URL it is successful, but I get an exception if I pass username and password separately in properties file. Please see details below:
Jars I have tried with:
Text_JDBC40.jar latest jar
Text_JDBC41.jar latest jar
Text_JDBC42.jar
latest jar
Works fine:
Class.forName("com.hxtt.sql.text.TextDriver").newInstance();
String url = "jdbc:csv:/ftp://<username>:<password>#www.hxtt.net/testdemo/sample?_CSV_Separator=,";
String sql = "select * from Test";
Connection con = DriverManager.getConnection(url);
Throws an exception:
Class.forName("com.hxtt.sql.text.TextDriver").newInstance();
String url = "jdbc:csv:/ftp://www.hxtt.net/testdemo/sample?_CSV_Separator=,";
Properties prperties = new Properties();
prperties.put("user", "<username>");
prperties.put("password", "<password>");
String sql = "select * from Test";
Exception:
java.sql.SQLException: Failed to login ftp server www.hxtt.net:21
at com.caigen.global.ao.a(Unknown Source)
at com.hxtt.concurrent.e.a(Unknown Source)
at com.hxtt.concurrent.e.<init>(Unknown Source)
at com.hxtt.concurrent.ak.aL(Unknown Source)
at com.hxtt.concurrent.ak.if(Unknown Source)
at com.hxtt.concurrent.bm.a(Unknown Source)
at com.hxtt.concurrent.ak.<init>(Unknown Source)
at com.hxtt.concurrent.az.a(Unknown Source)
at com.hxtt.concurrent.a9.a(Unknown Source)
at com.hxtt.concurrent.ak.a(Unknown Source)
at com.hxtt.concurrent.av.iL(Unknown Source)
at com.hxtt.concurrent.i.iN(Unknown Source)
at com.hxtt.sql.b7.a(Unknown Source)
at com.hxtt.sql.text.f.<init>(Unknown Source)
at com.hxtt.sql.text.EmbeddedDriver.connect(Unknown Source)
at com.hxtt.global.g.a(Unknown Source)
at com.hxtt.sql.HxttDriver.if(Unknown Source)
at com.hxtt.sql.HxttDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at org.test.HXTTConnectionTest4.main(HXTTConnectionTest4.java:35)
java.sql.SQLException: Failed to login ftp server www.hxtt.net:21
at com.caigen.global.ao.a(Unknown Source)
at com.hxtt.concurrent.e.a(Unknown Source)
at com.hxtt.concurrent.e.<init>(Unknown Source)
at com.hxtt.concurrent.ak.aL(Unknown Source)
at com.hxtt.concurrent.ak.if(Unknown Source)
at com.hxtt.concurrent.bm.a(Unknown Source)
at com.hxtt.concurrent.ak.<init>(Unknown Source)
at com.hxtt.concurrent.az.a(Unknown Source)
at com.hxtt.concurrent.a9.a(Unknown Source)
at com.hxtt.concurrent.ak.a(Unknown Source)
at com.hxtt.concurrent.av.iL(Unknown Source)
at com.hxtt.concurrent.i.iN(Unknown Source)
at com.hxtt.sql.b7.a(Unknown Source)
at com.hxtt.sql.text.f.<init>(Unknown Source)
at com.hxtt.sql.text.EmbeddedDriver.connect(Unknown Source)
at com.hxtt.global.g.a(Unknown Source)
at com.hxtt.sql.HxttDriver.if(Unknown Source)
at com.hxtt.sql.HxttDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at org.test.HXTTConnectionTest4.main(HXTTConnectionTest4.java:35)

java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect to this MySQL server"

I am currently trying to access mySQL-database through a Java Application.
In order to do so, I downloaded the "mysql-connector-java-5.1.42-bin.jar" file and imported it to Eclipse Neon.3.
My MySQL Server is up and running as following screenshot Shows:
I have tried it with following code:
final private String user = "root";
final private String passwd = "root";
public void connectToDB() throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
String url= "jdbc:mysql://xxx:3306/pilotproject";
// Setup the connection with the DB
connect = DriverManager.getConnection(url, user, passwd);
System.out.println("Database connection established");
{
{
but I am getting following error:
java.sql.SQLException: null, message from server: "Host 'xxx.fuchs.com' is not allowed to connect to this MySQL server"
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1040)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2194)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2225)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2024)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
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:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MySQLAccess.connectToDB(MySQLAccess.java:29)
at PilotFrame1$1.run(PilotFrame1.java:44)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Any suggestions on how I can solve this Problem?
I appreciate every help or advice you can give me, thanks a lot in advance!
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
FLUSH PRIVILEGES;
this helped!

unable to connect to remote database located on server

I am trying to update the database table which is hosted on the server through MYSQL JDBC connector using eclipse.
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://49.**.***.115:3306/databasename";
String name = "user";
String password = "passs";
But I not able to connect to the database from my local machine. I checked all the information provided above are correct.
Can please some tell me what to i do next?
Here is the error:
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(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.<init>(MysqlIO.java:357)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
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.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at connecttoDB.getconnection(connecttoDB.java:27)
at updateDB.main(updateDB.java:17)
Caused by: java.net.ConnectException: Connection refused: 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:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:307)
... 16 more
Have you verified that you can connect to your database server from the machine you are running this code on? It looks like there is a network problem. A good idea as a first step to diagnosing MySQL connection problems is to always try connecting from the command line to rule out networking or credentials problems
mysql -uuser -p -h49.**.***.115 databasename
where "user" is the username "databasename" is the database and "49.**.***.115" is the ip of the database server.
check the following steps:
1- Ping the IP address to see if you can establish connection, There might be a firewall that is blocking incoming/outgoing communication.
2- Check that your ojdbc6/ojdbc14.jar is loaded in the same directory of your application.

Java app handling for connections getting dropped

My app seems to be hanging overnight because of the connection getting dropped(I think that's the problem.) How can I structure my app so that it can try to roll up a new connection?
Since the incident I have updated the getConnection() method that my app uses as so:
private Connection getConnection() {
boolean failed = false;
try{
failed = !connection.isValid(1000);
}catch(SQLException ex){
System.out.println("WARNING: Connection not valid!");
}
try{
failed = connection.isClosed();
}catch(SQLException ex){
System.out.println("WARNING: Connection is closed!");
}
if(failed){
System.out.println("Renewing connection");
this.initializeConnection();
}
return connection;
}
This is the output:
** BEGIN NESTED EXCEPTION **
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
MESSAGE: The last packet successfully received from the server was54521 seconds
ago.The last packet sent successfully to the server was 54521 seconds ago, which
is longer than the server configured value of 'wait_timeout'. You should consi
der either expiring and/or testing connection validity before use in your applic
ation, increasing the server configured values for client timeouts, or using the
Connector/J connection property 'autoReconnect=true' to avoid this problem.
STACKTRACE:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet success
fully received from the server was54521 seconds ago.The last packet sent success
fully to the server was 54521 seconds ago, which is longer than the server conf
igured value of 'wait_timeout'. You should consider either expiring and/or testi
ng connection validity before use in your application, increasing the server con
figured values for client timeouts, or using the Connector/J connection property
'autoReconnect=true' to avoid this problem.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
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:1
074)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3246)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1917)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
at com.mysql.jdbc.ConnectionImpl.setCatalog(ConnectionImpl.java:4962)
at com.mysql.jdbc.DatabaseMetaData.getCallStmtParameterTypes(DatabaseMet
aData.java:1506)
at com.mysql.jdbc.DatabaseMetaData.getProcedureOrFunctionColumns(Databas
eMetaData.java:4120)
at com.mysql.jdbc.DatabaseMetaData.getProcedureColumns(DatabaseMetaData.
java:4057)
at com.mysql.jdbc.CallableStatement.determineParameterTypes(CallableStat
ement.java:809)
at com.mysql.jdbc.CallableStatement.<init>(CallableStatement.java:609)
at com.mysql.jdbc.JDBC4CallableStatement.<init>(JDBC4CallableStatement.j
ava:46)
at sun.reflect.GeneratedConstructorAccessor6.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.CallableStatement.getInstance(CallableStatement.java:5
05)
at com.mysql.jdbc.ConnectionImpl.parseCallableStatement(ConnectionImpl.j
ava:3881)
at com.mysql.jdbc.ConnectionImpl.prepareCall(ConnectionImpl.java:3965)
at com.mysql.jdbc.ConnectionImpl.prepareCall(ConnectionImpl.java:3939)
at com.protocase.hmiclient.db.HMIDatabaseAdapter.getAvailableBatchesForW
orkstation(HMIDatabaseAdapter.java:471)
at com.protocase.hmiclient.views.WorkstationContainer.getBatches(Worksta
tionContainer.java:74)
at com.protocase.hmiclient.views.BatchList.<init>(BatchList.java:55)
at com.protocase.hmiclient.views.WorkstationContainer.goToBatchList(Work
stationContainer.java:56)
at com.protocase.hmiclient.views.forms.BatchListControlPanel.refreshButt
onActionPerformed(BatchListControlPanel.java:118)
at com.protocase.hmiclient.views.forms.BatchListControlPanel.access$200(
BatchListControlPanel.java:16)
at com.protocase.hmiclient.views.forms.BatchListControlPanel$3.actionPer
formed(BatchListControlPanel.java:64)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.net.SocketException: Software caused connection abort: socket wr
ite error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3227)
... 61 more
** END NESTED EXCEPTION **
This exception suggests that you're opening the connection only once during application's startup and keeping forever open during the application's lifetime. This is bad. The DB will reclaim the connection sooner or later because it's been open for too long. You should close connections properly in the finally block of the very same try block as you're opening it and executing the query on it.
E.g.
public Entity find(Long id) throws SQLException {
Connection connection = null;
// ...
try {
connection = database.getConnection();
// ...
} finally {
// ...
if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}
return entity;
}
If you have a performance concern regarding this (which is very reasonable as connecting is the most expensive task), then you should be using a connection pool. It also transparently handles this kind of "connection dropped" problems. For example, BoneCP. Please note that also in case of a connection pool, you should still be closing the connections in the finally block as per the above JDBC code idiom. It will namely make them available for reuse.

Categories

Resources