All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.greenpepper.samples.application.phonebook.HibernateSessionService Maven / Gradle / Ivy

There is a newer version: 4.2.4
Show newest version
package com.greenpepper.samples.application.phonebook;

import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;



/**
 * 

HibernateSessionService class.

* * @author oaouattara * @version $Id: $Id */ public class HibernateSessionService implements SessionService { private final ThreadLocal threadTransaction = new ThreadLocal(); private final ThreadLocal threadSession = new ThreadLocal(); private SessionFactory sessionFactory; /** *

Constructor for HibernateSessionService.

* * @param properties a {@link java.util.Properties} object. * @throws org.hibernate.HibernateException if any. */ public HibernateSessionService(Properties properties) throws HibernateException { HibernateDatabase db = new HibernateDatabase(properties); sessionFactory = db.getSessionFactory(); } /** *

startSession.

* * @throws org.hibernate.HibernateException if any. */ public void startSession() throws HibernateException { initSession(); } /** *

getSession.

* * @return a {@link org.hibernate.Session} object. * @throws org.hibernate.HibernateException if any. */ public Session getSession() throws HibernateException { Session s = threadSession.get(); if (s == null) { s = initSession(); } return s; } /** * closeSession * * @throws org.hibernate.HibernateException if any. */ public void closeSession() throws HibernateException { Session s = threadSession.get(); threadSession.set(null); if (s != null) { s.close(); } Transaction tx = threadTransaction.get(); if (tx != null) { threadTransaction.set(null); //log warning todo } } /** *

beginTransaction.

* * @throws org.hibernate.HibernateException if any. */ public void beginTransaction() throws HibernateException { Transaction tx = threadTransaction.get(); if (tx == null) { tx = getSession().beginTransaction(); threadTransaction.set(tx); } } /** *

commitTransaction.

* * @throws org.hibernate.HibernateException if any. */ public void commitTransaction() throws HibernateException { Transaction tx = threadTransaction.get(); threadTransaction.set(null); if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) { tx.commit(); } } /** *

rollbackTransaction.

* * @throws org.hibernate.HibernateException if any. */ public void rollbackTransaction() throws HibernateException { Transaction tx = threadTransaction.get(); try { threadTransaction.set(null); if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) { tx.rollback(); } } finally { closeSession(); } } /** *

close.

* * @throws org.hibernate.HibernateException if any. */ public void close() throws HibernateException { sessionFactory.close(); } private synchronized Session initSession() { Session s = sessionFactory.openSession(); threadSession.set(s); return s; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy