0

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

2
  • I formatted the code for you - next time please use the {} button in the editor, or simply indent your code by 4 spaces :-) Commented Jan 4, 2011 at 10:45
  • Thanks, Peter.. I did try the {} a few times, but still was not being nice on the first bit of code. Commented Jan 4, 2011 at 11:02

1 Answer 1

1

There won't be any problems if you do remove synchronized in fact your Application might just perform a little better.

Depending on the your hibernate configuration. Hibernate will request a connection from the pool for either the life of the session or for the life of the transaction.

Sign up to request clarification or add additional context in comments.

3 Comments

I had thought not, but knew needed to ask... Thank you Gareth.
oop.. additional.. yes the reason why is so that I can improve the performance, esp at peak times.
but don't take my word for it go and get a copy of the YourKit profiler from yourkit.com . measure first, cut second

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.