pom 文件如下
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-narayana-jta</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mongodb-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mongodb-panache</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
代码如下
public String testAffairTemp(String name) throws Exception{
Student studentByName = new Student().getStudentByName(name);
try{
transaction.begin();
studentByName.setName("年糕");
studentByName.persistOrUpdate();
studentByName.update();
Student student = new Student();
student.setName("美猴王");
student.setAge(27);
student.setPhone("123123123");
student.setId(new ObjectId("62779c8c364eef2a0479f57b"));
student.persist();
transaction.commit();
}
catch (RuntimeException e){
transaction.rollback();
System.out.println("yunxing");
e.printStackTrace();
}
catch (Exception e){
System.out.println("抓到不会滚异常");
e.printStackTrace();
}
return studentByName.toString();
}
其中 student.setId(new ObjectId("62779c8c364eef2a0479f57b")); 这句话我故意制造runtimeexception 但是异常时抓到了但是并没有回滚
附官网代码如下:
@ApplicationScoped
public class SantaClausService {
@Inject ChildDAO childDAO
@Inject SantaClausDAO santaDAO
@Inject UserTransaction transaction
public void getAGiftFromSanta(Child child, String giftDescription) throws Exception {
// some transaction work
try {
transaction.begin();
Gift gift = childDAO.addToGiftList(child, giftDescription);
santaDAO.addToSantaTodoList(gift);
transaction.commit();
}
catch(SomeException e) {
// do something on Tx failure
transaction.rollback();
}
}
}
不理解为什么事务会失效,是因为我用了uni去接纳数据的原因吧 ,有可能是uni是异步非阻塞的所以可能不是同一个事务了,求解答