Spring的transaction-manager的用法

2)transaction-manager:

例 2.2.2

注意配置文件头加了两条:spring-tx-3.0.xsd和xmlns:tx

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
       >
 
    <context:component-scan
        base-package="com" />
    <context:component-scan
        base-package="service" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="NiutDAO" class="com.NiutDAO">
    </bean>
    <bean id="loginService" class="service.LoginServiceImpl" >
        <property name="niutDAO">
            <ref bean="NiutDAO" />
        </property>
    </bean>
   
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="login*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置切面 这种写法也正确"execution(* service.*.*(..))"-->
    <aop:config>
        <aop:pointcut id="myPointcut" expression="execution(* service.LoginServiceImpl.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>        
    </aop:config> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
         <property name="driverClassName"
                      value="com.mysql.jdbc.Driver"></property>
         <property name="url" 
                      value="jdbc:mysql://localhost:3306/test"></property>
         <property name="username"
                      value="root"></property>
         <property name="password" 
                      value="1234"></property>
    </bean>
    <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource" />
    </bean>
     <bean id="transactionManager"
 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>






package service;
import java.sql.Types;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import com.NiutDAO;
import service.interfac.ILoginService;

public class LoginServiceImpl implements ILoginService {
    @Resource
    private JdbcTemplate jt;
    
    NiutDAO niutDAO;
    public void setNiutDAO(NiutDAO niutDAO) {
        this.niutDAO = niutDAO;
    }
    public void login() {
        updateRegister(15,1);
        System.out.println("successfully update 1");
        updateRegisterWrong(19,2);
        System.out.println("successfully update 2");

    }    
    public void updateRegister(int age,int id) {
        String sql = "UPDATE register SET age=? WHERE id=?";
        Object params[] = new Object[] { new Integer(age),
                 new Integer(id) };
        int type[] = new int[] { Types.INTEGER , Types.INTEGER };
        jt.update(sql, params, type);
    }
    public void updateRegisterWrong(int age,int id) {
        String sql = "UPDATE register SET age=? WWWWWWW id=?";
        Object params[] = new Object[] { new Integer(age),
                new Integer(id) };
        int type[] = new int[] { Types.INTEGER , Types.INTEGER };
        jt.update(sql, params, type);
    }
}


即使updateRegister(15,1);成功执行,但数据库中结果并没有被更新,因为updateRegisterWrong(19,2);的错误致使updateRegister(15,1);的结果发生了回滚。

输出结果:
successfully update 1
严重: Servlet.service() for servlet spring threw exception
java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WWWWWWW id=2' at line 1
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570)

更多请见下节:https://blog.youkuaiyun.com/qq_44591615/article/details/109206425

<think>好的,我现在要解决用户遇到的Hibernate异常:“org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread”。首先,我需要理解这个问题的根本原因。根据用户提供的引用,特别是引用[4]提到,这个问题通常是因为跨过Service层直接调用Dao层导致的,因为事务通常是在Service层管理的[^4]。另外,引用[2]指出,当无法获取到事务同步的Session时,Hibernate会抛出这个异常[^2]。 接下来,我需要考虑可能的解决方案。根据引用[5],配置事务注解驱动可能是一个解决方法,比如在Spring配置文件中添加`<tx:annotation-driven transaction-manager="transactionManager"/>`[^5]。此外,引用[3]中的示例代码显示,直接使用openSession()而不是getCurrentSession()可能会绕过这个问题,但这样做可能需要手动管理事务,不够理想[^3]。 首先,应该检查是否在Service层正确配置了事务管理。确保Service方法上使用了@Transactional注解,这样Spring才能代理事务。然后,Dao层应该通过getCurrentSession()来获取Session,而不是openSession(),因为后者不会与事务绑定。 另外,检查Spring的配置文件是否正确配置了事务管理器和注解驱动。例如,是否声明了PlatformTransactionManager,以及是否启用了注解驱动的事务管理。 还需要确认Hibernate的版本是否与Spring版本兼容,特别是Hibernate 4及以上版本可能存在配置差异。如果配置正确,但仍然存在问题,可能需要调整SessionFactory的配置,确保它与Spring的事务管理正确集成。 总结可能的解决步骤: 1. 确保Service层使用@Transactional注解。 2. 检查Dao层是否使用getCurrentSession()而非其他方式获取Session。 3. 验证Spring配置中的事务管理器和注解驱动是否正确设置。 4. 避免在Dao层直接调用非事务方法。 同时,用户提供的引用[3]中提到的直接使用openSession()并手动管理事务虽然可行,但不符合最佳实践,可能导致事务管理混乱,应优先使用Spring声明式事务管理。</think>### 解决方案:Hibernate无法获取事务同步Session的排查与修复 #### 问题原因分析 该异常通常由以下原因导致: 1. **未正确配置事务管理**:Service层未启用事务管理,导致Dao层无法通过`getCurrentSession()`获取与事务绑定的Session。 2. **直接调用Dao层**:跨过Service层直接调用Dao层方法,绕过了事务代理机制。 3. **错误使用Session获取方式**:在Dao层使用`openSession()`而非`getCurrentSession()`,导致Session未与事务绑定。 --- #### 分步解决方案 ##### 1. 确保Service层启用事务管理 - **添加@Transactional注解**:在Service类或方法上添加Spring事务注解,声明事务边界。 ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Transactional // 关键注解,启用事务管理 @Override public void updateUser(User user) { userDao.update(user); } } ``` ##### 2. 检查Dao层Session获取方式 - **使用`getCurrentSession()`**:Dao层必须通过`sessionFactory.getCurrentSession()`获取Session,确保Session与事务绑定。 ```java @Repository public class UserDaoImpl implements UserDao { @Autowired private SessionFactory sessionFactory; // 正确方式:获取事务同步的Session private Session getSession() { return sessionFactory.getCurrentSession(); } @Override public void update(User user) { getSession().update(user); // 无需手动提交事务 } } ``` ##### 3. 验证Spring事务配置 - **配置事务管理器与注解驱动**:在Spring配置文件中(如`applicationContext.xml`)添加以下内容: ```xml <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 启用注解驱动的事务管理 --> <tx:annotation-driven transaction-manager="transactionManager"/> ``` ##### 4. 避免跨层调用 - **通过Service层调用Dao**:确保所有数据库操作经过Service层,禁止Controller直接调用Dao。 --- #### 其他注意事项 - **Hibernate与Spring版本兼容性**:Hibernate 4+需使用`HibernateTransactionManager`,而非旧版`Hibernate3TransactionManager`。 - **事务传播行为**:若需嵌套事务,可通过`@Transactional(propagation = Propagation.REQUIRED)`指定传播机制。 - **手动回滚事务**:在事务方法中捕获异常后,可调用`TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()`手动回滚。 --- ### 相关问题 1. **如何在Spring中配置声明式事务管理?** 2. **@Transactional注解有哪些常用属性?** 3. **Hibernate的`getCurrentSession()`和`openSession()`有什么区别?** 4. **事务传播机制有哪些类型?如何选择?**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mark_to_win

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值