欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP 中spring事务配置详解

程序员文章站 2023-08-13 21:50:45
jsp 中spring事务配置详解 前几天被问到,如何防止服务器宕机,造成的数据操作的不完全。 问了一下同事,是事务。哎,恍然大悟,迷糊一时了。 声明式的事务配置,这...

jsp 中spring事务配置详解

前几天被问到,如何防止服务器宕机,造成的数据操作的不完全。

问了一下同事,是事务。哎,恍然大悟,迷糊一时了。

声明式的事务配置,这个是最推荐的,配置到service层。

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  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.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
  
  <!-- 使用annotation @repository,@service自动注册bean, 并保证@required、@autowired的属性被注入的包范围 -->
  <context:component-scan base-package="com.rd,com.rongdu"/>
  <context:annotation-config/>
  

  <bean id="datasource" class="com.jolbox.bonecp.bonecpdatasource" destroy-method="close">
    <!-- connection info -->
    <property name="driverclass" value="com.mysql.jdbc.driver"/>
    <property name="jdbcurl" value="jdbc:mysql://localhost:3306/sfd?useunicode=true&characterencoding=utf8"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>

    <!-- 检查数据库连接池中空闲连接的间隔时间 -->
    <property name="idleconnectiontestperiod" value="4" />
    <!-- 连接池中未使用的链接最大存活时间 -->
    <property name="idlemaxage" value="240" />
    <!-- 设置每个分区含有connection最大个数 -->
    <property name="maxconnectionsperpartition" value="20" />
    <!-- 设置每个分区含有connection最小个数 -->
    <property name="minconnectionsperpartition" value="10" />
    <!-- 设置每个分区数 -->
    <property name="partitioncount" value="3" />
    <!-- 设置分区中的connection增长数量 -->
    <property name="acquireincrement" value="5" />
    <property name="statementscachesize" value="50"/>
     <property name="releasehelperthreads" value="3"/>
  </bean>

  <bean id="jdbctemplate" class="org.springframework.jdbc.core.jdbctemplate">
    <property name="datasource">
      <ref bean="datasource" />
    </property>
  </bean>
  <bean id="namedparameterjdbctemplate" class="org.springframework.jdbc.core.namedparam.namedparameterjdbctemplate">
    <constructor-arg index="0" ref="datasource"/> 
  </bean>

  <bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
     <property name="datasource" ref="datasource"/>
  </bean>
  
  
  <tx:advice id="txadvice" transaction-manager="txmanager">
    <tx:attributes>
      <tx:method name="add*" propagation="required" />
      <tx:method name="delete*" propagation="required" />
      <tx:method name="update*" propagation="required" />
      <tx:method name="*" propagation="required" />

    </tx:attributes>
  </tx:advice>
  
  <aop:config>
    <aop:pointcut id="allmanagermethod"
      expression="execution(* com.test.service.*.*(..))" />
    <aop:advisor advice-ref="txadvice" pointcut-ref="allmanagermethod" />
  </aop:config>
</beans>

其中,有个通配符是有点问题的。事务在于更新数据时候使用,查询不需要事务。所以直接用* ,这样太过于暴力了。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!