SQLNonTransientConnectionException: No current connection in my application while interacting with Derby database

I applied the derby database in my application to save my data, and I get this exception in the search for the select result query form in the Derby database. To establish a connection, I use the connection string as:

I establish a connection using this method:

I declare this method in the Connection.java class:

  public synchronized Connection createConnection() throws Exception { Connection rescon = null; try { if (this.dbuser == null) { rescon = DriverManager.getConnection(this.URI + ";create=true"); } else { rescon = DriverManager.getConnection(this.URI + ";create=true", this.dbuser, this.dbpass); } // new connection in connection pool created } catch (SQLException e) { final String stackNum = Utility.exceptionHandler(e); throw new Exception("Exception get during Connection - " + e.getMessage()); } return rescon; } 

I used this method as I create the connection, create the connection during the overlay using the line:

 private Connection objConnection = null; objConnectionpool = new Connection("jdbc:derby:" + Folder.getAbsolutePath() + "/myapplication" + sFileName, null, null, "org.apache.derby.jdbc.EmbeddedDriver"); objConnection = objConnectionpool.createNewConnection(); 

I get an exception in the execution of the request:

 sQuery = "select value,reason from " + TableNm + " where fileName ='" + FileName + "' AND type='"+Type+"' AND status ='remaining'"; 

during processing and interaction with the Derby database, we can update and get data from the database:

I execute setAutoCommit as false before inserting into the database

 objConnection.setAutoCommit(false); 

after insertion:

 ps = objConnection.prepareStatement(sQuery); rs = ps.executeQuery(); objConnection.commit(); objConnection.setAutoCommit(true); 

I get an exception like

 java.sql.SQLNonTransientConnectionException: No current connection. 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.noCurrentConnection(Unknown Source) at org.apache.derby.impl.jdbc.EmbedConnection.checkIfClosed(Unknown Source) at org.apache.derby.impl.jdbc.EmbedConnection.setupContextStack(Unknown Source) at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source) at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source) at com.myapplication.c.aw.a(Unknown Source) at com.myapplication.c.aw.a(Unknown Source) at com.myapplication.main.avd.run(Unknown Source) Caused by: java.sql.SQLException: No current connection. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) ... 11 more 
+6
source share
2 answers

This is discovered through Google.

I had the same problem and found the answer in this sample application: http://db.apache.org/derby/docs/10.4/devguide/rdevcsecure26537.html

 // force garbage collection to unload the EmbeddedDriver // so Derby can be restarted System.gc(); 

Now it works fine.

+3
source

What is Connection.createNewConnection ? I have never heard of such a method on java.sql.Connection , and I can not find it in any of the documents.

I always use DriverManager.getConnection to establish my connections to the Derby database.

0
source

Source: https://habr.com/ru/post/889355/


All Articles