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_s ize">5</pr operty>
<property name="hibernate.c3p0.max_s ize">20</p roperty>
<property name="hibernate.c3p0.timeo ut">300</p roperty>
<property name="hibernate.c3p0.max_s tatements" >50</prope rty>
<property name="hibernate.c3p0.idle_ test_perio d">3000</p roperty>
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.SqlUtil s.toSQLExc eption(Sql Utils.java :118)
at com.mchange.v2.sql.SqlUtil s.toSQLExc eption(Sql Utils.java :77)
at com.mchange.v2.c3p0.impl.N ewProxyCon nection.pr epareState ment(NewPr oxyConnect ion.java:1 71)
at com.finger.dao.MagasinDAO. getSearchP repStmt(Ma gasinDAO.j ava:187)
at com.finger.controllers.Mou vemTransfe rArtContro ller.lambd a$initForm Listeners$ 2(MouvemTr ansferArtC ontroller. java:198)
at javax.swing.AbstractButton .fireActio nPerformed (AbstractB utton.java :2022)
at javax.swing.AbstractButton $Handler.a ctionPerfo rmed(Abstr actButton. java:2348)
at javax.swing.DefaultButtonM odel.fireA ctionPerfo rmed(Defau ltButtonMo del.java:4 02)
at javax.swing.DefaultButtonM odel.setPr essed(Defa ultButtonM odel.java: 259)
at javax.swing.plaf.basic.Bas icButtonLi stener.mou seReleased (BasicButt onListener .java:252)
at java.awt.AWTEventMulticast er.mouseRe leased(AWT EventMulti caster.jav a:289)
at java.awt.Component.process MouseEvent (Component .java:6535 )
at javax.swing.JComponent.pro cessMouseE vent(JComp onent.java :3324)
at java.awt.Component.process Event(Comp onent.java :6300)
at java.awt.Container.process Event(Cont ainer.java :2236)
at java.awt.Component.dispatc hEventImpl (Component .java:4891 )
at java.awt.Container.dispatc hEventImpl (Container .java:2294 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:4713)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Contai ner.java:4 888)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Contain er.java:45 25)
at java.awt.LightweightDispat cher.dispa tchEvent(C ontainer.j ava:4466)
at java.awt.Container.dispatc hEventImpl (Container .java:2280 )
at java.awt.Window.dispatchEv entImpl(Wi ndow.java: 2750)
at java.awt.Component.dispatc hEvent(Com ponent.jav a:4713)
at java.awt.EventQueue.dispat chEventImp l(EventQue ue.java:75 8)
at java.awt.EventQueue.access $500(Event Queue.java :97)
at java.awt.EventQueue$3.run( EventQueue .java:709)
at java.awt.EventQueue$3.run( EventQueue .java:703)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.security.ProtectionDo main$JavaS ecurityAcc essImpl.do Intersecti onPrivileg e(Protecti onDomain.j ava:76)
at java.security.ProtectionDo main$JavaS ecurityAcc essImpl.do Intersecti onPrivileg e(Protecti onDomain.j ava:86)
at java.awt.EventQueue$4.run( EventQueue .java:731)
at java.awt.EventQueue$4.run( EventQueue .java:729)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.security.ProtectionDo main$JavaS ecurityAcc essImpl.do Intersecti onPrivileg e(Protecti onDomain.j ava:76)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:728)
at java.awt.EventDispatchThre ad.pumpOne EventForFi lters(Even tDispatchT hread.java :201)
at java.awt.EventDispatchThre ad.pumpEve ntsForFilt er(EventDi spatchThre ad.java:11 6)
at java.awt.EventDispatchThre ad.pumpEve ntsForFilt er(EventDi spatchThre ad.java:10 9)
at java.awt.WaitDispatchSuppo rt$2.run(W aitDispatc hSupport.j ava:184)
at java.awt.WaitDispatchSuppo rt$4.run(W aitDispatc hSupport.j ava:229)
at java.awt.WaitDispatchSuppo rt$4.run(W aitDispatc hSupport.j ava:227)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.awt.WaitDispatchSuppo rt.enter(W aitDispatc hSupport.j ava:227)
at java.awt.Dialog.show(Dialo g.java:108 4)
at java.awt.Component.show(Co mponent.ja va:1673)
at java.awt.Component.setVisi ble(Compon ent.java:1 625)
at java.awt.Window.setVisible (Window.ja va:1014)
at java.awt.Dialog.setVisible (Dialog.ja va:1005)
at com.finger.utils.FinanceLa unchApplic ation.lamb da$main$13 (FinanceLa unchApplic ation.java :36)
at java.awt.event.InvocationE vent.dispa tch(Invoca tionEvent. java:311)
at java.awt.EventQueue.dispat chEventImp l(EventQue ue.java:75 6)
at java.awt.EventQueue.access $500(Event Queue.java :97)
at java.awt.EventQueue$3.run( EventQueue .java:709)
at java.awt.EventQueue$3.run( EventQueue .java:703)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.security.ProtectionDo main$JavaS ecurityAcc essImpl.do Intersecti onPrivileg e(Protecti onDomain.j ava:76)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:726)
at java.awt.EventDispatchThre ad.pumpOne EventForFi lters(Even tDispatchT hread.java :201)
at java.awt.EventDispatchThre ad.pumpEve ntsForFilt er(EventDi spatchThre ad.java:11 6)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hread.java :105)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 01)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:9 3)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:82)
Caused by: java.lang.NullPointerExcep tion
at com.mchange.v2.c3p0.impl.N ewProxyCon nection.pr epareState ment(NewPr oxyConnect ion.java:1 35)
... 61 more
NOTE: I am using Hibernate in a Java SE application
Best regards
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_s
<property name="hibernate.c3p0.max_s
<property name="hibernate.c3p0.timeo
<property name="hibernate.c3p0.max_s
<property name="hibernate.c3p0.idle_
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.SqlUtil
at com.mchange.v2.sql.SqlUtil
at com.mchange.v2.c3p0.impl.N
at com.finger.dao.MagasinDAO.
at com.finger.controllers.Mou
at javax.swing.AbstractButton
at javax.swing.AbstractButton
at javax.swing.DefaultButtonM
at javax.swing.DefaultButtonM
at javax.swing.plaf.basic.Bas
at java.awt.AWTEventMulticast
at java.awt.Component.process
at javax.swing.JComponent.pro
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
at java.awt.Window.dispatchEv
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventQueue.access
at java.awt.EventQueue$3.run(
at java.awt.EventQueue$3.run(
at java.security.AccessContro
at java.security.ProtectionDo
at java.security.ProtectionDo
at java.awt.EventQueue$4.run(
at java.awt.EventQueue$4.run(
at java.security.AccessContro
at java.security.ProtectionDo
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.WaitDispatchSuppo
at java.awt.WaitDispatchSuppo
at java.awt.WaitDispatchSuppo
at java.security.AccessContro
at java.awt.WaitDispatchSuppo
at java.awt.Dialog.show(Dialo
at java.awt.Component.show(Co
at java.awt.Component.setVisi
at java.awt.Window.setVisible
at java.awt.Dialog.setVisible
at com.finger.utils.FinanceLa
at java.awt.event.InvocationE
at java.awt.EventQueue.dispat
at java.awt.EventQueue.access
at java.awt.EventQueue$3.run(
at java.awt.EventQueue$3.run(
at java.security.AccessContro
at java.security.ProtectionDo
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
Caused by: java.lang.NullPointerExcep
at com.mchange.v2.c3p0.impl.N
... 61 more
NOTE: I am using Hibernate in a Java SE application
Best regards
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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().openSe ssion()) {
session.beginTransaction() ;
connection = session.doReturningWork((C onnection cnctn) -> {
return cnctn;
});
session.getTransaction().c ommit();
} catch (HibernateException e) {
MyExceptionUtil.handle(nul l, 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.
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().openSe
session.beginTransaction()
connection = session.doReturningWork((C
return cnctn;
});
session.getTransaction().c
} catch (HibernateException e) {
MyExceptionUtil.handle(nul
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.
ASKER
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
ASKER
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_
<property name="connection.autoRecon
<property name="connection.autoRecon
<property name="connection.is-connec
<property name="connection.driver_cl
<property name="connection.url">jdbc
<property name="connection.username"
<property name="connection.password"
<property name="hibernate.connection
<property name="hibernate.connection
<property name="hibernate.connection
<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="hibernate.c3p0.min_s
<property name="hibernate.c3p0.max_s
<property name="hibernate.c3p0.timeo
<property name="hibernate.c3p0.max_s
<property name="hibernate.c3p0.idle_
<!-- configuration pool via c3p0-->
<property name="c3p0.preferredTestQu
<!-- Select our SQL dialect -->
<property name="dialect">org.hiberna
<!-- Echo the SQL to stdout -->
<property name="show_sql">true</prop
<property name="format_sql">true</pr
<!-- Set the current session context -->
<property name="current_session_cont