在 Spring Boot 中推荐使用 JTA 和 Hibernate

Is JTA recommended with Hibernate in Springboot

我必须将休眠与 spring 引导一起使用。是否建议在此堆栈中使用 JTA 事务管理器?

如果推荐使用 JTA,如何在控制器或服务层以编程方式访问当前会话?有例子会更有帮助。

作为 spring 文档 says:

Typically, you need an application server’s JTA capability only if your application needs to handle transactions across multiple resources, which is not a requirement for many applications.

并根据spring开机documentation

Spring Boot supports distributed JTA transactions across multiple XA resources by using either an Atomikos or Bitronix embedded transaction manager. JTA transactions are also supported when deploying to a suitable Java EE Application Server.

When a JTA environment is detected, Spring’s JtaTransactionManager is used to manage transactions. Auto-configured JMS, DataSource, and JPA beans are upgraded to support XA transactions. You can use standard Spring idioms, such as @Transactional, to participate in a distributed transaction.

第二个问题你可以看看this的回答