1

We are using spring + hibernate + jsf + mysql + atomikos in our project. Here below the version details:

  • Spring: 3.0.5
  • Hibernate : 3.6.0
  • JSF : 2.0 (myfaces 2.0.12)
  • Mysql: 5.1.38 (InnoDB table structure)
  • Atomikos: 3.8.0
  • Tomcat: 6.0.20

When I use JSF2 ViewScope bean to use transactional service in service layer, it works fine for the first request. It rollsback when there is a problem, and saves the two entities into different table when there is no problem. However, if I do the second request on the same view, then I get null object if I call getSessionFactory() in my DAO.

Any help on this?

Here below are my settings etc:

@ManagedBean
@ViewScoped
public class TestPageBean {
   public String click(){
        testService.saveTest(obj1, obj2);
    } 
}

@Transactional
public class TestService {
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void saveTest(Object1 obj1, Object2 obj2) {
        dao1.save(obj1);
        dao2.save(obj2);
    }
}

GenericDao includes save method:

public void save(Entity entity){
    Session session = getSessionFactory().getCurrentSession().save(entity);
}

Here is xml configuration of Spring:

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="atomikosDataSourceBean" />
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.company.txtest.entity</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.connection.characterEncoding">utf8</prop>
                <prop key="hibernate.transaction.factory_class">com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory</prop>
                  <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
                  <prop key="hibernate.current_session_context_class">jta</prop>
                  <prop key="hibernate.connection.release_mode">auto</prop> 
            </props>
        </property>
    </bean>


  <bean id="atomikosDataSourceBean" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean" init-method="init" destroy-method="close">
        <property name="uniqueResourceName"><value>NonXADBMS</value></property>
        <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
        <property name="url"><value>jdbc:mysql://localhost:3306/txtestdb?autoReconnect=true</value></property>
        <property name="user"><value>username</value></property>
        <property name="password"><value>password</value></property>
        <property name="minPoolSize"><value>1</value></property>
    </bean>

    <!-- Construct Atomikos UserTransactionManager, needed to configure Spring -->
    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"  init-method="init" destroy-method="close">
        <!--  when close is called, should we force transactions to terminate or not? -->
        <property name="forceShutdown"><value>true</value></property>
    </bean>

    <!-- Also use Atomikos UserTransactionImp, needed to configure Spring  --> 
    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout"><value>300</value></property>
    </bean>

    <tx:annotation-driven/>

    <!-- Configure the Spring framework to use JTA transactions from Atomikos -->
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager"><ref bean="atomikosTransactionManager"  /></property>
        <property name="userTransaction"><ref bean="atomikosUserTransaction"  /></property>
    </bean>

    <bean id="genericDaoHibernate" class="com.company.txtest.dao.hibernate.GenericDaoHibernate">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
Jonathan
  • 20,053
  • 6
  • 63
  • 70
huzeyfe
  • 3,554
  • 6
  • 39
  • 49

0 Answers0