|
|
Spring-Hibernate Connection Pooling::c3p0::Tomcat::MyEclipse
mit(); log.debug(quot;save successfulquot;); } catch (RuntimeException re) { tx.rollback();log.error(quot;save failedquot;, re); throw re; } finally {HibernateSessionFactory.closeSession(); }
}
Whenever I open a session, I close it. My HibernateSessionFactory is as follows:
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = quot;/hibernate.cfg.xmlquot;;
private static final ThreadLocallt;Sessiongt; threadLocal = new ThreadLocallt;Sessiongt;(); private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION;
static { try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println(quot;%%%% Error Creating SessionFactory %%%%quot;);
e.printStackTrace();
} } private HibernateSessionFactory() { }
/** * Returns the ThreadLocal Session instance. Lazy initialize * the lt;codegt;SessionFactorylt;/codegt; if needed. * * @return Session * @throws HibernateException */ public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session; }
/** * Rebuild hibernate session factory * */
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println(quot;%%%% Error Creating SessionFactory %%%%quot;);
e.printStackTrace();
}
}
/** * Close the single hibernate session instance. * * @throws HibernateException */ public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null);
if (session != null) { session.flush();session.close();System.out.println(quot;%%%%% Session is closed %%%%%quot;);/*if(session.isConnected()){System.out.println(quot;%%%%% Session is still connected %%%%% quot;);//session.disconnect();}*/ } }
/** * return session factory * */
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/** * return session factory * *session factory will be rebuilded in the next call */
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/** * return hibernate configuration * */
public static Configuration getConfiguration() {
return configuration;
}
}
PORTION OF applicationContext.xml:
lt;bean id=quot;hibernateSessionquot;
class=quot;org..orm.hibernate3.LocalSes sionFactoryBeanquot;gt;
lt;property name=quot;configLocationquot;
value=quot;classpath:hibernate.cfg.xmlquot;gt;
lt;/propertygt;
lt;/beangt;
lt;bean id=quot;transactionManagerquot; class=quot;org..orm.hibernate3.Hibernat eTransactionManagerquot;gt;
lt;property name=quot;sessionFactoryquot;gt;
lt;ref bean=quot;hibernateSessionquot;/gt;lt;/propertygt;
lt;/beangt; lt;bean id=quot;baseTransactionProxyquot; class=quot;org..transaction.interceptor .TransactionProxyFactoryBeanquot; abstract=quot;truequot;gt; lt;property name=quot;transactionManagerquot; ref=quot;transactionManagerquot;/gt; lt;property name=quot;transactionAttributesquot;gt; lt;propsgt; lt;prop key=quot;insert*quot;gt ROPAGATION_REQUIREDlt;/propgt; lt;prop key=quot;update*quot;gt ROPAGATION_REQUIREDlt;/propgt; lt;prop key=quot;*quot;gt ROPAGATION_REQUIRED,readOnlylt;/propgt; lt;/propsgt; lt;/propertygt;lt;/beangt;
lt;bean id=quot;loginDAOServicequot; parent=quot;baseTransactionProxyquot;gt; lt;property name=quot;targetquot; ref=quot;persistenceLayerquot;/gt;lt;/beangt;
lt;bean id=quot;loginDAOquot; class=quot;com.dionex.hibernateSpring.LoginDAOquot;gt; lt;property name=quot;sessionFactoryquot;gt; lt;ref bean=quot;hibernateSessionquot; /gt; lt;/propertygt; lt;/beangt; lt;bean id=quot;persistenceLayerquot;
class=quot;com.dionex.hibernateSpring.PersistenceLayer quot; abstract=quot;falsequot;
singleton=quot;truequot; lazy-init=quot;defaultquot; autowire=quot;defaultquot;
dependency-check=quot;defaultquot;gt;
lt;property name=quot;loginDAOquot;gt;
lt;ref bean=quot;loginDAOquot; /gt;
lt;/propertygt;
lt;/beangt;
-------------------------hibernate.cfg.xml is:------------
lt;?xml version='1.0' encoding='UTF-8'?gt;
lt;!DOCTYPE hibernate-configuration PUBLIC quot;-//Hibernate/Hibernate Configuration DTD 3.0//ENquot; quot;hibernate-configuration-3.0.dtdquot;gt;
lt;!-- Generated by MyEclipse Hibernate Tools. --gt;
lt;hibernate-configurationgt;
lt;session-factorygt;
lt;property name=quot;connection.usernamequot;gt;***lt;/propertygt;
lt;property name=quot;connection.uclquot;gt;jdbc racle:thin: @****:****:****lt;/propertygt;
lt;property name=quot;dialectquot;gt;org.hibernate.dialect.Oracle9Dialec tlt;/propertygt;
lt;property name=quot;myeclipse.connection.profilequot;gt;thinlt;/propertygt;
lt;property name=quot;connection.passwordquot;gt;*****lt;/propertygt;
lt;property name=quot;connection.driver_classquot;gt;oracle.jdbc.driver. OracleDriverlt;/propertygt;
lt;property name=quot;connection.pool_sizequot;gt;10lt;/propertygt;
lt;property name=quot;cache.provider_classquot;gt;org.hibernate.cache.No CacheProviderlt;/propertygt;
lt;property name=quot;hibernate.transaction.auto_close_sessionquot;gt;tr uelt;/propertygt;
lt;property name=quot;hibernate.transaction.factory_classquot;gt;org.hib ernate.transaction.JDBCTransactionFactorylt;/propertygt;
lt;property name=quot;hibernate.show_sqlquot;gt;truelt;/propertygt;
lt;property name=quot;hibernate.connection.release_modequot;gt;on_closelt;/propertygt;
lt;property name=quot;hibernate.connection.aggressive_releasequot;gt;tru elt;/propertygt;
lt;!-- configuration pool via c3p0--gt;
lt;property name=quot;hibernate.c3p0.acquire_incrementquot;gt;0lt;/propertygt;
lt;!-- Determines how many connections at a time c3p0 will try to acquire when the pool is exhausted. :: default=1 --gt;
lt;property name=quot;hibernate.c3p0.max_sizequot;gt;20lt;/propertygt;
lt;property name=quot;hibernate.c3p0.max_statementsquot;gt;0lt;/propertygt;
lt;property name=quot;hibernate.c3p0.min_sizequot;gt;10lt;/propertygt;
lt;property name=quot;hibernate.c3p0.timeoutquot;gt;60lt;/propertygt; lt;!-- seconds --gt;
lt;!-- The seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.--gt;
lt;mapping resource=quot;com/dionex/hibernateSpring/Login.hbm.xmlquot; /gt;
lt;/session-factorygt;
lt;/hibernate-configurationgt;
---------------c3p0.properties is:----------------
c3p0.preferredTestQuery=SELECT 1 from dual
c3p0.maxConnectionAge=60
c3p0.testConnectionOnCheckout=false
c3p0.acquireRetryDelay=1000
c3p0.acquireRetryAttempts=30
c3p0.breakAfterAcquireFailure=false
c3p0.unreturnedConnectionTimeout=60
c3p0.debugUnreturnedConnectionStackTraces=true
------------------------------------------------------------------
When my application fires 3 queries, the DBA tells that I have 20 processes running... eventually the Database becomes slow and then crashes. I see the
message quot;%%%%% Session is closed %%%%%quot; 3 times in my console.
And few things like:
DEBUG - Closing Hibernate Session [org.hibernate.impl.SessionImpl@1ed00d1] after transaction
DEBUG - Closing Hibernate Session
DEBUG - closing session
DEBUG - processForwardConfig(ForwardConfig[name=successSales,path=/jsp/pages/sales.jsp,redirect=false,contextRelative=false,mod ule=null])
DEBUG - running Session.finalize()
DEBUG - running Session.finalize()
DEBUG - running Session.finalize()
DEBUG - running Session.finalize()
----------------------------------------------------------------------
I am ready to take all the blame on me...if someone can tell me which configuration is wrong...PLEASE HELP A POOR JAVA DEVELOPER ....
THANKS A LOT IN ADVANCE !!!!
I also see c3p0 logs:
DEBUG - Checking for expired resources - Mon May 05 11:34:17 PDT 2008 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@31bcc 8 ---gt; age: 55904 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@82a13 a ---gt; age: 58732 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@12821 5d ---gt; age: 58951 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@14189 d0 ---gt; age: 59029 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@1bd9d 76 ---gt; age: 59107 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@1a998 c7 ---gt; age: 59185 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@179c7 cf ---gt; age: 59263 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@56663 3 ---gt; age: 59341 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37 ---gt; age: 59404 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@8d0b0 f ---gt; age: 59420 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - Checking for expired resources - Mon May 05 11:34:20 PDT 2008 [com.mchange.v2.resourcepool.BasicResourcePool@183d 260]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@1b80d 9b ---gt; age: 58638 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@183d 260]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@1f210 56 ---gt; age: 59076 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@183d 260]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@146ad 8b ---gt; age: 59138 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@183d 260]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@14083 25 ---gt; age: 59232 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@183d 260]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@15386 c2 ---gt; age: 59310 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@183d 260]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@cd51a d ---gt; age: 59388 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@183d 260]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@88364 4 ---gt; age: 59466 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@183d 260]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@16321 e6 ---gt; age: 59544 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@183d 260]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@fd9b9 7 ---gt; age: 59622 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@183d 260]
DEBUG - resource age is okay: com.mchange.v2.c3p0.impl.NewPooledConnection@10924 47 ---gt; age: 59810 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@183d 260]
DEBUG - Checking for expired resources - Mon May 05 11:34:24 PDT 2008 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - EXPIRED resource: com.mchange.v2.c3p0.impl.NewPooledConnection@31bcc 8 ---gt; age: 63404 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - Removing expired resource: com.mchange.v2.c3p0.impl.NewPooledConnection@31bcc 8 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 9, unused: 9, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
*****same 3 lines for****82a13a, 128215d, 14189d0, so on...till
DEBUG - EXPIRED resource: com.mchange.v2.c3p0.impl.NewPooledConnection@8d0b0 f ---gt; age: 66920 max: 60000 [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - Removing expired resource: com.mchange.v2.c3p0.impl.NewPooledConnection@8d0b0 f [com.mchange.v2.resourcepool.BasicResourcePool@159d 87f]
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 0, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 1, unused: 1, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 2, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 3, unused: 3, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 4, unused: 4, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 5, unused: 5, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 6, unused: 6, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 7, unused: 7, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 8, unused: 8, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 9, unused: 9, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
DEBUG - trace com.mchange.v2.resourcepool.BasicResourcePool@159d 87f [managed: 10, unused: 10, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1da27 37)
******* AND REPEATS THE SAME THINGS ie. try to close same expired resource...***********
Hi, one more observation:
Using query:
select username, paddr, process, program, sid from v$session where program not like '%oracle%' group by username, process, paddr, program, sid;
shows that WHEN the web application makes 3 conversations with the Database, 20 new Non-oracle processes are created, where sid is different for each one of them...and quot;JDBC Thin Clientquot; is the program name for each one of them.
Thanks for the help in advance...
hibernate.cf.xml :
-----------------------------------------------------------------------
lt;!-- configuration pool via c3p0--gt;
lt;property name=quot;hibernate.c3p0.acquire_incrementquot;gt;0lt;/propertygt;
lt;!-- Determines how many connections at a time c3p0 will try to acquire when the pool is exhausted. :: default=1 --gt;
lt;property name=quot;hibernate.c3p0.max_sizequot;gt;20lt;/propertygt;
lt;property name=quot;hibernate.c3p0.max_statementsquot;gt;0lt;/propertygt;
lt;property name=quot;hibernate.c3p0.min_sizequot;gt;0lt;/propertygt;
lt;!-- more than 0 created the problem --gt;
lt;property name=quot;hibernate.c3p0.timeoutquot;gt;30lt;/propertygt; lt;!-- seconds --gt;
lt;!-- The seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.--gt;
-----------------------------------------------------------------------
c3p0.properties:
c3p0.preferredTestQuery=SELECT 1 from dual
c3p0.maxConnectionAge=60
c3p0.testConnectionOnCheckout=false
c3p0.acquireRetryDelay=1000
c3p0.acquireRetryAttempts=30
c3p0.breakAfterAcquireFailure=false
c3p0.unreturnedConnectionTimeout=30
c3p0.debugUnreturnedConnectionStackTraces=true
-----------------------------------------------------------------------
This forum is lucky for me...as soon as I get frustrated and post a question...God secretly sends me some idea to fix it ... I love myself (lolz...) |
|