testing Hibernate with Spring's EmbeddedDatabaseBuilder issues with SpringSessionContext -
i unit-testing spring framework's embeddeddatabasebuilder datasource , passing hibernate config sessionfactory, using spring 4 , hibernate 4. i not using spring context in way - config use programmatic (no annotations, no xml except hibernate mapping files).
i expected hibernate use default threadlocalsessioncontext , able start , rollback transactions in unit test.
however somehow hibernate has set sessionfactory.currentsessioncontext spring's springsessioncontext , complains whenever try call sessionfactory.getcurrentsession():
hibernateexception: not obtain transaction-synchronized session current thread @ org.springframework.orm.hibernate4.springsessioncontext.currentsession(springsessioncontext.java:134) @ org.hibernate.internal.sessionfactoryimpl.getcurrentsession(sessionfactoryimpl.java:1014) in code below in unit test, have set hibernate.current_session_context_class thread ignored or replaced spring implementation.
public abstract class hibernatetestbase { private static embeddeddatabase datasource; private static sessionfactory sessionfactory; private session session; @beforeclass public static void setupclass() { datasource = new embeddeddatabasebuilder(). settype(embeddeddatabasetype.h2). addscript("file:sqlresources/schema-1.1.sql"). addscript("file:sqlresources/schema-1.2.sql"). build(); configuration configuration = new configuration(); configuration.addresource("hibernate-mappings/hierarchylevel.hbm.xml"); configuration.addresource("hibernate-mappings/hierarchyfilter.hbm.xml"); configuration.addresource("hibernate-mappings/auditlog.hbm.xml"); configuration.setproperty("hibernate.dialect", "org.hibernate.dialect.oracle10gdialect"); configuration.setproperty("hibernate.show_sql", "true"); configuration.setproperty("hibernate.current_session_context_class", "thread"); standardserviceregistrybuilder serviceregistrybuilder = new standardserviceregistrybuilder(); serviceregistrybuilder.applysetting(environment.datasource, datasource); serviceregistrybuilder.applysettings(configuration.getproperties()); standardserviceregistry serviceregistry = serviceregistrybuilder.build(); sessionfactory = configuration.buildsessionfactory(serviceregistry); sessionfactory.opensession(); } @afterclass public static void teardown() { if (sessionfactory != null) { sessionfactory.close(); } if (datasource != null) { datasource.shutdown(); } } @before public final void starttransaction() { session = sessionfactory.getcurrentsession(); session.begintransaction(); } @after public final void rollback() { session.flush(); transaction transaction = session.gettransaction(); transaction.rollback(); } } i forced call sessionfactory.opensession() instead won't able use daos same sessionfactory because call getcurrentsession(). won't able benefit coded data handling functionality.
what can do?
it works ok when setting configuration object using traditional hibernate.cfg.xml using springsessioncontext production this:
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "classpath://org/hibernate/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.oracle10gdialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.springsessioncontext</property> <!-- property name="hibernate.current_session_context_class">thread</property--> <mapping resource="hibernate-mappings/accessright.hbm.xml" /> </session-factory> </hibernate-configuration> and loading in test, overwriting session context this:
configuration configuration = new configuration(); configuration.configure( "hibernate-mappings/hibernate.cfg.xml"); configuration.setproperty("hibernate.current_session_context_class", "thread"); so there going on in background when hibernate sets hibernate.cfg.xml required.
Comments
Post a Comment