doubt with open and close connection

Asked

Viewed 566 times

0

I would like your help with the link below, when I close I have the following error below, am I doing it the right way? regarding the opening and closing of the connection?

my pool

public class MysqlConnectionPool {
private final DataSource dataSource;

public MysqlConnectionPool() {
    MysqlConnectionPoolDataSource pool = new MysqlConnectionPoolDataSource();
    pool.setUrl("jdbc:mysql://*********.sa-east-1.rds.amazonaws.com:3306/*********?autoReconnect=true&useSSL=false");
    pool.setUser("*********");
    pool.setPassword("*********");

    this.dataSource = pool;
}    
public Connection getConnection() throws SQLException {
    Connection connection = dataSource.getConnection();
    return connection;
}}

Example of a DAO I have, all the others follow the same pattern

public class UsuarioDAO {
ExceptionHandling capturaException = new ExceptionHandling();
GeradorURL geradorURL = new GeradorURL();
MysqlConnectionPool mysqlConnectionPool;
private final Connection connection;

public UsuarioDAO() throws SQLException {
    this.connection = new MysqlConnectionPool().getConnection();
}

public List<UsuarioDTO> seleciona() throws SQLException {
    List<UsuarioDTO> results = new ArrayList<>();
    String sql = "select aqui";
    try (PreparedStatement stmt = connection.prepareStatement(sql);
            ResultSet resultSet = stmt.executeQuery()) {

        while (resultSet.next()) {
            results.add(populaConta(resultSet));
        }
        resultSet.close();
        stmt.close();
    } catch (SQLException e) {
        capturaException.ExceptionHandlerController(e);
        throw new RuntimeException(e);
    } finally {
         connection.close();
    }
    return results;
}}

error

02-Jan-2018 15:49:16.027 SEVERE [http-nio-8084-exec-5] org.apache.Catalina.core.Standardwrappervalve.invoke Servlet.service() for Servlet [Dispatcher] in context with path [/Head2head] threw Exception [Request Processing failed; nested Exception is java.lang.Runtimeexception: com.mysql.jdbc.exceptions.jdbc4.Mysqlnontransientconnectionexception: No Operations allowed after Connection closed. ] with root cause com.mysql.jdbc.exceptions.jdbc4.Mysqlnontransientconnectionexception: No Operations allowed after Connection closed.

1 answer

1

You probably closed the connection and then tried to use it. That’s why error ! Closes all connection inside Finally this can help.

  finally {
     connection.close();
     resultSet.close();
     stmt.close();
  }

Where you are calling or using your User class.java?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.