Define create Session and SessionFactory



Q) How to create Session and SessionFactory in Hibernate?
A)

Step 1> Put Hibernate properties in the classpath.

Step 2> Put .hbm.xml in class path ?

Code is Here to create session …

package com.dao;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
*
* @author Satya Das
*/
public class HibernateUtil {
protected static final Logger
logger=Logger.getLogger(HibernateUtil.class);
public static String appHome = “No”;

private static SessionFactory sessionFactory;

private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction=new ThreadLocal();

/**
* Initialize Hibernate Configuration
*/

public static void initMonitor(){
logger.info(“Hibernate configure”);
try {
logger.info(“appHome”+appHome);
String path_properties =
appHome+File.separatorChar+”hibernate.properties”;
String path_mapping = appHome+File.separatorChar+”mapping_classes.mysql.hbm.xml”;
//String ecache = appHome+File.separatorChar+”ehcache.xml”;

Properties propHibernate = new Properties();
propHibernate.load(new FileInputStream(path_properties));

Configuration configuration = new Configuration();
configuration.addFile(path_mapping);
configuration.setProperties(propHibernate);

/* try {
CacheManager.create(ecache);
} catch (CacheException e) {
// logger.logError(e);
}*/

sessionFactory = configuration.buildSessionFactory();

} catch (Throwable ex) {
logger.error(“Exception in initMonitor”,ex);
throw new ExceptionInInitializerError(ex);
}
}
/**
* @return a Session Factory Object
*/

public static SessionFactory getSessionFactory() {
logger.info(“Inside getSessionFactory method”);
try {
if (sessionFactory == null) {
initMonitor();
}else {
//sessionFactory.getStatistics().logSummary();
}
} catch (Exception e) {
logger.error(“Exception in getSessionFactory”,e);
}
return sessionFactory;
}

/**
* @return Session . Start a Session
*/

public static Session getSession() {
Session s = (Session) threadSession.get();
logger.debug(“session”+s);
if (s == null) {
s = getSessionFactory().openSession();
threadSession.set(s);
logger.debug(“session 1 $”+s);
}
return s;
}

/**
* Close Session
*/

public static void closeSession(){
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen()) {
s.flush();
s.close();
}
}

/**
* Start a new database transaction.
*/

public static void beginTransaction(){
Transaction tx = null;
if (tx == null) {
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
}

/**
* Commit the database transaction.
*/

public static void commitTransaction(){
Transaction tx = (Transaction) threadTransaction.get();
try {
if ( tx != null ) {
tx.commit();
}
threadTransaction.set(null);
} catch (HibernateException ex) {
rollbackTransaction();
throw ex;
}
}

/**
* Rollback the database transaction.
*/

public static void rollbackTransaction(){
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
tx.rollback();
}
} finally {
closeSession();
}
}
}

Random Posts

  • Creating a Text file using Java
  • JSF Important Interview Questions
  • Collection FrameWork
  • Hibernate Interview Questions
  • JMS INTERVIEW QUESTIONS

Post a Comment