Spring声明式事务配置与使用
1、配置:
<context:component-scan base-package="com.vrvwh.wh01" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="maxActive" value="50" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="3000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x' FROM DUAL" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- mysql 不支持 poolPreparedStatements -->
<!--<property name="poolPreparedStatements" value="true" /> -->
<!--<property name="maxPoolPreparedStatementPerConnectionSize" value="20"
/> -->
<!-- 开启Druid的监控统计功能 -->
<property name="filters" value="stat" />
</bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.vrvwh.wh01.domain</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=${dialect}
hibernate.show_sql=${hibernate.show_sql}
hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
cache.provider_class=${hibernate.cache.provider_class}
cache.use_second_level_cache=${hibernate.cache.use_second_level_cache}
cache.use_query_cache=${hibernate.cache.use_query_cache}
hibernate.jdbc.batch_size=${hibernate.jdbc.batch_size}
</value>
</property>
</bean> <!-- 配置Hibernate事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务异常封装 -->
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<tx:annotation-driven transaction-manager="transactionManager"/>
2、使用
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
public Serializable save(T instance) {
return getCurrentSession().save(instance);
}
注意:
sessionFactory.getCurrentSession() 才能在事务中运行 在单独使用不带任何参数的@Transactional注释时,传播模式要设置为REQUIRED,只读标志设置为false,事务隔离级别设置为READ_COMMITTED,而且事务不会针对受控异常(checked exception)回滚。 事务使用陷进:http://www.ibm.com/developerworks/cn/java/j-ts1.html
Spring声明式事务配置与使用的更多相关文章
- Spring声明式事务配置详解
Spring支持编程式事务管理和声明式的事务管理. 编程式事务管理 将事务管理代码嵌到业务方法中来控制事务的提交和回滚 缺点:必须在每个事务操作业务逻辑中包含额外的事务管理代码 声明式事务管理 一般情 ...
- Spring声明式事务配置
1.首先在/WEB-INF/applicationContext.xml添加以下内容: <!-- 配置事务管理器 --> <bean id="transactionMana ...
- Spring声明式事务配置中propagation各个值的意思
值 含义 REQUIRED 支持当前事务,如果当前没有事务,就新建一个事务. SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行. MANDATORY 支持当前事务,如果当前没有事务 ...
- Spring声明式事务管理与配置详解
转载:http://www.cnblogs.com/hellojava/archive/2012/11/21/2780694.html 1.Spring声明式事务配置的五种方式 前段时间对Spring ...
- Spring声明式事务的配置~~~
/*2011年8月28日 10:03:30 by Rush */ 环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加 ...
- Spring声明式事务管理与配置介绍
转至:http://java.9sssd.com/javafw/art/1215 [摘要]本文介绍Spring声明式事务管理与配置,包括Spring声明式事务配置的五种方式.事务的传播属性(Propa ...
- spring声明式事务管理总结
事务配置 首先在/WEB-INF/applicationContext.xml添加以下内容: <!-- 配置事务管理器 --> <bean id="transactionM ...
- Spring声明式事务配置管理方法
环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add ...
- Spring声明式事务配置管理方法(转)
项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add libra ...
随机推荐
- format when printing
http://msdn.microsoft.com/en-us/library/vstudio/56e442dc.aspx %[flags] [width] [.precision] [{h | l ...
- .net中自定义过滤器对Response内容进行处理
原文:http://www.cnblogs.com/zgqys1980/archive/2008/09/02/1281895.html 代码DEMO:http://files.cnblogs.com/ ...
- jquery实现页面动态切换的方法--toggleClass(className)
$(function() { $(".A").click(function() { $(this).toggleClass("B"); }); }); 当点击带 ...
- IOS-Appium 自动化测试——环境配置及模拟器、真机跑测试
在MAC环境下配置IOS的appium的自动化测试环境,主要包含三个部分: 一.环境配置 1.安装homebrew(homebrew可以提供MAC OS无法提供的很多套件) ruby -e " ...
- 問題排查:行動裝置網頁前端 UI 設計 (2)
之前上網找了個星級評分的範例來玩, 當然這個範例已經用在另一個專案了, 目前看起來沒什麼狀況, 不過在移植到目前的專案之後, 就出現了怪現象: 1. 在大部份時間裡,點擊星星不會有任何反應 2. 即便 ...
- meta http-equiv='refresh' 解读
页面定期刷新,如果加url的,则会重新定向到指定的网页,content后面跟的是时间(单位秒), 把这句话加到指定网页的<head></head>里 一般也用在实时性很强的应用 ...
- 如何断开所有SQL Server所有的连接
方法1: 执行以下脚本 01.declare @sql varchar(100) 02. 03.while 1=1 04.begin 05. 06.select top 1 ...
- 在C代码中调用C++接口
一 在C源文件中调用C++封装的接口 例如: 要想在A.c文件中,调用生命在B.h,实现在B.cpp中的接口bool getMAC(char *mac_addr); 其实现方法 B.cpp 如下: / ...
- delphi XE5下 andriod 广告图片的demo
试了一下,关于好几个广告界面,左右滚动的效果:用TabControl 加上定时器实现即可. 1.TTabControl外观 TTabControl的外观由属性TabPosition控制,可选项包括: ...
- EditText 焦点
让EditText不自动获取焦点,只需要在EditText的父控件加上 android:focusable="true"android:focusableInTouchMode=& ...