网上搜的一篇关于配置两个数据源的spring配置,自己稍加改动成现在的样子,程序可以正常运行,但是我没有看到配置事务的地方,请问我还需要配置事吗?
<?xml version="1.0" encoding="UTF-8"?>   <beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">       <!-- 数据源相同的内容 -->  <bean  class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="parentDataSource">  
   <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
   <property name="username">  
  <value>root</value>  
  </property>  
</bean>      <!-- 数据库commonDataSource -->  <bean parent="parentDataSource" id="commonDataSource">     <property name="url">      <value>jdbc:mysql://localhost:3306/yzdxpt?user=root&amp;password=jk1268&amp;Unicode=true&amp;characterEncoding=utf8&amp;noAccessToProcedureBodies=true</value>   </property>  
 <property name="password">  
<value>jk1268</value>  
</property>  
  </bean>    <!-- 数据库ltDataSource -->  <bean parent="parentDataSource" id="ltDataSource">  
   <property name="url">  
     <value>jdbc:mysql://192.168.1.220:3306/lxgateway?user=root&amp;password=12345678&amp;Unicode=true&amp;characterEncoding=utf8&amp;noAccessToProcedureBodies=true</value>
  </property>  
  <property name="password">  
<value>12345678</value>  
</property>  
 </bean>    <!-- 编写spring 配置文件的配置多数源映射关系 --> 
 <bean class="com.lingxun.util.DynamicDataSource"    id="dataSource">  
   <property name="targetDataSources">  
    <map key-type="java.lang.String">  
      <entry value-ref="commonDataSource" key="12"></entry>  
      <entry value-ref="ltDataSource" key="123"></entry>  
    </map>  
   </property>  
   <property name="defaultTargetDataSource"  ref="commonDataSource"></property>  
 </bean>        <!-- sessionFactory的配置 -->       <bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"  id="sessionFactory">  
   <property name="dataSource">  
     <ref local="dataSource"/> 
   </property>     <!-- 实体映射资源 -->  <property name="mappingDirectoryLocations">
<list>
<value>
classpath:/com/lingxun/hibernate
</value>
<value>
classpath:/com/lingxun/lt/hibernate
</value>
</list>
</property>   <!-- 为sessionFactory配置Hibernate属性 --> <property name="hibernateProperties">  
   <props>  
     <prop key="hibernate.dialect">  
      org.hibernate.dialect.MySQLDialect  
     </prop>  
     <prop key="hibernate.show_sql">true</prop>  
     <prop key="hibernate.connection.autocommit">false</prop>  
     <prop key="hibernate.cache.use_query_cache">false</prop>  
     <prop key="hibernate.max_fetch_depth">2</prop>  
     <prop  key="hibernate.bytecode.use_reflection_optimizer">true</prop>  
</props>  
 </property>  
</bean>  
 </beans> 

解决方案 »

  1.   

    那就配呗 <!-- 加载JTA事务管理器 -->
    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
    init-method="init" destroy-method="close">
    <property name="forceShutdown">
    <value>true</value>
    </property>
    </bean> <!-- 设置事务管理器参数 -->
    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
    <property name="transactionTimeout">
    <value>300</value>
    </property>
    </bean> <!-- 为Spring的JTA事务管理器指定实现 -->
    <bean id="springTransactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager">
    <ref bean="atomikosTransactionManager" />
    </property>
    <property name="userTransaction">
    <ref bean="atomikosUserTransaction" />
    </property>
    <property name="allowCustomIsolationLevels" value="true" />
    </bean> <tx:annotation-driven transaction-manager="springTransactionManager" /> <!-- 指定包含事务@Transactional的代码所在的包路径 -->
    <context:component-scan base-package="xxx.xxx" />给你个spring分布式事务参考一下...
      

  2.   

    还有你要注意要benas中添加上xmlns:tx="http://www.springframework.org/schema/tx"
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
    不然事物是起不到作用的