Note to self about SessionFactory
I was very pleased about how well the WebService code was working. I had finally gotten the process of changing the WS code as well as the C++ client code nailed down. As I started testing I noticed that JBoss/Hibernate started fail and complain about too many connections.
It turns out there are a lot of people that have had the same issue. The recommended solutions are:
1. Use finally block to make sure sessions are closed.
2. Use a connection pool other then the default. C3P0 is highly recommended.
I went through the code line and by and made sure sessions were being closed. Configuring a new connection pool wasn’t as straight forward as I would have hoped. After starting JBoss I noticed complaints about C3P0 vs Hibernate properties.The solution seems to be to add “hibernate” to the property name as shown below.
Original:
<!– start c3p0 connection properties –>
<property name=”c3p0.min_size”>10</property>
New:
<!– start c3p0 connection properties –>
<property name=”hibernate.c3p0.min_size”>10</property>
Even after doing all this I still had the “too many connections” problem. I reduced the Ws code to a few lines, open a session and close a session. And still I had “too many connections” .
Then the “big dummy” light came on. I was creating a new sessionFactory each time a made a WS call!
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
I moved the sessionfactory creation to the constructor and made sessionFactory a member variable. After re-deploying it works perfect and is much faster.
This is one case where having code reviews or at least a second set of eyes on the code would have helped.




Buy:Levitra.Soma.Super Active ED Pack.Viagra Soft Tabs.Viagra.VPXL.Propecia.Cialis Soft Tabs.Cialis Professional.Viagra Super Active+.Viagra Super Force.Cialis Super Active+.Cialis.Zithromax.Maxaman.Tramadol.Viagra Professional….