Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.