网上搜的一篇关于配置两个数据源的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&password=jk1268&Unicode=true&characterEncoding=utf8&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&password=12345678&Unicode=true&characterEncoding=utf8&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>
<?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&password=jk1268&Unicode=true&characterEncoding=utf8&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&password=12345678&Unicode=true&characterEncoding=utf8&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>
解决方案 »
- 100分那位朋友做过上传试题,帮帮忙啊。
- JBuiler中的一个小问题
- hql 语句中能写delete update语句吗?
- STRUTS“The requested resource (/addbook.do) is not available.”问题,没办法了,小女只能另开贴,那一帖的分还没得结
- 部署一个ejb的jar只能提供一个ejb类的相关接口吗?
- struts中的session变量的使用!
- 条件查找的一个问题?
- 《网络机器人JAVA编程指南》配套jar包!!!
- 如何在EJB中进行实现事务,急切的等待帮助。
- maven编译报错,求大神
- 如何在不启动tomcat的情况下判断数据库链接是否正确
- jms spring消息监听器处理频率问题
<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分布式事务参考一下...
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
不然事物是起不到作用的