`

Spring transaction manager example

 
阅读更多
ApplicationContext.xml

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans	http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
	default-lazy-init="true">

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url"
			value="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=lin.com)(PORT=2000)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=s1.lin.com)))" />
		<property name="username" value="user" />
		<property name="password" value="pwd" />
	</bean>

	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- jdbc template -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg>
			<ref bean="dataSource" />
		</constructor-arg>
	</bean>
	
	<!-- Dao -->
	<bean id="abstractDaoLocator" class="lin.dao.AbstractDaoLocator" abstract="true">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<bean id="exchangeDaoLocator" class="lin.dao.impl.ExchangeDaoLocator" parent="abstractDaoLocator">
	</bean>

	<bean id="testDaoLocator" class="lin.dao.impl.TestDaoLocator" parent="abstractDaoLocator">
	</bean>

	<!-- Transaction Control  -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
		<property name="defaultTimeout" value="15"/>
		<property name="rollbackOnCommitFailure" value="true"/>
	</bean>
	
	<aop:config>
		<aop:advisor pointcut="execution(* lin.DbTest..*(..))" advice-ref="requiresNewTxAdvice" />
	</aop:config>
	
	<tx:advice id="requiresNewTxAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRES_NEW" />
		</tx:attributes>
	</tx:advice>
</beans>

 

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	This XML holds the hibernate configuration information.
	RTP uses annotations, so we do not need to maintain hibernate mapping xml files.
	Please add an entry for every domain object here.
-->
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- Database Settings -->
		<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
		<property name="show_sql">false</property>

		<!-- JDBC Settings -->
		<property name="jdbc.use_streams_for_binary">true</property>
		<property name="max_fetch_depth">1</property>
		
		<property name="hibernate.transaction.auto_close_session">true</property>
		<property name="hibernate.transaction.flush_before_completion">true</property>
		
		<mapping class="lin.domain.Exchange" />
		<mapping class="lin.domain.Test" />
		
	</session-factory>
</hibernate-configuration>

 

分享到:
评论

相关推荐

    spring事务与数据库操作

    &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt; *" propagation="REQUIRED" rollback-for="Exception"/&gt; *" propagation="REQUIRED" rollback-for="Exception"/&gt; *" propagation=...

    Spring-JDBC整合-MySQL8、java8版本

    &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt; *" propagation="REQUIRED"/&gt; ``` 以上配置表明,所有在com.example.service包及其子包下的方法都将被包含在默认的REQUIRED事务传播...

    spring-boot-reference.pdf

    10.2.6. Quick-start Spring CLI Example 10.3. Upgrading from an Earlier Version of Spring Boot 11. Developing Your First Spring Boot Application 11.1. Creating the POM 11.2. Adding Classpath ...

    Spring进阶:Spring的CRUD

    &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt; *" propagation="REQUIRED"/&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource....

    Spring整合Mybatis源代码

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; ``` 总的来说,Spring与...

    Spring 最全约束配置文件

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; ``` 3. **启用组件扫描...

    springmvc+spring+mybatis

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; ``` 7. **mybatis-config.xml文件**:配置MyBatis的基本参数。 ```xml &lt;!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config...

    spring配置事务五种方式.doc

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; ``` 这样,带有@Transactional注解的类或方法将会自动地被Spring事务代理处理。 3. **编程式事务管理** 编程式事务管理允许在代码中显式地...

    java8+tomcat8+struts2.5+spring4.3+hibernate5.2框架搭建详细过程

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; ``` **2. dispatcher-servlet.xml**: - 配置Spring MVC的相关组件。 - 示例配置: ```xml &lt;beans xmlns="http://www.springframework...

    spring-mybatis-spring-2.0.4.zip

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; &lt;context:component-scan base-package="com.example"/&gt; ``` 最后,我们可以在服务类中通过@Autowired注解注入Mapper接口,直接调用其方法执行...

    Spring集成MyBatis简单demo

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; ``` 在项目中创建Mapper接口,例如`UserMapper.java`,并定义对应的方法: ```java package com.example.demo.mapper; import org.apache....

    Spring2.0的配置

    &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt; *" propagation="REQUIRED"/&gt; *" propagation="REQUIRED" rollback-for="java.lang.RuntimeException"/&gt; *" propagation="REQUIRED" ...

    Spring基于XML方式配置事务

    这些可以在`&lt;tx:advice&gt;`标签的`transaction-manager`属性中指定。 3. **创建事务通知**: 事务通知定义了哪些方法应该在事务内执行。使用`&lt;tx:method&gt;`标签,可以指定匹配的方法名、传播行为、读写性质、隔离级别...

    spring中事物配置

    &lt;tx:advice id="transactionAdvice" transaction-manager="transactionManager"&gt; *" propagation="REQUIRED"/&gt; ``` 这里,`*`表示所有方法都将在`REQUIRED`事务传播策略下运行,即如果当前没有事务,则...

    spring整合hibernate实现事务处理

    &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt; *" propagation="REQUIRED"/&gt; (* com.example.service.UserService.*(..))"/&gt; ``` 这段配置表示,对于`UserService`类中...

    Spring 3.1配置文件示例(备忘)

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; ``` 最后,资源加载是 Spring...

    spring ibatis 配置(包括事务管理)

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; ``` 8. **使用示例**:在业务逻辑类中,我们可以通过@Autowired注解注入SqlSessionTemplate或Mapper接口,然后在方法中调用相应的方法进行...

    spring学习笔记(十六)-声明式事务的例子

    &lt;tx:advice id="transactionAdvice" transaction-manager="transactionManager"&gt; *" propagation="REQUIRED"/&gt; (* com.example.service.*.*(..))"/&gt; ``` 这里,所有的`com.example.service`包下的方法都...

    spring applicationContext.xml详细配置

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; ``` 5. **拦截器(Interceptor)**: 拦截器用于在方法调用前后执行特定逻辑,如权限检查、日志记录等。在Spring MVC中,我们通常通过`...

Global site tag (gtag.js) - Google Analytics