Spring4 hibernate5整合 事务管理问题
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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 扫描注解的包 -->
<context:component-scan base-package="com.company.SH"></context:component-scan>
<!-- 配置数据源 -->
<!-- 导入资源文件 -->
<context:property-placeholder location="jdbc.properties"/>
<!-- 配置C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置 Hibernate 的 SessionFactory 实例 :通过 Spring 提供的LocalSessionFactoryBean 进行配置. *看清楚hibernate的版本-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 1.配置数据源属性 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 2.配置hibernate 配置文件的位置 和名称 -->
<property name="configLocation" value="hibernate.cfg.xml"></property>
<!-- 3.配置hibernate 映射文件的位置 和名称 -->
<property name="mappingLocations" value="com/company/SH/entity/*.hbm.xml"></property>
</bean>
<!-- 配置Spring 的声明式事务 -->
<!-- 1.配置事务管理器. *看清楚hibernate的版本-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 2.配置事务属性,需要事务管理器-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" rollback-for="java.lang.Exception" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 3.配置事务切点,并把切点和事务管理器关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.company.SH.service.*.*(..))" id="pointCut1"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut1"/>
</aop:config>
</beans>
DaoImpl:
package com.company.SH.dao.Impl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.company.SH.dao.BookDao;
import com.company.SH.entity.Book;
import com.company.SH.exception.BookStockException;
import com.company.SH.exception.CustomerException;
@Repository(value="bookDao")
public class BookDaoImpl implements BookDao {
@Autowired
private SessionFactory sessionFactory;
public Session getSession() {
return sessionFactory.getCurrentSession();
}
@Override
public float getBookPriceByBookId(String bookid) {
String hql = "SELECT b.price FROM Book b WHERE b.bookId = ?";
float price = (float) getSession().createQuery(hql).setParameter(0, bookid).uniqueResult();
return price;
}
@Override
public void updateBookStock(String bookid) {
//验证库存是否为充足
String hql2 = "SELECT b.stock FROM Book b WHERE b.bookId = ?";
int stock = (int) getSession().createQuery(hql2).setParameter(0, bookid).uniqueResult();
if(stock == 0){
System.out.println(123);
throw new BookStockException("书的库存不足!");
}
String hql = "UPDATE Book b SET b.stock = b.stock -1 WHERE b.bookId = ?";
getSession().createQuery(hql).setParameter(0, bookid).executeUpdate();
}
@Override
public void updateCostomerBalance(float price, String username) {
//验证余额是否充足
String hql2 = "SELECT c.balance FROM Customer c WHERE userName=?";
float balance = (float) getSession().createQuery(hql2).setParameter(0, username).uniqueResult();
if(price>balance){
System.out.println(456);
throw new CustomerException("余额不足!");
}
String hql = "UPDATE Customer c SET c.balance = c.balance - ? WHERE userName=?";
getSession().createQuery(hql).setParameter(0, price).setParameter(1, username).executeUpdate();
}
}
serviceImpl:
package com.company.SH.service.Impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.company.SH.dao.BookDao;
import com.company.SH.service.BookService;
@Service(value="bookService")
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public void borrow(String bookid, String username) {
float price = bookDao.getBookPriceByBookId(bookid);
bookDao.updateBookStock(bookid);
bookDao.updateCostomerBalance(price, username);
}
}