简体   繁体   中英

Postgres Stored Functions not inserting with c3p0

Basically, I connect and disconnect to the database with these methods defined in the AbstractModel class:

    // close connection
    public void closeConnection(){
        try{
//          if (!rs.isClosed()){
//              rs.close();
//          }
            cstmt.close();
            SingletonConnection.instance();
            DatabaseConnection.closeConnection();
        } catch (SQLException e){
            System.out.println("SQL Exception: ");
            e.printStackTrace();
        }

    }

    // establish connection
    public void createConnection(){
        try {
            SingletonConnection.instance();
            myConnection = DatabaseConnection.establishConnection();
        } catch (SQLException e){
            e.printStackTrace();
        }
    }

I had another problem with a NullPointerException when trying to close the ResultSet rs, but the c3p0 documentation says that ResultSets are closed by default when the connection terminates.

These two methods of course call these two methods in the DatabaseConnection class:

// create connection
public static Connection establishConnection() throws SQLException {
    return datasource.getConnection();
}

// close connection
public static void closeConnection() throws SQLException{
    DataSources.destroy(datasource);
}

So I basically use getConnection() and destroy() methods to open and close connections on the datasource. Is this the right approach when dealing with a c3p0 connection pool? It seems to be working fine for now.

TL;DR but one thing caught me:

NOTE: I have commented our some of the pool settings/parameter because the app quickly ran out of connections, for a reason I do not know.

The reason might be, that the connection is not given back to the pool properly. And since I cannot find any reference to a Connection.commit in your code I think your transactions are still open and never committed. After the termination of the application PostgreSQL will rollback any of those connections and your INSERTs are gone.

So:

  • Double check, that the TXNs are indeed committed.
  • Double check, that the connections are given back to the pool properly.

If that does not help: trim down and cleanup your code considerably . This servers two purposes: First: While doing this YOU might detect errors in your code yourself. Second: If you have a SSCCE example, you can post this and someone might try to understand it in a blink. This is not possible with your current code.

I got it to work, but am not exactly sure how or why it does. This line of code seems to be the issue:

myConnection.setAutoCommit(false);

It is from a method in a Model. Commenting out this line on methods that are supposed to modify the data in a table(s) seems to have done the trick. Problem is, some methods that insert data seem to work just fine with it while others don't. I'm not sure why.

Is there a simple example, other than the documentation, that explain c3p0 implementation in a simple manner? I've been looking for one but haven't found anything.

I will keep you updated as soon as investigate this matter further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM