Jdbc 为什么Ebean ORM抛出java.sql.SQLException:IJ031021:在托管事务期间无法回滚
我有一个Jax-rsrest服务,它使用Ebean查询数据库。在任何查询中,我都会抛出此异常 比如说Jdbc 为什么Ebean ORM抛出java.sql.SQLException:IJ031021:在托管事务期间无法回滚,jdbc,jax-rs,ebean,Jdbc,Jax Rs,Ebean,我有一个Jax-rsrest服务,它使用Ebean查询数据库。在任何查询中,我都会抛出此异常 比如说 User currentUser = new QUser().where().id.eq(currentUserID)).findUnique(); 日志 错误[io.ebeaninternal.server.transaction.JdbcTransaction](默认任务-10)通过回滚结束仅查询事务时出错:java.sql.SQLException:IJ031021:无法在托管事务期间回
User currentUser = new QUser().where().id.eq(currentUserID)).findUnique();
日志
错误[io.ebeaninternal.server.transaction.JdbcTransaction](默认任务-10)通过回滚结束仅查询事务时出错:java.sql.SQLException:IJ031021:无法在托管事务期间回滚
现在查询返回适当的用户,并且不会干扰Jax-RS
但我不能忽视大代码的味道
以及由于每次查询都会抛出而创建的巨大日志
我的配置
在JavaEE中使用ebean时,需要在使用EbeanServer之前对其进行配置。执行此操作的典型位置是@Startup@singletonbean管理事务ejb中的@PostConstruct方法。您需要将其配置为使用JTA事务管理器,这样它就不会试图自己开始/提交事务
@Singleton
@Startup
@TransactionManagement(TransactionManagementType.BEAN)
public class AtStartup {
@Resource(mappedName = "java:jboss/datasources/EbeanTestDS")
private DataSource ds;
@SneakyThrows
@PostConstruct
public void startup() {
new MigrationRunner(new MigrationConfig()).run(ds); // begin/commits transaction for the migration...
ServerConfig config = new ServerConfig();
config.setDataSource(ds);
config.addPackage(Customer.class.getPackage().getName());
config.setUseJtaTransactionManager(true); // This is important !
config.setAutoCommitMode(false);
EbeanServerFactory.create(config);
}
您的连接是如何设置的?@markrotterveel我添加了我对Ebean的配置。错误表明您正在使用托管事务,而不是事务管理器正在尝试回滚(可能是Ebean)。您的数据源配置是什么?您是否使用事务管理器(例如Spring
@Transactional
)?您可能需要将自动提交模式设置为true
,或者使用像ebean spring txn这样的模块。请注意,我不认识ebean,只是根据文档的某些部分进行猜测。@Markrottveel谢谢您为我指明了正确的方向。设置自动提交确实起到了作用,但我想调查一下它的来源。再次感谢你。
@Singleton
@Startup
@TransactionManagement(TransactionManagementType.BEAN)
public class AtStartup {
@Resource(mappedName = "java:jboss/datasources/EbeanTestDS")
private DataSource ds;
@SneakyThrows
@PostConstruct
public void startup() {
new MigrationRunner(new MigrationConfig()).run(ds); // begin/commits transaction for the migration...
ServerConfig config = new ServerConfig();
config.setDataSource(ds);
config.addPackage(Customer.class.getPackage().getName());
config.setUseJtaTransactionManager(true); // This is important !
config.setAutoCommitMode(false);
EbeanServerFactory.create(config);
}