码迷,mamicode.com
首页 > 编程语言 > 详细

Spring声明式事务配置与使用

时间:2015-02-25 18:31:01      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

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() 才能在事务中运行

Spring声明式事务配置与使用

标签:

原文地址:http://www.cnblogs.com/tyb1222/p/4299895.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!