gpt4 book ai didi

java - 丢失具有 JPA 唯一约束的事务?

转载 作者:行者123 更新时间:2023-12-02 08:30:36 25 4
gpt4 key购买 nike

我有一个具有唯一约束的字段:

@Column(unique=true)
private String uriTitle;

当我尝试保存具有相同值的两个实体时,我收到一个异常 - 但另一个异常比我执行的要多:

java.lang.IllegalStateException: <|Exception Description: No transaction is currently active
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.rollback(EntityTransactionImpl.java:122)
at com.example.persistence.TransactionFilter.doFilter(TransactionFilter.java:35)

有问题的过滤方法如下所示:

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
EntityManager entityManager = ThreadLocalEntityManager.get();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
try {
chain.doFilter(request, response);
transaction.commit();
}
catch (Throwable t) {
transaction.rollback(); // line 35, mentioned in the exception
throw new RuntimeException(t);
}
finally {
entityManager.close();
ThreadLocalEntityManager.reset();
}
}

...和 ​​ThreadLocalEntityManager 像这样:

public class ThreadLocalEntityManager {

private static EntityManagerFactory entityManagerFactory = null;
private static final ThreadLocal<EntityManager> entityManager =
new ThreadLocal<EntityManager>() {
@Override
protected EntityManager initialValue() {
return entityManagerFactory.createEntityManager();
}
};

private ThreadLocalEntityManager() {}

public static synchronized void init(String persistenceUnit) {
requireNotNull(persistenceUnit);
requireState(entityManagerFactory == null, "'init' can be called only "
+ "once.");
entityManagerFactory =
Persistence.createEntityManagerFactory(persistenceUnit);
}

public static EntityManager get() {
requireState(entityManagerFactory != null, "Call 'init' before calling "
+ "'get'");
return entityManager.get();
}

public static void reset() {
entityManager.remove();
}

}

我用 try ...catch 包装了对 save 的调用来处理唯一约束违规,但这不起作用:

try {
ThreadLocalEntityManager.get().persist(article); // article is constrained
}
catch(Throwable t) {
t.printStackTrace();
}

知道为什么没有交易吗?处理唯一约束违规的正确方法是什么?

最佳答案

由于数据库约束违规,您的事务自动回滚,因此没有需要手动回滚的事务。为了处理这种情况,你可以写

catch (Throwable t) {
if (transaction.isActive())
transaction.rollback(); // line 35, mentioned in the exception
throw new RuntimeException(t);
}

关于java - 丢失具有 JPA 唯一约束的事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3500096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com