Lost transaction with JPA unique constraint? <p>I have a field with an unique constraint:</p> <pre><code>@Column(unique=true) private String uriTitle; </code></pre> <p>When I try to save two entities with the same value, I get an exception - but another exception than I exected:</p> <pre><code>java.lang.IllegalStateException: &lt;|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) </code></pre> <p>The questionable filter method looks like this:</p> <pre><code>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(); } } </code></pre> <p>... and the ThreadLocalEntityManager like this:</p> <pre><code>public class ThreadLocalEntityManager { private static EntityManagerFactory entityManagerFactory = null; private static final ThreadLocal&lt;EntityManager&gt; entityManager = new ThreadLocal&lt;EntityManager&gt;() { @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(); } } </code></pre> <p>I wrapped the call to <code>save</code> with <code>try ...catch</code> to handle the unique constraint violation, but that doesn't work:</p> <pre><code>try { ThreadLocalEntityManager.get().persist(article); // article is constrained } catch(Throwable t) { t.printStackTrace(); } </code></pre> <p>Any idea why there is no transaction? What is the correct way to handle a unique constraint violation?</p>