zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    B树与B+详解
    处理器拦截器(HandlerInterceptor)详解(转)
    过滤器(Filter)与拦截器(Interceptor )区别
    Redis和MemCache静态Map做缓存区别
    Ubuntu16.10下mysql5.7的安装及远程访问配置
    windows中mysql5.7保存emoji表情
    基于Quartz.NET 实现可中断的任务(转)
    Ubuntu16.10下使用VSCode开发.netcore
    ubuntu16.10 安装ibus中文输入法
    ubuntu 中安装mysql 使用默认用户密码登录
  • 原文地址:https://www.cnblogs.com/tyb1222/p/4299895.html
Copyright © 2011-2022 走看看