`
wu_quanyin
  • 浏览: 204700 次
  • 性别: Icon_minigender_1
  • 来自: 福建省
社区版块
存档分类
最新评论

Spring---多个数据库的事务操作配置(JNDI)

阅读更多

一,当有记录需要操作两个以上数据库时,这时就会引发事务问题,,,jndi能解决此类问题

context.xml

<Context path="/spring-all" docBase="spring-all" debug="5" reloadable="true"
	crossContext="true">

	<Resource name="jdbc/mysql" auth="Container"
		type="javax.sql.DataSource" maxActive="100" maxIdle="50"
		maxWait="10000" 
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://127.0.0.1:3306/orange1"
		username="root"
		password="kgddxsksk"
		testOnBorrow="true" testWhileIdle="true"/>	
		
		<Resource name="jdbc/sqlserver" auth="Container"
		type="javax.sql.DataSource" maxActive="100" maxIdle="50"
		maxWait="10000" 
		driverClassName="net.sourceforge.jtds.jdbc.Driver"
		url="jdbc:jtds:sqlserver://192.168.7.83:1433/test"
		username="sa"
		password="kgddxsksk"
		testOnBorrow="true" testWhileIdle="true"/>	
</Context>

 

二,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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/jee 
     http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

	<!-- 
		<bean id="dataSource1"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
		<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
		<value>jdbc:mysql://127.0.0.1:3306/orange1</value>
		</property>
		<property name="username">
		<value>root</value>
		</property>
		<property name="password">
		<value>kgddxsksk</value>
		</property>
		</bean>
		
	-->
	<jee:jndi-lookup id="dataSource1"
		jndi-name="java:/comp/env/jdbc/mysql">
	</jee:jndi-lookup>
	
	<jee:jndi-lookup id="dataSource2"
		jndi-name="java:/comp/env/jdbc/sqlserver">
	</jee:jndi-lookup>
	
	<bean id="jotm"
		class="com.spring.web.controller.JotmFactoryBean" />
	<!-- 
		当使用jta时,说明使用隔离级别
	-->
	<bean id="txManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="allowCustomIsolationLevels" value="true" />
		<property name="userTransaction">
			<ref local="jotm" />
		</property>
	</bean>

	<!-- "person" table in mysql and "user" table in sqlserver -->
	<bean id="person" class="com.spring.web.model.Person"></bean>
	<bean id="personDao" class="com.spring.web.dao.PersonDao">
		<property name="dataSource" ref="dataSource1"></property>
	</bean>
	<bean id="user" class="com.spring.web.model.User"></bean>
	<bean id="userDao" class="com.spring.web.dao.UserDao">
		<property name="dataSource" ref="dataSource2"></property>
	</bean>

	<bean id="userService"
		class="com.spring.web.services.UserService">
		<property name="userDao" ref="userDao"></property>
		<property name="personDao" ref="personDao"></property>
	</bean>

	<!-- 事务配置 -->
	<!-- 非检查型(RuntimeException)默认才会回滚 -->
	<!-- 
		对txManager进行了事务控制,,,所以在代码中使用持久化api时,也要是定制的,如
		JdbcTemplate、HibernateTemplate和JdoTemplate,,,也可用相应的
		DataSourceUtils(针对JDBC),SessionFactoryUtils(针对Hibernate),PersistenceManagerFactoryUtils(针对JDO)
		因为这些类都会引用manager中的事务
	-->
	<tx:annotation-driven transaction-manager="txManager" />
	<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<!-- the transactional semantics... -->
		<tx:attributes >
			<!-- propagation  只有在嵌套事务(service调用了service里面的方法)存在时,几种机制才有意义 -->
			<tx:method name="insert*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
		</tx:attributes>
	</tx:advice>

	<!-- ensure that the above transactional advice runs for any execution
		of an operation defined by the Service interface -->
	<aop:config>
		<aop:pointcut id="daoOperation"
			expression="execution (* com.spring.web.services.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperation" />
	</aop:config>


</beans>

 

 

JotmFactoryBean在新版本中已实移除,需自己加入才可以.

public class JotmFactoryBean implements FactoryBean, DisposableBean {

	private Current jotmCurrent;

	private Jotm jotm;


	public JotmFactoryBean() throws NamingException {
		// Check for already active JOTM instance.
		this.jotmCurrent = Current.getCurrent();

		// If none found, create new local JOTM instance.
		if (this.jotmCurrent == null) {
			// Only for use within the current Spring context:
			// local, not bound to registry.
			this.jotm = new Jotm(true, false);
			this.jotmCurrent = Current.getCurrent();
		}
	}

	/**
	 * Set the default transaction timeout for the JOTM instance.
	 * <p>Should only be called for a local JOTM instance,
	 * not when accessing an existing (shared) JOTM instance.
	 */
	public void setDefaultTimeout(int defaultTimeout) {
		this.jotmCurrent.setDefaultTimeout(defaultTimeout);
	}

	/**
	 * Return the JOTM instance created by this factory bean, if any.
	 * Will be <code>null</code> if an already active JOTM instance is used.
	 * <p>Application code should never need to access this.
	 */
	public Jotm getJotm() {
		return this.jotm;
	}


	public Object getObject() {
		return this.jotmCurrent;
	}

	public Class getObjectType() {
		return this.jotmCurrent.getClass();
	}

	public boolean isSingleton() {
		return true;
	}


	/**
	 * Stop the local JOTM instance, if created by this FactoryBean.
	 */
	public void destroy() {
		if (this.jotm != null) {
			this.jotm.stop();
		}
	}

}

 还得加入jotm中相应的一些包..http://androidguy.blog.51cto.com/974126/216454

carol.jar

carol-iiop-delegate.jar

carol-interceptors.jar

howl.jar

jotm.jar

xapool.jar

 

0
1
分享到:
评论

相关推荐

    dynamic-datasource-spring-boot-starter-v3.5.1.zip

    支持每个数据库独立初始化表结构schema和数据库database。 支持无数据源启动,支持懒加载数据源(需要的时候再创建连接)。 支持 自定义注解 ,需继承DS(3.2.0+)。 提供并简化对Druid,HikariCp,BeeCp,Dbcp2的快速...

    dynamic-datasource-spring-boot-starter-v3.5.1.tar.gz

    支持每个数据库独立初始化表结构schema和数据库database。 支持无数据源启动,支持懒加载数据源(需要的时候再创建连接)。 支持 自定义注解 ,需继承DS(3.2.0+)。 提供并简化对Druid,HikariCp,BeeCp,Dbcp2的快速...

    dynamic-datasource-spring-boot-starter:springboot的动态数据源多数据源动态数据源主从分离读写分离分布式事务https:dynamic-datasource.com

    支持每个数据库独立初始化表结构schema和数据库数据库。支持自定义注解,需继承DS(3.2.0+)。提供对德鲁伊,Mybatis-Plus,P6sy,Jndi的快速集成。简化Druid和HikariCp配置,提供变量参数配置。配置一次,并行通用...

    Spring-Reference_zh_CN(Spring中文参考手册)

    处理多个持久化单元 12.6.2. JpaTemplate 和 JpaDaoSupport 12.6.3. 基于原生的JPA实现DAO 12.6.4. 异常转化 12.6.5. 事务管理 12.6.6. JpaDialect III. Web 13. Web框架 13.1. 介绍 13.1.1. 与其他web框架的集成 ...

    springboot参考指南

    连接到一个JNDI数据库 ii. 28.2. 使用JdbcTemplate iii. 28.3. JPA和Spring Data i. 28.3.1. 实体类 ii. 28.3.2. Spring Data JPA仓库 iii. 28.3.3. 创建和删除JPA数据库 viii. 29. 使用NoSQL技术 i. 29.1. Redis i...

    Spring.3.x企业应用开发实战(完整版).part2

    4.7 整合多个配置文件 4.8 Bean作用域 4.8.1 singleton作用域 4.8.2 prototype作用域 4.8.3 Web应用环境相关的Bean作用域 4.8.4 作用域依赖问题 4.9 FactoryBean 4.10 基于注解的配置 4.10.1 使用注解定义Bean ...

    spring in action英文版

     8.3.5 在一个控制器中处理多个动作  8.3.6 使用一次性控制器  8.4 视图解析  8.4.1 使用模板视图  8.4.2 解析视图Bean  8.4.3 选择视图解析器  8.5 使用Spring的绑定标签  8.6 处理异常  ...

    Spring in Action(第2版)中文版

    6.1.2理解spring对事务管理的支持 6.2选择事务管理器 6.2.1jdbc事务 6.2.2hibernate事务 6.2.3jpa事务 6.2.4jdo事务 6.2.5jta事务 6.3在spring中编写事务 6.4声明式事务 6.4.1定义事务参数 6.4.2代理事务 ...

    Spring in Action(第二版 中文高清版).part2

    6.1.2 理解Spring对事务管理的支持 6.2 选择事务管理器 6.2.1 JDBC事务 6.2.2 Hibernate事务 6.2.3 JPA事务 6.2.4 JDO事务 6.2.5 JTA事务 6.3 在Spring中编写事务 6.4 声明式事务 6.4.1 定义事务参数 ...

    Spring in Action(第二版 中文高清版).part1

    6.1.2 理解Spring对事务管理的支持 6.2 选择事务管理器 6.2.1 JDBC事务 6.2.2 Hibernate事务 6.2.3 JPA事务 6.2.4 JDO事务 6.2.5 JTA事务 6.3 在Spring中编写事务 6.4 声明式事务 6.4.1 定义事务参数 ...

    spring4.3.2参考文档(英文)

    Spring 上下文:Spring 上下文是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。 Spring AOP:通过配置管理特性,Spring AOP 模块...

    Spring面试题

    组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下: ☆ 核心容器:核心容器提供 Spring 框架的基本功能。核心容器的主要组件是 BeanFactory,它是工厂...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    一共四个,其中pdf 三个包,源码一个包 第一章 J2EE快速入门 1.1 J2EE概述 1.1.1 J2EE的来源 1.1.2 J2EE整体框架 1.1.3 从J2EE到JavaEE 1.2 J2EE组件 1.2.1 客户端组件 1.2.2 Web组件 1.2.3 业务逻辑组件 1.3 J2EE...

    Spring3.x企业应用开发实战(完整版) part1

    4.7 整合多个配置文件 4.8 Bean作用域 4.8.1 singleton作用域 4.8.2 prototype作用域 4.8.3 Web应用环境相关的Bean作用域 4.8.4 作用域依赖问题 4.9 FactoryBean 4.10 基于注解的配置 4.10.1 使用注解定义Bean ...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (3)

    一共四个,其中pdf 三个包,源码一个包 第一章 J2EE快速入门 1.1 J2EE概述 1.1.1 J2EE的来源 1.1.2 J2EE整体框架 1.1.3 从J2EE到JavaEE 1.2 J2EE组件 1.2.1 客户端组件 1.2.2 Web组件 1.2.3 业务逻辑组件 1.3 J2EE...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (2)

    一共四个,其中pdf 三个包,源码一个包 第一章 J2EE快速入门 1.1 J2EE概述 1.1.1 J2EE的来源 1.1.2 J2EE整体框架 1.1.3 从J2EE到JavaEE 1.2 J2EE组件 1.2.1 客户端组件 1.2.2 Web组件 1.2.3 业务逻辑组件 1.3 J2EE...

    J2EE应用开发详解

    325 18.4.2 配置数据库连接池 327 18.5 HQL简介 328 18.6 小结 330 第19章 权限管理系统(Struts+Spring+Hiberante+Ajax) 331 19.1 需求分析 331 19.2 系统总体流程设计 331 19.3 系统设计 332 19.4 系统总体解析 ...

    Grails 中文参考手册

    2.2 创建一个Grails应用 2.3 Hello World示例 2.4 使用IDE 2.5 规约配置 2.6 运行Grails应用 2.7 测试Grails应用 2.8 部署Grails应用 2.9 所支持的Java EE容器 2.10 创建工件 2.11 生成Grails应用 3. 配置 3.1 基本...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    10.4.2 将实体映射到多个表 394 10.4.3 映射复合类型的属性 396 10.4.4 映射实体类的主键 398 10.5 关联关系映射 402 10.5.1 单向N-1关联 403 10.5.2 单向1-1关联 406 10.5.3 单向1-N关联 409 10.5.4 单向N-N...

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    由于J2EE的开源的框架中提供了MVC模式实现框架Struts、对象关系模型中的Hibernate 的框架及拥有事务管理和依赖注入的Spring。利用现存框架可以更快开发系统。所以选择Java技术作为blog 的开发工具。 为了增加系统的...

Global site tag (gtag.js) - Google Analytics