事物管理

jdbc事务管理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: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 ">


<!-- 自动扫描与装配bean -->
<context:component-scan base-package="cn.itcast.spring.p_jdbc"></context:component-scan>


<!-- 加载外部的配置文件 -->
<context:property-placeholder location="classpath:cn/itcast/spring/p_jdbc/jdbc.properties"/>


<!-- 一、配置数据库连接池 ComboPooledDataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本的连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="userName" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 一些管理的配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>



<!-- 二、配置JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>



<!-- 三、配置声明式事务管理 -->


<!-- 声明“事务管理器” -->
<bean id="dsTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- ==== 基于注解的方式配置事务 ==== -->
<tx:annotation-driven transaction-manager="dsTransactionManager"/>



<!-- ==== 基于XML的方式配置事务 ==== -->
<!-- 声明“通知”:
对于以save或update或delete开头的方法,使用正常的事务,其他的方法都使用只读的事务
read-only:表示事务是否是只读的,默认为false。
<tx:advice id="transactionManager" transaction-manager="dsTransactionManager">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
-->
<!-- 声明“切面”
<aop:config>
<aop:advisor advice-ref="transactionManager" pointcut="execution(* cn.itcast.spring.p_jdbc.*Dao.*(..))"/>
</aop:config>
-->

</beans>

 

Hibernate事务管理

<?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 ">


<!-- 自动扫描与装配bean -->
<context:component-scan base-package="cn.itcast.spring.q_tx"></context:component-scan>


<!-- 加载外部的配置文件 -->
<context:property-placeholder
location="classpath:cn/itcast/spring/p_jdbc/jdbc.properties" />


<!-- 一、配置数据库连接池 ComboPooledDataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本的连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 一些管理的配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>

 

<!-- 二、配置JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

 

<!-- 三、配置声明式事务管理 -->


<!-- 声明“事务管理器” -->
<bean id="dsTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- ==== 基于注解的方式配置事务 ==== -->
<tx:annotation-driven transaction-manager="dsTransactionManager" />

 


</beans>

 注解设置事物管理

@Service("userDao")
public class UserDao {

@Resource
private SessionFactory sessionFactory;

//开启事物
@Transactional
public void saveUser(User user){

Session session = sessionFactory.getCurrentSession();

session.save(user);
//int i=1/0;
session.save(new User());

}

}

posted on 2015-08-09 16:50  卖肾割阑尾  阅读(122)  评论(0编辑  收藏  举报

导航