|
|
Session close at correct time?
Hi all,
I am running spring + hibernate with postgres. My app works as I expect except for undeployment.
When I deploy the app a process is running:
tomcat myApp 127.0.0.1(51646) idle
owned by postgres, which I assume is the db connection. However when I undeploy it doesn't go away and when I redeploy a new one turns up such that there is a process per deployment of the app. If I restart tomcat they all go away obviously.
I assume it's because I am not closing the session. I'm not sure what the right way to close the session is (I didn't think I'd need to initially). I am injecting the sessionFactory into all classes that need it, and in each function I call getCurrentSession and call save on the session when I am finished. Due to release_mode being after_statement I assumed that would mean I was good to go.
Any suggestions, thoughts, or recommendations are very much appreciated.
Interesting config is:
context.xml in META-INF:Code:
lt;Resource name=quot;jdbc/myAppquot; auth=quot;Containerquot; type=quot;javax.sql.DataSourcequot; driverClassName=quot;org.postgresql.Driverquot; ucl=quot;jdbc:postgresql--127.0.0.1:5432/myAppquot; username=quot;tomcatquot; password=quot;XXXquot; maxActive=quot;20quot; maxIdle=quot;10quot; maxWait=quot;-1quot;/gt;
web.xmlCode: lt;resource-refgt; lt;res-ref-namegt;jdbc/myApplt;/res-ref-namegt; lt;res-typegt;javax.sql.DataSourcelt;/res-typegt; lt;res-authgt;Containerlt;/res-authgt; lt;/resource-refgt;
beans-persistance.xmlCode: lt;bean id=quot;dataSourcequot; class=quot;org..jndi.JndiObjectFactoryBeanquot;gt; lt;property name=quot;jndiNamequot; value=quot;java:comp/env/jdbc/myAppquot;/gt; lt;/beangt;
lt;bean id=quot;sessionFactoryquot; class=quot;org..orm.hibernate3.annotation.AnnotationSessionFactoryBeanquot;gt; lt;property name=quot;dataSourcequot; ref=quot;dataSourcequot;/gt; lt;property name=quot;packagesToScanquot;gt; lt;listgt; lt;valuegt;org.blim.myApplt;/valuegt; lt;/listgt; lt;/propertygt; lt;property name=quot;annotatedClassesquot;gt; lt;listgt; lt;valuegt;org.blim.myApp.myClasslt;/valuegt; lt;/listgt; lt;/propertygt; lt;property name=quot;schemaUpdatequot; value=quot;truequot; /gt; lt;property name=quot;hibernatePropertiesquot;gt; lt;valuegt; hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect hibernate.show_sql=false hibernate.jdbc.batch_size=50 hibernate.connection.release_mode=after_statement hibernate.auto_commit=false lt;/valuegt; lt;/propertygt; lt;/beangt;A session isn't the same as a database connection!
You are using a connectionpool (Commons DBCP to be exact) and that opens connections to the database so probably connections remain open. Instead of using jndi I suggest configure it inside the applicationcontext. You will need to add a destroy method to the configuration so that connections get cleanedup nicely.
Understood, thanks for the response.
Unfortunately I need to use JNDI eventually for definite. Therefore I think my question simplifies to:
If I am using a JNDI resource and hibernate, what's the proper (or a good) method for closing the connections? Alternatively should hibernate doing this already, and it's something I've misconfigured?
Commons DBCP is the tomcat default. |
|