spring+hibernate+jotm 配置

项目开始的时候用的是Spring+hibernate+struts的架构,没有汲及JDBC编程,新加了一个工作流组件时,由于它是用JDBC编程的。问题就来了,JDBC的事务与spring的事务怎么结合呢。在网上搜了一下,大部分推荐用JTA。而JOTM就是一种实现。闲话少说,配置是关键。

1.配置jotm

<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />

2.配置数据源

<bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
   <property name="dataSource"> 
        <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">   
             <property name="transactionManager" ref="jotm"/>        
             <property name="driverName" value="${driverName}"> </property>
             <property name="url" value="${url}"></property>
             <property name="user" value="${user}" />
              <property name="password" value="${password}" />   
          </bean> 
   </property>
   <property name="user" value="${user}" />
   <property name="password" value="${password}" />   
</bean>

刚开始配置的时候由于StandardXADataSource没有定义用户名和密码总是报错。这里应该加上这两项配置

3.配置Hibernate的sessionFactory的jtaTransactionManager属性

<bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSource"></property>
  
   <property name="hibernateProperties">
    <props>
     <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
     <prop key="hibernate.default_schema">BPUSER</prop>
     <prop key="hibernate.show_sql">true</prop>
     <prop key="hibernate.format_sql">true</prop>
     <prop key="hibernate.use_sql_comments">false</prop>
     <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
    </props>
   </property>
  
   <property name="mappingDirectoryLocations">
    <list>
     <value>classpath:你自己的Hibernate映射文件</value>
        </list>
   </property>
  <property name="jtaTransactionManager" ref="jotm"></property>
</bean>

4.配置事务管理器

<bean id="txManager"      class="org.springframework.transaction.jta.JtaTransactionManager">   
    <property name="userTransaction"   ref="jotm" />  

     <property name="allowCustomIsolationLevels"   ref="true" />   </bean>

上面的配置文件已经完全配置好了,接下来是配置tomcat的JOTM

5.在Context节点下配置

<Resource name="UserTransaction" auth="Container"
     type="javax.transaction.UserTransaction"
     factory = "org.objectweb.jotm.UserTransactionFactory"
     jotm.timeout = "60"/>   
<Transaction factory="org.objectweb.jotm.UserTransactionFactory" jotm.timeout="60"/>

基本上配置完全了,如何使用呢

UserTransaction trans = null;

trans = (UserTransaction) ctx.lookup("java:comp/UserTransaction");

        trans.begin();

//处理逻辑

        trans.commit();       

//事务回滚

          trans.rollback();

到些全部介绍完毕,不过在配置的时候要下载最新的JOTM jar包,下载地址是

http://forge.ow2.org/project/showfiles.php?group_id=19&release_id=3580

你可能感兴趣的:(jotm)