jdbc 事物问题
测试代码public class BookService {

private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void addBook(String bookName) throws Exception{
String insert = "INSERT INTO T_BOOK(ID,BOOK_NAME,OPER_TIME) VALUES(sys_guid(),'"+bookName+"',sysdate)";
jdbcTemplate.execute(insert);
//throw new RuntimeException("3434");
throw new Exception("-----------");
}
}
private BookService bookService;

public void setBookService(BookService bookService) {
this.bookService = bookService;
} public void addUser(String bookName,String userName) throws Exception{
this.bookService.addBook(bookName);

String insert = " insert into t_user(id,user_name) values(sys_guid(),'"+userName+"') ";
jdbcTemplate.execute(insert);

}
<tx:advice id="txAdvice" transaction-manager="transactionManager">  
    <tx:attributes>  
     <tx:method name="addBook" propagation="REQUIRES_NEW"/>
     <tx:method name="add*" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice>
public static void main(String[] args) throws Exception{

     context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        /*UserService userService = (UserService) context.getBean("userService");
userService.addUser("测试数据2");*/
    
     /*
     BookService bookService = (BookService) context.getBean("bookService");
     bookService.addBook("bookName-1");*/
    
     UserService userService = (UserService) context.getBean("userService");
userService.addUser("b-ck44", "u_cz44");
}执行日志:
INFO [2017-07-18 12:55:26 0838] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@102799c: startup date [Tue Jul 18 12:55:26 CST 2017]; root of context hierarchy
INFO [2017-07-18 12:55:26 0964] - Loading XML bean definitions from class path resource [ApplicationContext.xml]
INFO [2017-07-18 12:55:27 0852] - Loaded JDBC driver: oracle.jdbc.driver.OracleDriver
DEBUG[2017-07-18 12:57:00 0967] - Creating new transaction with name [com.withub.springTransaction.service.UserService.addUser]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
DEBUG[2017-07-18 12:57:10 0484] - Creating new JDBC DriverManager Connection to [jdbc:oracle:thin:@localhost:1521:cmoausr]
DEBUG[2017-07-18 12:57:12 0426] - Acquired Connection [oracle.jdbc.driver.T4CConnection@85e57] for JDBC transaction
DEBUG[2017-07-18 12:57:20 0533] - Switching JDBC Connection [oracle.jdbc.driver.T4CConnection@85e57] to manual commit
DEBUG[2017-07-18 12:59:14 0640] - Suspending current transaction, creating new transaction with name [com.withub.springTransaction.service.BookService.addBook]
DEBUG[2017-07-18 12:59:20 0424] - Creating new JDBC DriverManager Connection to [jdbc:oracle:thin:@localhost:1521:cmoausr]
DEBUG[2017-07-18 12:59:20 0537] - Acquired Connection [oracle.jdbc.driver.T4CConnection@187d27e] for JDBC transaction
DEBUG[2017-07-18 12:59:20 0538] - Switching JDBC Connection [oracle.jdbc.driver.T4CConnection@187d27e] to manual commit
DEBUG[2017-07-18 12:59:58 0806] - Executing SQL statement [INSERT INTO T_BOOK(ID,BOOK_NAME,OPER_TIME) VALUES(sys_guid(),'b-cz44',sysdate)]
DEBUG[2017-07-18 13:00:03 0701] - Initiating transaction commit
DEBUG[2017-07-18 13:00:03 0702] - Committing JDBC transaction on Connection [oracle.jdbc.driver.T4CConnection@187d27e]
DEBUG[2017-07-18 13:00:03 0705] - Releasing JDBC Connection [oracle.jdbc.driver.T4CConnection@187d27e] after transaction
DEBUG[2017-07-18 13:00:03 0706] - Returning JDBC Connection to DataSource
DEBUG[2017-07-18 13:00:03 0728] - Resuming suspended transaction after completion of inner transaction
DEBUG[2017-07-18 13:01:20 0589] - Initiating transaction commit
DEBUG[2017-07-18 13:01:20 0590] - Committing JDBC transaction on Connection [oracle.jdbc.driver.T4CConnection@85e57]
DEBUG[2017-07-18 13:01:20 0592] - Releasing JDBC Connection [oracle.jdbc.driver.T4CConnection@85e57] after transaction
DEBUG[2017-07-18 13:01:20 0592] - Returning JDBC Connection to DataSource
Exception in thread "main" java.lang.Exception: -----------结果:
t_book 表的数据插入成功,t_user 表的数据插入不成功;问题:
addUser 事物类型是REQUIRED,addBook 的事物类型是 REQUIRES_NEW,日志提示两个事物都正常提交,但为什么数据库中t_user表数据插入不成功?