I have a legacy webapp which uses a servlet filter to wrap each request as follows
HibernateUtil.beginTransac
tion();
chain.doFilter(request, response);
(boilerplate code which then calls one of the following...)
HibernateUtil.commitTransa
ction();
or
HibernateUtil.rollbackTran
saction();
HibernateUtil created a sessionFactory according to info in hibernate.cfg.xml as follows (I was using hibernate3 contextual sessions)
<session-factory>
<property name="connection.datasourc
e">java:co
mp/env/jdb
c/filmSour
ce</proper
ty>
<property name="show_sql">true</prop
erty>
<property name="dialect">org.hiberna
te.dialect
.MySQLDial
ect</prope
rty>
<property name="hibernate.cache.prov
ider_class
">org.hibe
rnate.cach
e.EhCacheP
rovider</p
roperty>
<property name="hibernate.current_se
ssion_cont
ext_class"
>thread</p
roperty>
<mapping resource="film/hibernate/C
ommission.
hbm.xml" />
</session-factory>
</hibernate-configuration>
Everything worked fine.
However the app has now been integrated with Spring and I want to configure the Hibernate session in springs applicationContext.xml
Here is what I currently have in there..
<bean id="myDataSource" class="org.springframework
.jndi.Jndi
ObjectFact
oryBean">
<property name="jndiName" value="java:comp/env/jdbc/
filmSource
"/>
</bean>
<bean id="mySessionFactory" class="org.springframework
.orm.hiber
nate3.Loca
lSessionFa
ctoryBean"
>
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>film/hibernate/Comm
ission.hbm
.xml</valu
e>
</list>
</property>
<property name="hibernateProperties"
>
<props>
<prop key="hibernate.dialect">or
g.hibernat
e.dialect.
MySQLDiale
ct</prop>
<prop key="hibernate.cache.provi
der_class"
>org.hiber
nate.cache
.EhCachePr
ovider</pr
op>
<prop key="hibernate.cache.use_s
econd_leve
l_cache">t
rue</prop>
<prop key="hibernate.current_ses
sion_conte
xt_class">
thread</pr
op>
<prop key="hibernate.transaction
.factory_c
lass">org.
hibernate.
transactio
n.JDBCTran
sactionFac
tory</prop
>
<prop key="hibernate.show_sql">t
rue</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework
.orm.hiber
nate3.Hibe
rnateTrans
actionMana
ger">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<bean id="hibernateUtil" class="film.web.framework.
util.Hiber
nateUtil"/
>
and here is my hibernateUtil which has now been modified for Spring.
public class HibernateUtil {
private static Log log = LogFactory.getLog(Hibernat
eUtil.clas
s);
private static SessionFactory sessionFactory;
@Autowired
public void setMySessionFactory(Sessio
nFactory value) {
sessionFactory = value;
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession() {
return getSessionFactory().getCur
rentSessio
n();
}
public static void beginTransaction() {
getSessionFactory().getCur
rentSessio
n().beginT
ransaction
();
}
public static void commitTransaction() {
getSessionFactory().getCur
rentSessio
n().getTra
nsaction()
.commit();
}
public static void rollbackTransaction() {
getSessionFactory().getCur
rentSessio
n().getTra
nsaction()
.rollback(
);
}
}
The problem is when the request filter calls HibernateUtil.beginTransac
tion() I get the following error:
org.hibernate.HibernateExc
eption: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here.