• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > Java事务管理学习之Spring和Hibernate详解

Java事务管理学习之Spring和Hibernate详解

作者:oscar999 字体:[增加 减小] 来源:互联网 时间:2017-05-28

oscar999 通过本文主要向大家介绍了Java事务管理学习之Spring和Hibernate详解等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

环境与版本

本文出来之前的一篇文章中的hibernate的相关lib 外

Java事务管理之Hibernate

还需要加入spring的lib 包和如下的一些依赖包

      org.aopalliance

      org.aspectj

      org.apache.commons

Spring 的版本是Spring 4.1.5。

依赖包也可以到Spring 官方网站下载到 ,名字类似 spring-framework-3.0.2.RELEASE-dependencies

理论知识

Spring和Hibernate整合后,通过Hibernate API进行数据库操作时发现每次都要opensession,close,beginTransaction,commit,这些都是重复的工作,我们可以把事务管理部分交给spring框架完成。

使用spring管理事务后在dao中不再需要调用beginTransaction和commit,也不需要调用session.close() ,使用API  sessionFactory.getCurrentSession()来替代sessionFactory.openSession()

* 如果使用的是本地事务(jdbc事务)

<property name="hibernate.current_session_context_class">thread</property>

* 如果使用的是全局事务(jta事务)

<property name="hibernate.current_session_context_class">jta</property>

Spring中Propagation类的事务属性详解:

     PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

     PROPAGATION_SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。

     PROPAGATION_MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。

     PROPAGATION_REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。

     PROPAGATION_NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

     PROPAGATION_NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。

     PROPAGATION_NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。

Spring 可以使用xml方式进行配置或是使用注解声明的方式进行事务的管理。

xml 方式配置事务代码实例

代码结构如下:

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xmlns:aop="http://www.springframework.org/schema/aop" 
 xsi:schemaLocation=" 
  http://www.springframework.org/schema/util 
  http://www.springframework.org/schema/util/spring-util-3.1.xsd 
  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.1.xsd 
  http://www.springframework.org/schema/mvc 
  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
 
 <context:component-scan base-package="com.oscar999.trans.sprhib" /> 
 <context:property-placeholder location="classpath:/com/oscar999/trans/sprhib/config.properties" /> 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
  destroy-method="close"> 
  <!-- Connection Info --> 
  <property name="driverClassName" value="${jdbc.driver}" /> 
  <property name="url" value="${jdbc.url}" /> 
  <property name="username" value="${jdbc.username}" /> 
  <property name="password" value="${jdbc.password}"></property> 
 </bean> 
 
 <bean id="sessionFactory" 
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
  <property name="dataSource" ref="dataSource"></property> 
  <property name="hibernateProperties"> 
   <props> 
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 
    <prop key="hibernate.hbm2ddl.auto">update</prop> 
    <prop key="hibernate.show_sql">true</prop> 
    <prop key="hibernate.format_sql">true</prop> 
    <prop key="hibernate.jdbc.batch_size">20</prop> 
    <prop key="hibernate.enable_lazy_load_no_trans">true</prop> 
    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> 
    <prop key="jdbc.use_streams_for_binary">true</prop> 
   </props> 
  </property> 
  <property name="packagesToScan"> 
   <list> 
    <value>com.oscar999.trans.sprhib.model</value> 
   </list> 
  </property> 
 </bean> 
 
 <!-- Transaction --> 
 <bean id="transactionManager" 
  class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
  <property name="sessionFactory" ref="sessionFactory" /> 
 </bean> 
 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
  <tx:attributes> 
   <tx:method name="save*" propagation="REQUIRED" /> 
   <tx:method name="*" read-only="true" /> 
  </tx:attributes> 
 </tx:advice> 
 <aop:config proxy-target-class="true"> 
  <aop:pointcut expression="execution(* com.oscar999.trans.sprhib.dao.ProductDAOImpl.*(..))" id="pointcut" /> 
  <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /> 
 </aop:config> 
 
</beans> 
</div>

config.properties

jdbc.driver=oracle.jdbc.driver.OracleDriver 
jdbc.url=jdbc:oracle:thin:@12.6.18.43:1521:orcl 
jdbc.username= 
jdbc.password=oracle 
</div>

Product.Java

/** 
 * @Title: Product.java 
 * @Package com.oscar999.trans.hibernate 
 * @Description: 
 * @author XM 
 * @date Feb 15, 2017 1:44:47 PM 
 * @version V1.0 
 */ 
package com.oscar999.trans.sprhib.model; 
 
import java.io.Serializable; 
 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 
 
/** 
 * @author XM 
 * 
 */ 
@Entity 
@Table(name = "TEST_PRODUCT") 
public class Product implements Serializable { 
 
 public Product() { 
 } 
 
 @Id 
 @Column(name = "ID") 
 private Integer id; 
 
 @Column(name = "NAME") 
 private String name; 
 
 @Column(name = "PRICE") 
 private String price; 
 
 private static final long serialVersionUID = 1L; 
 
 public Integer getId() { 
  return id; 
 } 
 
 public void setId(Integer id) { 
  this.id = id; 
 } 
 
 public String getName() { 
  return name; 
 } 
 
 public void setName(String name) { 
  this.name = name; 
 } 
 
 public String getPrice() { 
  return price; 
 } 
 
 public void setPrice(String price) { 
  this.price = p



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2017-05-28java利用java.net.URLConnection发送HTTP请求的方法详解
  • 2017-05-28java 爬虫详解及简单实例
  • 2017-05-28详解Spring Boot整合Mybatis实现 Druid多数据源配置
  • 2017-05-28java加载properties文件的六种方法总结
  • 2017-05-28Spring Boot下的Job定时任务
  • 2017-05-28Java使用正则表达式实现找出数字功能示例
  • 2017-05-28Spring Boot启动过程完全解析(二)
  • 2017-05-28Java 读取外部资源的方法详解及实例代码
  • 2017-05-28详解Spring简单容器中的Bean基本加载过程
  • 2017-05-28SpringBoot整合ElasticSearch实践

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Java实现数组反转翻转的方法实例
    • mybatis中的缓存问题解析
    • Java System类详解_动力节点Java学院整理
    • java使用POI操作excel文件
    • java 中 阻塞队列BlockingQueue详解及实例
    • JVM(Java虚拟机)简介(动力节点Java学院整理)
    • JDK源码之PriorityQueue解析
    • 详解 Java Maximum redirects (100) exceeded
    • Java微信公众平台开发(8) 多媒体消息回复
    • Spring boot实现文件上传实例(多文件上传)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有