Link to home
Start Free TrialLog in
Avatar of Omer-Pitou
Omer-Pitou

asked on

Hibernate: Error message: You can't operation on a Closed Connection

Hi sirs,
I changed my hibernate configuration to use a 3rd party connection pool (CP03) by adding the lines below, and remove the connection.pool_size property.
<property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>

Since this change, I am getting this error message. I wonder if someone encountered this issue.
java.sql.SQLException: You can't operate on a closed Connection!!!
      at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
      at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:77)
      at com.mchange.v2.c3p0.impl.NewProxyConnection.prepareStatement(NewProxyConnection.java:171)
      at com.finger.dao.MagasinDAO.getSearchPrepStmt(MagasinDAO.java:187)
      at com.finger.controllers.MouvemTransferArtController.lambda$initFormListeners$2(MouvemTransferArtController.java:198)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
      at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
      at java.awt.Component.processMouseEvent(Component.java:6535)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
      at java.awt.Component.processEvent(Component.java:6300)
      at java.awt.Container.processEvent(Container.java:2236)
      at java.awt.Component.dispatchEventImpl(Component.java:4891)
      at java.awt.Container.dispatchEventImpl(Container.java:2294)
      at java.awt.Component.dispatchEvent(Component.java:4713)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
      at java.awt.Container.dispatchEventImpl(Container.java:2280)
      at java.awt.Window.dispatchEventImpl(Window.java:2750)
      at java.awt.Component.dispatchEvent(Component.java:4713)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
      at java.awt.EventQueue.access$500(EventQueue.java:97)
      at java.awt.EventQueue$3.run(EventQueue.java:709)
      at java.awt.EventQueue$3.run(EventQueue.java:703)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
      at java.awt.EventQueue$4.run(EventQueue.java:731)
      at java.awt.EventQueue$4.run(EventQueue.java:729)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109)
      at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:184)
      at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:229)
      at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:227)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:227)
      at java.awt.Dialog.show(Dialog.java:1084)
      at java.awt.Component.show(Component.java:1673)
      at java.awt.Component.setVisible(Component.java:1625)
      at java.awt.Window.setVisible(Window.java:1014)
      at java.awt.Dialog.setVisible(Dialog.java:1005)
      at com.finger.utils.FinanceLaunchApplication.lambda$main$13(FinanceLaunchApplication.java:36)
      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
      at java.awt.EventQueue.access$500(EventQueue.java:97)
      at java.awt.EventQueue$3.run(EventQueue.java:709)
      at java.awt.EventQueue$3.run(EventQueue.java:703)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.NullPointerException
      at com.mchange.v2.c3p0.impl.NewProxyConnection.prepareStatement(NewProxyConnection.java:135)
      ... 61 more

NOTE: I am using Hibernate in a Java SE application

Best regards
ASKER CERTIFIED SOLUTION
Avatar of Am P
Am P
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Omer-Pitou
Omer-Pitou

ASKER

HI
Thanks for your prompt reply.
I changed my config to this one below, but still getting the same message. I wonder what I am missing
 <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
      <property name="connection.autoReconnect"> true</property>
      <property name="connection.autoReconnectForPools">true</property>
      <property name="connection.is-connection-validation-required">true</property>
   
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/finger_erp?useSSL=false</property>
    <property name="connection.username">root</property>
    <property name="connection.password">finger4</property>
   
    <property name="hibernate.connection.CharSet">utf8</property>
    <property name="hibernate.connection.characterEncoding">utf8</property>
    <property name="hibernate.connection.useUnicode">true</property>

    <!-- JDBC connection pool settings ... using built-in test pool -->
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>
   
    <!-- configuration pool via c3p0-->
     
      <property name="c3p0.preferredTestQuery">select 1;</property>
   
    <!-- Select our SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
   
    <!-- Echo the SQL to stdout -->
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
   
    <!-- Set the current session context -->
    <property name="current_session_context_class">thread</property>
Hi,
This is the reason why I keep getting this error message "You can't work on a closed connection".
In my hibernate util I added a method to return a Connection, that I use for reports, special call and prepared statement, etc... in order to avoid re-writing all the code relating to the session.doWork or session.doReturningWork.
Here is my code
public static Connection getConnection() {
        if (connection == null) {
            try(Session session = getSessionFactory().openSession())  {
                session.beginTransaction();
                  connection = session.doReturningWork((Connection cnctn) -> {
                    return cnctn;
                });
                session.getTransaction().commit();
               
            } catch (HibernateException e) {
                MyExceptionUtil.handle(null, e);
                return null;
            }
        }
        return connection;
    }

I tracked down the issue by checking if the connection returned was closed. What is strange is the cnctn.isClosed comes false, while the test connection.isClosed comes true. What makes it change state?
This structure works fine when using the built in connection pool.

Please assist.
I fix my problem by borrowing the connection from session.doWork, and do all the necessary tasks inside the session.doWork or session.doReturningWork, and returning a List<?> where necessary