Announcement Announcement Module
Collapse
No announcement yet.
Problems with JBoss5, Hibernate and JTA Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with JBoss5, Hibernate and JTA

    I have to implement cluster support for an existing application/product running on Tomcat. Because I also needed second-level cache support, I decided to switch to JBoss and use the TreeCacheProvider.
    So, I have to switch to JTATransactionManager instead of HibernateTransactionManager.

    If trying to load my ServerConfiguration (ServerConfigurationDaoImpl.load()), Hibernate throughs following Exception: "createCriteria is not valid without active transaction"

    Does anyone have an idea which part of my configuration is wrong?

    Code:
    	<context:annotation-config/>
    	<context:component-scan base-package="com.company.product"/>
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    				
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost/db" />
    		<property name="username" value="root" />
    		<property name="password" value="" />
    	</bean>
    	
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="annotatedClasses">
    			<list>
    				...
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    				<prop key="hibernate.hbm2ddl.auto">update</prop>
    				<prop key="hibernate.show.sql">true</prop> 
    				<prop key="hibernate.current_session_context_class">thread</prop>
    				<prop key="hibernate.transaction.flush_before_completion">true</prop>
    				<prop key="hibernate.transaction.auto_close_session">true</prop>  
    				<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
    				<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</prop>
    			</props>
    		</property>
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    	</bean> 
    
    	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    		<property name="transactionManagerName" value="java:/TransactionManager"></property>
    	</bean>
    Code:
    @Repository
    @Transactional(readOnly = true)
    public class ServerConfigurationDaoImpl implements ServerConfigurationDao {
    
    	@Autowired@Qualifier("sessionFactory")
    	private SessionFactory sessionFactory;
    	
    	public ServerConfiguration load() {
    		ServerConfiguration config;
    		try {
    			config = (ServerConfiguration)sessionFactory.getCurrentSession().createCriteria(ServerConfiguration.class).uniqueResult();
    		} catch (HibernateException e) {
    			throw SessionFactoryUtils.convertHibernateAccessException(e);
    		}
    		return config;
    	}
    
    	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    	public ServerConfiguration save(ServerConfiguration config) {
    		try {
    			sessionFactory.getCurrentSession().saveOrUpdate(config);
    		} catch (HibernateException e) {
    			throw SessionFactoryUtils.convertHibernateAccessException(e);
    		}
    		return config;
    	}
Working...
X