发布网友 发布时间:2022-04-10 08:36
共1个回答
热心网友 时间:2022-04-10 10:06
我是用的spring集成的1可以用aop,切面控制事务2可以用注解控制事务(申明式事务)3编程式事务jdbc.xml --> --> --> --> --> --> java代码:申明式事务,关键字@Transactional@Transactionalpublic int Save1(String name, String hometown) { Person person = new Person(); person.setName(name); person.setHometown(hometown); int i = personDao.inset(person); System.out.println("id,getId:" + person.getId()); System.out.println("result:" + i); System.out.println("person:" + person.toString()); User user = new User(); user.setNickName(name); user.setLoginName(name); userDao.save(user); return 1;}编程式事务: public int hello2(String name, String hometown) { //编程式事务// TransactionStatus status = this.getTransactionManager().getTransaction(this.getTxDefinition()); //手动提交// getTransactionManager().commit(status); //手动回滚 //getTransactionManager().rollback(status); TransactionStatus status = this.getTransactionManager().getTransaction(this.getTxDefinition()); try { Person person = new Person(); person.setName(name); person.setHometown(hometown); int i = personDao.inset(person); System.out.println("id,getId:" + person.getId()); System.out.println("result:" + i); System.out.println("person:" + person.toString()); User user = new User(); user.setId(46); user.setNickName(name); user.setLoginName(name); userDao.save(user); getTransactionManager().commit(status); } catch (Exception e) { e.printStackTrace(); getTransactionManager().rollback(status); } return 1; }