Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should the "connection" argument of doWork() be closed?

I am using a C3P0 Connection Pool with Hibernate to execute some JDBC operations. However, I am getting a "Closed Connection" (SQL Error: 17008, SQLState: null) error after some time of usage.

I am using the org.hibernate.jdbc.Work interface to perform my operations:

public class ClassThatDoesWork implements Work {

    @Override
    public void execute(final Connection connection)
            throws SQLException {

        doSomeWork();
        //should connection be closed here?
    }
}

My question is: should the connection object passed as an argument to the execute() method be closed at the end of that method or Hibernate takes care for that automatically?

EDIT These are the Hibernate and c3p0 parameters used:

hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.pool_size=10
hibernate.dialect=org.hibernate.dialect.Oracle9iDialect
hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
hibernate.show_sql=false
acquireIncrement=3
acquireRetryDelay=500
acquireRetryAttempts=5
breakAfterAcquireFailure=false
checkoutTimeout=0
connectionTesterClassName=com.mchange.v2.impl.DefaultConnectionTester
debugUnreturnedConnectionStackTraces=false
dataSourceName=irrelevantDB
identityToken=irrelevantDB
idleConnectionTestPeriod=0
initialPoolSize=3
maxConnectionAge=0
maxIdleTime=7200
maxIdleTimeExcessConnections=0
maxPoolSize=20
maxStatements=50
maxStatementsPerConnection=0
minPoolSize=5
numHelperThreads=3
propertyCycle=0
testConnectionOnCheckin=false
testConnectionOnCheckout=true
unreturnedConnectionTimeout=0
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=10
hibernate.c3p0.max_statements=50
like image 396
nekojsi Avatar asked May 03 '12 14:05

nekojsi


People also ask

What happens if Hibernate Session is not closed?

When you don't close your Hibernate sessions and therefore do not release JDBC connections, you have what is typically called Connection leak. So, after a number of requests (depending on the size of your connection pool) the server will not be able to acquire a connection to respond your request.

What is flush method Why do we need it?

Flushing is the process of synchronizing the state of the persistence context with the underlying database. The EntityManager and the Hibernate Session expose a set of methods, through which the application developer can change the persistent state of an entity.

What is Hibernate Session and how do you get it?

The Session interface is the main tool used to communicate with Hibernate. It provides an API enabling us to create, read, update, and delete persistent objects. The session has a simple lifecycle.

Which of the following Hibernate instances can not be reused in an application?

HibernateException: HHH000469: The ClassLoaderService can not be reused.


1 Answers

The database connection is passed in as a method argument by Hibernate, and thus should not be tempered with (e.g. closed) inside the method -- this is Hibernate's responsibility.

like image 176
01es Avatar answered Oct 04 '22 02:10

01es