I have the following code:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Configuration cfg = new Configuration();
cfg=cfg.configure();
sessionFactory = cfg.buildSessionFactory();
} catch (Throwable ex) {
System.out.println("** Initial SessionFactory creation failed: " + ex.getMessage());
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal hibernateSession = new ThreadLocal();
public static Session currentSession() {
Session s = (Session) hibernateSession.get();
if (s == null) {
s = sessionFactory.openSession();
//System.out.println("sessionFactory.openSession()");
hibernateSession.set(s);
}
return s;
}
public static void closeSession() {
Session s = (Session) hibernateSession.get();
if (s != null)
s.close();
hibernateSession.set(null);
}
}
Also
public class Db {
public static synchronized void Insert(Object updateBean) {
org.hibernate.Session hibernateSession = HibernateUtil.currentSession();
try {
Transaction tx = hibernateSession.beginTransaction();
hibernateSession.save(updateBean);
tx.commit();
} catch(Exception e) {
System.out.println("*hibernate insert Exception: "+e.getMessage());
}
HibernateUtil.closeSession();
}
}
Hibernate is using c3p0 pooling. The Db.Insert is then called from JSP pages in a busy server production environment.
My question is, if I removed the 'synchronized' on Db.Insert(Object) Would this cause issues?
I realise it's something I should most likely know already, but don't and don't want to try it out and get errors.
Also if it's likely to cause said issues, then I'm not sure if I understand the point of using c3p0 for connection pooling... my understanding at the moment is calling HibernateUtil.currentSession(); or hibernateSession.beginTransaction(); calls an available connection from the c3p0 pool and never the twain shall meet.
Sorry for the first bit of code not being 'code' displayed, this webform just does not want to work correctly.
Thanks for reading
{}button in the editor, or simply indent your code by 4 spaces :-)