JSP 中spring事务配置详解

发布时间 - 2026-01-11 00:55:41    点击率:

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&amp;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>

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

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


# spring事务配置  # JSP  # spring  # 配置  # JSP 获取spring容器中bean的两种方法总结  # JSP开发中在spring mvc项目中实现登录账号单浏览器登录  # Spring boot 跳转到jsp页面的实现方法  # SpringBoot创建JSP登录页面功能实例代码  # java实现图片上加文字水印(SpringMVC + Jsp)  # JSP 获取Spring 注入对象示例  # JSP spring boot / cloud 使用filter防止XSS  # 有个  # 不需要  # 希望能  # 问了  # 前几天  # 不完全  # 谢谢大家  # 问到  # 恍然大悟  # 区中  # 池中  # 中未  # 如何防止  # 连接池  # bean  # Service  # Required  # Autowired  # xsd  # tx 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel如何获取当前用户信息_Laravel Auth门面获取用户ID  Laravel如何使用Spatie Media Library_Laravel图片上传管理与缩略图生成【步骤】  Laravel如何处理CORS跨域问题_Laravel项目CORS配置与解决方案  如何登录建站主机?访问步骤全解析  Laravel如何为API生成Swagger或OpenAPI文档  Laravel如何记录日志_Laravel Logging系统配置与自定义日志通道  Javascript中的事件循环是如何工作的_如何利用Javascript事件循环优化异步代码?  Laravel怎么使用Markdown渲染文档_Laravel将Markdown内容转HTML页面展示【实战】  Laravel怎么实现模型属性转换Casting_Laravel自动将JSON字段转为数组【技巧】  Midjourney怎样加参数调细节_Midjourney参数调整技巧【指南】  Mybatis 中的insertOrUpdate操作  原生JS获取元素集合的子元素宽度实例  如何在VPS电脑上快速搭建网站?  Laravel用户认证怎么做_Laravel Breeze脚手架快速实现登录注册功能  Laravel Octane如何提升性能_使用Laravel Octane加速你的应用  ,怎么在广州志愿者网站注册?  Laravel如何从数据库删除数据_Laravel destroy和delete方法区别  使用spring连接及操作mongodb3.0实例  Microsoft Edge如何解决网页加载问题 Edge浏览器加载问题修复  Laravel如何实现API版本控制_Laravel API版本化路由设计策略  Android利用动画实现背景逐渐变暗  微信小程序制作网站有哪些,微信小程序需要做网站吗?  如何在IIS7上新建站点并设置安全权限?  微信小程序 canvas开发实例及注意事项  php在windows下怎么调试_phpwindows环境调试操作说明【操作】  在线教育网站制作平台,山西立德教育官网?  Laravel N+1查询问题如何解决_Eloquent预加载(Eager Loading)优化数据库查询  Java Adapter 适配器模式(类适配器,对象适配器)优缺点对比  如何用wdcp快速搭建高效网站?  如何用AI帮你把自己的生活经历写成一个有趣的故事?  Laravel怎么实现观察者模式Observer_Laravel模型事件监听与解耦开发【指南】  Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】  公司门户网站制作流程,华为官网怎么做?  简历在线制作网站免费版,如何创建个人简历?  Android滚轮选择时间控件使用详解  Laravel Sail是什么_基于Docker的Laravel本地开发环境Sail入门  Laravel如何设置定时任务(Cron Job)_Laravel调度器与任务计划配置  JavaScript如何实现继承_有哪些常用方法  用yum安装MySQLdb模块的步骤方法  深入理解Android中的xmlns:tools属性  智能起名网站制作软件有哪些,制作logo的软件?  Laravel模型事件有哪些_Laravel Model Event生命周期详解  Laravel如何实现多表关联模型定义_Laravel多对多关系及中间表数据存取【方法】  网页设计与网站制作内容,怎样注册网站?  如何基于云服务器快速搭建个人网站?  Laravel事件和监听器如何实现_Laravel Events & Listeners解耦应用的实战教程  Laravel怎么做缓存_Laravel Cache系统提升应用速度的策略与技巧  如何构建满足综合性能需求的优质建站方案?  MySQL查询结果复制到新表的方法(更新、插入)  Laravel如何将应用部署到生产服务器_Laravel生产环境部署流程