Spring事务的管理分为声明式和编程式,声明式就是在Spring配置文件中统一声明,而编程式就是使用@Transactional对方法或类进行注解.在项目开发过各中为对事物进行更灵活的控制,我们理所当然的认为在Service声明开启事物,然后在Dao层对只读方法声明为只读和将事务挂起,这样即使在Service有的查询方法中插入更新或者删除操作时也可以进行很好的事务控制,但是我们发现在service层和dao层同时使用事务时会出现问题.下面分别对声明式事务管理和编程式事务管理分别进行说明.
首先贴出其基本的测试代码:
Model:
package gd.hz.shopping.model; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; /** * 商品类别实体类 * 实现Serializable为以后做分布提供扩展 * @author Administrator * */ @Entity public class ProductType { private static final long serialVersionUID = 1L; /** * 类别ID */ private int id ; /** * 用户名称 */ private String name ; /** * 备注,用于搜索引擎 */ private String note ; /** * 是否可见 */ private boolean visible = true ; /** * 父商品,可以没有 */ private ProductType parent ; /** * 子类商品,可以没有 */ private Set<ProductType> childType = new HashSet<ProductType>() ; @Id @GeneratedValue @Column(name="productId") public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(nullable=false , length=32) public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(length=128) public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Column(nullable=false) public boolean isVisible() { return visible; } public void setVisible(boolean visible) { this.visible = visible; } @ManyToOne(cascade={CascadeType.REFRESH} , fetch=FetchType.LAZY) @JoinColumn(name="parentId") public ProductType getParent() { return parent; } public void setParent(ProductType parent) { this.parent = parent; } @OneToMany(mappedBy="parent" , cascade={CascadeType.REFRESH , CascadeType.REMOVE}) public Set<ProductType> getChildType() { return childType; } public void setChildType(Set<ProductType> childType) { this.childType = childType; } }
这里我们要注意,我们的实体有一个外键(parent)是指向自己的,并且声明为懒加载.
Dao接口:
package gd.hz.shopping.dao; public interface ProductTypeDao { public <T> T select(Class<T> entityClass, Object entityId) ; public void insert(Object entity) ; public <T> void delete(Class<T> entityClass , Object entityId) ; }
Dao接口实现:
package gd.hz.shopping.dao.impl; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; import org.springframework.transaction.support.TransactionSynchronizationManager; import gd.hz.shopping.dao.ProductTypeDao; /** * 商品类别DAO层 * @author Administrator * */ @Repository("productTypeDao") public class ProductTypeDaoImpl implements ProductTypeDao { /** * 使用EntityManager管理器 */ protected EntityManager entityManager ; @PersistenceContext protected void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } /** * 查询实体 */ @Override public <T> T select(Class<T> entityClass, Object entityId) { return entityManager.find(entityClass , entityId) ; } /** * 插入实体 */ @Override public void insert(Object entity) { entityManager.persist(entity) ; } /** * 更新实体 */ @Override public <T> void delete(Class<T> entityClass, Object entityId) { entityManager.remove(entityManager.getReference(entityClass, entityId)) ; } }
Service接口:
package gd.hz.shopping.service; public interface ProductTypeService { public <T> T find(Class<T> entityClass, Object entityId) ; public void save(Object entity) ; public <T> void remove(Class<T> entityClass , Object entityId) ; }
Service接口实现:
package gd.hz.shopping.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.support.TransactionSynchronizationManager; import gd.hz.shopping.dao.ProductTypeDao; import gd.hz.shopping.service.ProductTypeService; /** * 产品类别Service层 * @author Administrator * */ @Service("productTypeService") public class ProductTypeServiceImpl implements ProductTypeService { private ProductTypeDao productTypeDao = null ; /** * 注入产品类别Dao类 * @param productTypeDao */ @Resource(name="productTypeDao") public void setProductTypeDao(ProductTypeDao productTypeDao) { this.productTypeDao = productTypeDao; } @Override public <T> T find(Class<T> entityClass, Object entityId) { return productTypeDao.select(entityClass, entityId) ; } @Override public void save(Object entity) { productTypeDao.insert(entity); } @Override public <T> void remove(Class<T> entityClass, Object entityId) { productTypeDao.delete(entityClass, entityId) ; } }
我的测试的环境是基于Spring+JPA+struts进行的,其基本配置可以看我之前写的文章<<java Spring-3.2.0+Struts-2.3.4+JPA2.0整合>>,首先看一下声明式事务管理:
我们在Spring配置文件中同时对service层和dao层声明事务.其部分配置如下:
<aop:config> <aop:pointcut id="bussinessService" expression="execution(public * gd.hz.shopping.service.*.*(..))" /> <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" propagation="NOT_SUPPORTED" /> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="remove*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="bussinessDao" expression="execution(public * gd.hz.shopping.dao.*.*(..))" /> <aop:advisor pointcut-ref="bussinessDao" advice-ref="txdao" /> </aop:config> <tx:advice id="txdao" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="select*" read-only="true" propagation="NOT_SUPPORTED" /> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> </tx:attributes> </tx:advice>
这样我们就在Service层开启了事物,然后在Dao层对只读方法声明没有事务和只读.我们是这样认为的.
然后我们使用struts2写一个控制类,查询一个ID为52的实体,这个实体的父类ID为31(名称为篮球31):
package gd.hz.shopping.action; import javax.annotation.Resource; import gd.hz.shopping.model.ProductType; import gd.hz.shopping.service.ProductTypeService; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; /** * 产品类别控制器 * @author Administrator * */ @Controller("productTypeAction") @Scope("prototype") public class ProductTypeAction extends ActionSupport { private static final long serialVersionUID = 1L; private ProductTypeService productTypeService = null ; private ProductType productType = null ; public ProductType getProductType() { return productType; } public void setProductType(ProductType productType) { this.productType = productType; } @Resource(name="productTypeService") public void setProductTypeService(ProductTypeService productTypeService) { this.productTypeService = productTypeService; } /** * 返回产品类型列表 * @return */ public String productType() { productType = productTypeService.find(ProductType.class, 52); return "productType" ; } }
新建一个JSP文件Test.jsp(查询其父类名称):
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 测试session关闭:${productType.parent.name } </body> </html>
配置我们的struts2文件:
<package name="center" namespace="/admin/center" extends="struts-default"> <action name="productTypeAction" class="productTypeAction"> <result name="productType">/Test.jsp</result> </action> </package>
输入URL后进行测试,没问题,可以正常显示.Spring提供的一个TransactionSynchronizationManager类,这个类有两个静态方法.getCurrentTransactionName()和isCurrentTransactionReadOnly(),它们的用处分别是判断当前上下文是否有事务,有就返回名称,没有返回空.而isCurrentTransactionReadOnly返回当前事务是否只读.
把这两个方法另到我们的查询中.
Dao层:
public <T> T select(Class<T> entityClass, Object entityId) { System.out.println("Dao--> 是否使用事务: " + TransactionSynchronizationManager.getCurrentTransactionName()); System.out.println("Dao--> 是否只读: " + TransactionSynchronizationManager.isCurrentTransactionReadOnly()); return entityManager.find(entityClass , entityId) ; }
Service层:
相关推荐
Spring+Struts2+JPA入门小例子
分布式宠物商店(EJB3+JPA+Struts2) 宠物商店(petstore)是个比较经典的demo案例, 以宠物商店充分演示EJB3与Java Web程序,Java图形界面程序的结合 主要模块:会员模块 宠物类别模块 宠物模块 购物模块
spring+hibernate+jpa+struts1+struts2+springmvc+jquery+freemaker 学习笔记 Compass将lucene、Spring、Hibernate三者结合
在学习jpa时候做的一个struts2+spring+jpa整合的完整例子 包含分页,一个简单的 资产入库系统 并实现了登陆等。
使用Struts2.2.3+Spring3.0.5+JPA2.0框架案例。 包含包。使用eclipse导入,直接可以运行。编码使用GBK。 出现乱码的话,把默认编码改为GBK就OK了。
Spring+JPA+Struts2三个框架的整合,用mysql数据库
基于_Struts_2+Spring+JPA_框架的WEB_应用
权限管理代码(struts2+spring2.5+jpa)权限管理代码(struts2+spring2.5+jpa)权限管理代码(struts2+spring2.5+jpa)权限管理代码(struts2+spring2.5+jpa)权限管理代码(struts2+spring2.5+jpa)权限管理代码...
struts2+jpa+spring的详细整合,PPT上面有详细解说
本系统是学生信息管理系统,主要演示JPA在实际项目中的使用,采用的是servlet+JSP+JPA.是EJB JPA 这本书里的例子。
关于jsf+ejb+jpa整合,光盘上代码这样写的。
该项目是基于SpringBoot + Vue +JPA 的实验室助理信息管理系统-可用于大学生日常课设 功能点描述: (1)学生 学生注册为系统用户、系统用户报名助理岗位、 (2)实验室技术员 筛选报名信息、实验室助理招聘计划的...
Struts2+Spring+JPA整合配置实例,及增删改查实例 (jar包放在另外一个文件中,超过15M了,请从新下载)
struts2+jpa+spring2.5配置基础框架,直接运行即可
Struts2+spring3.0+JPA(注解方式集成) 步骤详细 推荐下载
JPA+Spring+Struts整合实例,JPA+Spring+Struts整合实例
struts2+spring2+jpa+ajax的组合运用。
oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA)
关于spring+Struts2+jpa集成 小列子
基于JSF+EJB3+JPA的竞价拍卖系统,服务器为JBOSS