【问题标题】:Spring :: @Transactional not workingSpring :: @Transactional 不起作用
【发布时间】:2016-07-12 20:53:39
【问题描述】:

我是 Spring 新手,正在学习事务概念。无法让@Transactional 工作。

用例:
当 getEmployee() 抛出 RunTimeException 时,员工和员工详细信息的数据插入应该回滚。但是回滚没有发生。 我正在使用 Oracle 数据库 11g 和 spring 4.3.1.RELEASE。以下是正在运行的独立 java 代码。

代码

public static void main(String[] args) { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("spring-bean.xml"); ctx.registerShutdownHook();

Employee emp = new Employee("149518", "Mickey", "Mouse", "15 years", "tennis");

IEmployee empIntfc = (IEmployee)ctx.getBean("empService");
try {
        empIntfc.createEmployee(emp);
        empIntfc.createEmployeeDetails(emp);

        //Below will throw RunTime Exception
        empIntfc.getEmployee(2);

    }catch (Exception e ) {
        e.printStackTrace();

    } finally {
        ctx.close();    
    }

}

EmployeeService.java

public class EmployeeService implements IEmployee {

private DataSource dataSource;
private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    jdbcTemplate = new JdbcTemplate(this.dataSource);
}
public JdbcTemplate getJdbcTemplate() {
    return jdbcTemplate;
}

@Override
@Transactional
public int createEmployee(Employee emp) {       

    String sql1 = "INSERT INTO TEST_T1(EMP_ID, EMP_FNAME, EMP_LNAME) values   
    (?,?,?)";
    return getJdbcTemplate().update(sql1, emp.getEmpId(), 
    emp.getEmpFirstName(), emp.getEmpLastName());
}

@Override
@Transactional
public int createEmployeeDetails(Employee emp) {

    String sql = "INSERT INTO TEST_T2(EMP_ID, EXP, SKILLS) values (?,?,?)";     
    return getJdbcTemplate().update(sql, emp.getEmpId(), emp.getExp(), 
    emp.getSkills());
}

@Override
@Transactional(readOnly = true, noRollbackFor=RuntimeException.class)
public Employee getEmployee(int empId) {
    throw new RuntimeException("Intentional runtime exception");
}

}

spring-bean.xml

<beans xmlns="http://www.springframework.org/schema/beans">

<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager" 
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource"   
  class="org.springframework.jdbc.datasource.DriverManagerDataSource"  >
  <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
  <property name="url" value="jdbc:oracle:thin:@//xxxx:1521/xxxx"/>
  <property name="username" value="user"/>
  <property name="password" value="user"/>
  </bean> 
  <bean id="empService" class="com.service.EmployeeService">
  <property name="dataSource" ref="dataSource"/>
  </bean>   
  </beans>

【问题讨论】:

  • 这里已经有一个公认的解决方案 - stackoverflow.com/questions/9649318/…
  • 你的主要方法不是事务性的......意味着:输入'createEmployee'创建一个新事务并提交它,'createEmployeeDetails'创建一个新事务并提交它......
  • 我使 main() 方法具有事务性,但它不起作用。获取您的指针并将对 createEmployee、createEmployeeDetails 和 getEmployee 的调用包装在不同的类中。在新的类方法中使用事务性,它现在正在工作。感谢您的帮助。

标签: spring transactions rollback spring-transactions transactional


【解决方案1】:

您的主要方法不是事务性的......意味着:输入“createEmployee”创建一个新事务并提交它,“createEmployeeDetails”创建一个新事务并提交它。

【讨论】:

    猜你喜欢
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 2018-10-09
    • 1970-01-01
    相关资源
    最近更新 更多