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

网友投稿 205 2023-05-30


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

环境与版本

本文出来之前的一篇文章中的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事务)

thread

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

jta

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

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

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

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

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

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

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

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

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

xml 方式配置事务代码实例

代码结构如下:

applicationContext.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:util="http://springframework.org/schema/util" xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.1.xsd

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd">

destroy-method="close">

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

org.hibernate.dialect.OracleDialect

update

true

true

20

true

http:// org.springframework.orm.hibernate4.SpringSessionContext

true

com.oscar999.trans.sprhib.model

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:util="http://springframework.org/schema/util" xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.1.xsd

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd">

destroy-method="close">

destroy-method="close">

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

org.hibernate.dialect.OracleDialect

update

true

true

20

true

http:// org.springframework.orm.hibernate4.SpringSessionContext

true

com.oscar999.trans.sprhib.model

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

org.hibernate.dialect.OracleDialect

update

true

true

20

true

http:// org.springframework.orm.hibernate4.SpringSessionContext

true

com.oscar999.trans.sprhib.model

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

config.properties

jdbc.driver=oracle.jdbc.driver.OracleDriver

jdbc.url=jdbc:oracle:thin:@12.6.18.43:1521:orcl

jdbc.username=

jdbc.password=oracle

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 = price;

}

}

ProductDAOImpl.java

/**

* @Title: ProductDAOImpl.java

* @Package com.oscar999.trans.sprhib

* @Description:

* @author XM

* @date Feb 15, 2017 4:15:09 PM

* @version V1.0

*/

package com.oscar999.trans.sprhib.dao;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

import com.oscar999.trans.sprhib.model.Product;

/**

* @author XM

*

*/

@Repository

public class ProductDAOImpl {

@Autowired

private SessionFactory sessionFactory;

public Product findProductById(int id) {

Session session = sessionFactory.getCurrentSession();

Product product = (Product) session.get(Product.class, id);

return product;

}

public Product saveProduct(Product product) {

Session session = sessionFactory.getCurrentSession();

session.save(product);

return product;

}

}

ProductServiceImpl.java

package com.oscar999.trans.sprhib;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.oscar999.trans.sprhib.dao.ProductDAOImpl;

import com.oscar999.trans.sprhib.model.Product;

@Service

public class ProductServiceImpl {

@Autowired

private ProductDAOImpl productdao;

public void findProduct(int id) {

Product product = productdao.findProductById(id);

if (product != null) {

System.out.println(product.getName());

}

}

public void saveProduct() {

Product product = new Product();

product.setId(2);

product.setName("product2");

product.setPrice("price2");

productdao.saveProduct(product);

}

}

TestMain.java

/**

* @Title: TestMain.java

* @Package com.oscar999.trans.sprhib

* @Description:

* @author XM

* @date Feb 15, 2017 3:54:54 PM

* @version V1.0

*/

package com.oscar999.trans.sprhib;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* @author XM

*

*/

public class TestMain {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/oscar999/trans/sprhib/applicationContext.xml");

ProductServiceImpl productService = applicationContext.getBean(ProductServiceImpl.class);

//productService.findProduct(1);

productService.saveProduct();

}

}

说明如下:

get 可以不需要transaction

插入或是更新如果没有的话, 就不会更新成功

声明方式配置事务

需要在xml配制中设置

事物注解方式: @Transactional

当标于类前时,标示类中所有方法都进行事物处理,以下代码在service层进行事务处理(给Service层配置事务是比较好的方式,因为一个Service层方法操作可以关联到多个DAO的操作。在Service层执行这些Dao操作,多DAO操作有失败全部回滚,成功则全部提交。)

@Service

@Transactional

public class UserServiceImpl implements UserService {

@Autowired

private UserDao userDao;

public User getUserById(int id) {

return userDao.findUserById(id);

}

}

当类中某些方法不需要事物时:

@Service

@Transactional

public class UserServiceImpl implements UserService {

@Autowired

private UserDao userDao;

@Transactional(propagation = Propagation.NOT_SUPPORTED)

public User getUserById(int id) {

return userDao.findUserById(id);

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用java能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对我们的支持。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java 中HashMap实现原理深入理解
下一篇:vue2.0获取自定义属性的值
相关文章

 发表评论

暂时没有评论,来抢沙发吧~