Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

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

I implemented derby data base in my application to save my data,and I am getting this exception in retrieval of result set form select query in Derby database. To establish the connection I use connection string as:

I establish connection using this method:

I declare this method in class Connection.java:

 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 and create connection, create connection during intraction using string:

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

I am getting exception in execution of query:

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

during processing and interaction with Derby database we might update as well get the data from the database:

I perform setAutoCommit as false before inserting in the database

objConnection.setAutoCommit(false);

after insertion:

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

I am getting Exception as

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Found this via Google.

I had the same problem and found my 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();

Works perfectly now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

2.1m questions

2.1m answers

60 comments

56.8k users

...