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

请大牛帮忙解决个问题,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]
去掉了!还是那样子!

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

jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=3000

在数据源里面加



http://commons.apache.org/dbcp/configuration.html
你进去看看,里面有很多的配置

class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">




PROPAGATION_REQUIRED,readOnly

PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED



这是个事物配置,你配置了吗

[code="java"]@Transactional(noRollbackForClassName = "java.lang.Throwable") [/code]所有异常都是Throwable的子类,你都定义了出现此异常不回滚,去掉试下。