大家好,小弟在项目中需要连接两个数据库,并要求实现全局事务。查阅了一下资料,使用JOTM进行Spring与JTA的集成,但服务器(Tomcat)启动时报错。主要遇到如下两个问题:
1、事务的bean,id必须为transactionManager吗?为什么我指定为其他名字,然后在<tx:advice id="txAdvice" transaction-manager="myTxManager">指定,还是报错,说no bean names 'transactionManager'?
2、即使我将bean的id命名为transactionManager,还是报错,为:Cannot convert value of type [org.springframework.transaction.jta.JtaTransactionManager] to required type [javax.transaction.TransactionManager] for property 'transactionManager': no matching editors or conversion strategy found
3、使用JOTM配置JTA,是不是一定要用XA连接池(StandardXAPoolDataSource),用dbcp或c3p0可以么?
请指教,谢谢~~
下面是我配置的XML:        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc2.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc2.url}</value>
</property>
<property name="username">
<value>${jdbc2.username}</value>
</property>
<property name="password">
<value>${jdbc2.password}</value>
</property>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.max_fetch_depth">1</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.connection.characterEncoding">utf8</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath*:/xx/xxx</value>
</list>
</property>
</bean>
<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.max_fetch_depth">1</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.connection.characterEncoding">utf8</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath*:/xxx/xxxxx</value>
</list>
</property>
</bean> <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />
   <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="userTransaction" ref="jotm" />
   </bean>        <!-- 属性文件读入 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean> <!-- 支持 @Transactional 标记 -->
<tx:annotation-driven /> <!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy /> <!-- 以AspectJ方式 定义 AOP -->
<aop:config proxy-target-class="true">
<aop:advisor
pointcut="execution(* xx.xxx.xxxx..*Manager.*(..))"
advice-ref="txAdvice" />
<aop:advisor
pointcut="execution(* xx.xxx.xxxx.xxxxxxx.dao.*Dao.*(..))"
advice-ref="txAdvice" />
</aop:config> <!-- 基本事务定义 -->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>        <!-- 业务逻辑注入(只写了部分) -->
        <bean id="testDao" class="xxxxxxxxxxxxxxxx.TestDAO">
               <property name="sessionFactory" ref="sessionFactory" />
        </bean>        <bean id="testService" class="xxxxxxxxxxxxxxxx.TestService">
               <property name="testDao" ref="testDao" />
        </bean>

解决方案 »

  1.   

    没用过spring的jta,
    用ejb 就好用
      

  2.   

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
    default-autowire="byName">
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation" value="classpath:hibernate.cfg.xml">
    </property>
    </bean> <!--閰嶇疆Hibernate鐨勪簨鐗╃鐞嗗櫒-->
    <bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <!--bean闇€瑕佷緷璧栨敞鍏ヤ竴涓猄essionFactory bean鐨勫紩鐢?-->
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!--瀹氫箟浜嬬墿閫氱煡锛岄渶瑕佹寚瀹氫竴涓簨鐗╃鐞嗗櫒-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <!--瀹氫箟灞炴€э紝澹版槑浜嬬墿瑙勫垯-->
    <tx:attributes>
    <tx:method name="get*" read-only="true" />
    <tx:method name="find*" read-only="true" propagation="REQUIRED" />
    <tx:method name="search*" read-only="true" />
    <tx:method name="query*" read-only="true" />
    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="regist*" propagation="REQUIRED" />
    <tx:method name="del*" propagation="REQUIRED" />
    <tx:method name="infoinput*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="add*" propagation="REQUIRED" />
    <tx:method name="do*" propagation="REQUIRED" />
    <tx:method name="*" propagation="REQUIRED" read-only="true" />
    </tx:attributes>
    </tx:advice>
    <aop:config>
    <!--瀹氫箟閭d簺鏂规硶搴旂敤杩欎簺瑙勫垯-->
    <aop:pointcut expression="execution(* cn.jzsz.zj.houtai.service.*.*(..))"
    id="serviceMethod" />
    <!--灏嗕簨鐗╅€氱煡涓庡簲鐢ㄨ鍒欑殑鏂规硶缁勫悎-->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
    </aop:config> <!-- 閰嶇疆DAO -->
    <bean id="baomingDao" class="cn.jzsz.zj.houtai.dao.impl.BaomingDaoImpl">
    </bean> <bean id="kaochangDao" class="cn.jzsz.zj.houtai.dao.impl.KaochangDaoImpl">
    </bean>

    <bean id="gangweiDao" class="cn.jzsz.zj.houtai.dao.impl.GangweiDaoImpl">
    </bean>
        
        <bean id="guanliyuanDao"
    class="cn.jzsz.zj.houtai.dao.impl.GuanliyuanDaoImpl">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- 閰嶇疆鏈嶅姟灞?-->
    <bean id="baomingService" class="cn.jzsz.zj.houtai.service.impl.BaomingServiceImpl">
    <property name="baomingDao" ref="baomingDao"></property>
    </bean> <bean id="kaochangService" class="cn.jzsz.zj.houtai.service.impl.KaochangServiceImpl">
    <property name="kaochangDao" ref="kaochangDao"></property>
    </bean>

    <bean id="gangweiService" class="cn.jzsz.zj.houtai.service.impl.GangweiServiceImpl">
    <property name="gangweiDao" ref="gangweiDao"></property>
    </bean>   
         <bean id="guanliyuanService"
    class="cn.jzsz.zj.houtai.service.impl.GuanliyuanServiceImpl">
    <property name="guanliyuanDao" ref="guanliyuanDao" />
    </bean>
    <!-- 鎺у埗灞?(id涓簊truts.xml涓殑class) 浠ヤ笅姣忎釜bean蹇呴』閮借澧炲姞scope="prototype"灞炴€?-->

    <bean id="BaomingAction" class="cn.jzsz.zj.houtai.action.BaomingAction"
    scope="prototype">
    <property name="baomingService" ref="baomingService"></property>
    <!-- <property name="gws" ref="gangweiService"></property>-->
    </bean>

    <bean id="FpkcAction" class="cn.jzsz.zj.houtai.action.FpkcAction" scope="prototype">
     </bean>
      <bean id="UploadAction" class="cn.jzsz.zj.houtai.action.UploadAction" scope="prototype">
     </bean>
     
     <bean id="guanliyuanAction" class="cn.jzsz.zj.houtai.action.GuanliyuanAction" scope="prototype">
    <property name="guanliyuanService" ref="guanliyuanService" />
    </bean>
    <bean id="kaochangAction" class="cn.jzsz.zj.houtai.action.KaochangAction" scope="prototype">
    <property name="kaochangService" ref="kaochangService" />
    </bean>
    <bean id="GangweiAction" class="cn.jzsz.zj.houtai.action.GangweiAction" scope="prototype">

    </bean>

    </beans>我用的是这种,也不知道属于你们说的那一种
      

  3.   

    你的第2条 结合 你的配置,很明显说明你在代码中TransactionManager类是 javax.transaction.TransactionManager类型,与你配置的类型不同