mydreamlive 2009-01-20 17:05
浏览 494
已采纳

请大牛帮忙解决个问题,spring+jpa(全标注配置) 事务不回滚

在一个事务中,插入两条数据,有一条数据已存在数据库中,执行程序后应该发生异常,事务进行回滚,不插入数据库任何数据;但程序执行后发生了异常,但事务没有回滚,数据库又新增了一条数据,请大牛帮忙解决一下!
[b]配置文件:[/b]
<?xml version="1.0" encoding="UTF-8"?>
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<!-- 服务功能配置 -->
<!-- 数据源配置  -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/test" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

<!-- JPA EntityManagerFactory -->
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL" />
            <property name="showSql" value="yes" />
        </bean>
    </property>
</bean>

<!-- 事务配置   -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />

<context:component-scan base-package="com.spring"/>


[b]dao层:[/b]
package com.spring.test6.esp.sample001.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.spring.test6.esp.sample001.model.User;

@Service("UserDAO")
public class UserDAOImpl implements UserDAO {

private EntityManager em;

@PersistenceContext
public void setEntityManager(EntityManager em) {
    this.em = em;
}

public EntityManager getEntityManager() {
    return this.em;
}   

public void create(User user) {
    em.persist(user);
}

}
[b]service层:[/b]
package com.spring.test6.esp.sample001.service;

import org.springframework.transaction.annotation.Transactional;

import com.spring.test6.esp.common.service.BaseServiceImpl;
import com.spring.test6.esp.sample001.model.User;

@Transactional
public class UserServiceImpl extends BaseServiceImpl implements
UserService {

public UserServiceImpl() {
    super(User.class);
}

@Override
@Transactional(noRollbackForClassName = "java.lang.Throwable")
public void create1() {
    try {
        User user = new User();
        user.setId(68);
        user.setName("caterpillar");
        user.setAge(30);
        User user1 = new User();
        user1.setId(82);
        user1.setName("caterpillar");
        user1.setAge(30);
        this.dao.create(user1);
        this.dao.create(user);
    } catch (RuntimeException re) {
        throw re;
    }
}

}
[b]调用:[/b]
package com.spring.test6.esp.sample001.controller;

/*

  • 鍩烘湰CRUD鎿嶄綔 */ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.test6.esp.sample001.service.UserService;

public class UserFormController {
public static void main(String[] args) {
String[] locations = {"beans6.xml"};
ApplicationContext ctx =
new ClassPathXmlApplicationContext(locations);
UserService _userService = (UserService) ctx.getBean("UserService");
try {
_userService.create1();
} catch (Exception e) {
System.out.println(e.getClass().getName());
}
}
}

[b]问题补充:[/b]
能写详细一些吗,比如在那里加,谢谢!
[b]问题补充:[/b]
还是不行啊!还是报异常,插数据!
[b]问题补充:[/b]


也不行!是我代码那块写错了吗?
[b]问题补充:[/b]
配了,运行还是老样子!
[b]问题补充:[/b]
去掉了!还是那样子!

  • 写回答

7条回答 默认 最新

  • maoxiaolu 2009-01-20 17:23
    关注

    表类型看看,是否是支持回滚的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?