Skip to content

Instantly share code, notes, and snippets.

@doubledouble
Created July 25, 2012 17:00
Show Gist options
  • Save doubledouble/3177263 to your computer and use it in GitHub Desktop.
Save doubledouble/3177263 to your computer and use it in GitHub Desktop.
package com.iteye.common.test;
import junit.framework.TestCase;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
public abstract class AbstractTestBean extends TestCase {
protected ApplicationContext applicationContext;
private SessionFactory sessionFactory;
private Session session;
protected void setUp() throws Exception {
String configFile = "spring/*.xml";
applicationContext = new ClassPathXmlApplicationContext(configFile);
sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.NEVER);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
protected void tearDown() throws Exception {
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.releaseSession(session, sessionFactory);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment