Search in sources :

Example 1 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class TransactionTimeoutTest method testTransactionTimeoutFailure.

@Test
public void testTransactionTimeoutFailure() throws InterruptedException {
    Session session = openSession();
    try {
        Transaction transaction = session.getTransaction();
        transaction.setTimeout(1);
        assertEquals(-1, ((SessionImplementor) session).getJdbcCoordinator().determineRemainingTransactionTimeOutPeriod());
        transaction.begin();
        Thread.sleep(1000);
        session.persist(new Person("Lukasz", "Antoniak"));
        transaction.commit();
    } catch (TransactionException e) {
    // expected
    } catch (PersistenceException e) {
        assertTyping(TransactionException.class, e.getCause());
    } finally {
        session.close();
    }
}
Also used : TransactionException(org.hibernate.TransactionException) Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Person(org.hibernate.test.jdbc.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with PersistenceException

use of javax.persistence.PersistenceException in project OpenAttestation by OpenAttestation.

the class MwAssetTagCertificateJpaController method edit.

public void edit(MwAssetTagCertificate mwAssetTagCertificate) {
    EntityManager em = getEntityManager();
    try {
        em.getTransaction().begin();
        mwAssetTagCertificate = em.merge(mwAssetTagCertificate);
        mwAssetTagCertificate.getId();
        em.getTransaction().commit();
    } catch (PersistenceException ex) {
        String msg = ex.getLocalizedMessage();
        Integer id = mwAssetTagCertificate.getId();
        if (msg == null || msg.length() == 0) {
            if (id != null && findMwAssetTagCertificate(id) == null) {
                try {
                    throw new NonexistentEntityException("The mwAssetTagCertificate with id " + id + " no longer exists.");
                } catch (NonexistentEntityException ex1) {
                    Logger.getLogger(MwAssetTagCertificateJpaController.class.getName()).log(Level.SEVERE, null, ex1);
                }
            }
        }
        throw ex;
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) NonexistentEntityException(com.intel.mtwilson.as.controller.exceptions.NonexistentEntityException)

Example 3 with PersistenceException

use of javax.persistence.PersistenceException in project aries by apache.

the class TxContextBindingEntityManager method getRealEntityManager.

@Override
protected final EntityManager getRealEntityManager() {
    TransactionContext txContext = txControl.getCurrentContext();
    if (txContext == null) {
        throw new TransactionException("The resource " + provider + " cannot be accessed outside of an active Transaction Context");
    }
    EntityManager existing = (EntityManager) txContext.getScopedValue(resourceId);
    if (existing != null) {
        return existing;
    }
    EntityManager toReturn;
    EntityManager toClose;
    try {
        if (txContext.getTransactionStatus() == NO_TRANSACTION) {
            toClose = provider.createEntityManager();
            toReturn = new ScopedEntityManagerWrapper(toClose);
        } else if (txContext.supportsLocal()) {
            toClose = provider.createEntityManager();
            toReturn = new TxEntityManagerWrapper(toClose);
            txContext.registerLocalResource(getLocalResource(toClose));
            toClose.getTransaction().begin();
        } else {
            throw new TransactionException("There is a transaction active, but it does not support local participants");
        }
    } catch (Exception sqle) {
        throw new TransactionException("There was a problem getting hold of a database connection", sqle);
    }
    txContext.postCompletion(x -> {
        try {
            toClose.close();
        } catch (PersistenceException sqle) {
        }
    });
    txContext.putScopedValue(resourceId, toReturn);
    return toReturn;
}
Also used : EntityManager(javax.persistence.EntityManager) ScopedEntityManagerWrapper(org.apache.aries.tx.control.jpa.common.impl.ScopedEntityManagerWrapper) TransactionException(org.osgi.service.transaction.control.TransactionException) TxEntityManagerWrapper(org.apache.aries.tx.control.jpa.common.impl.TxEntityManagerWrapper) TransactionContext(org.osgi.service.transaction.control.TransactionContext) PersistenceException(javax.persistence.PersistenceException) PersistenceException(javax.persistence.PersistenceException) TransactionException(org.osgi.service.transaction.control.TransactionException)

Example 4 with PersistenceException

use of javax.persistence.PersistenceException in project logging-log4j2 by apache.

the class ContextDataJsonAttributeConverter method convertToEntityAttribute.

@Override
public ReadOnlyStringMap convertToEntityAttribute(final String s) {
    if (Strings.isEmpty(s)) {
        return null;
    }
    try {
        final StringMap result = ContextDataFactory.createContextData();
        final ObjectNode root = (ObjectNode) OBJECT_MAPPER.readTree(s);
        final Iterator<Map.Entry<String, JsonNode>> entries = root.fields();
        while (entries.hasNext()) {
            final Map.Entry<String, JsonNode> entry = entries.next();
            // Don't know what to do with non-text values.
            // Maybe users who need this need to provide custom converter?
            final Object value = entry.getValue().textValue();
            result.putValue(entry.getKey(), value);
        }
        return result;
    } catch (final IOException e) {
        throw new PersistenceException("Failed to convert JSON string to map.", e);
    }
}
Also used : ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) StringMap(org.apache.logging.log4j.util.StringMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PersistenceException(javax.persistence.PersistenceException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) Map(java.util.Map) StringMap(org.apache.logging.log4j.util.StringMap)

Example 5 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class PersisterClassProviderTest method testPersisterClassProvider.

@Test
@SuppressWarnings("unchecked")
public void testPersisterClassProvider() {
    Map settings = SettingsGenerator.generateSettings(PersisterClassResolverInitiator.IMPL_NAME, GoofyPersisterClassProvider.class, AvailableSettings.LOADED_CLASSES, Arrays.asList(Bell.class));
    try {
        EntityManagerFactory entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
        entityManagerFactory.close();
    } catch (PersistenceException e) {
        Assert.assertNotNull(e.getCause());
        Assert.assertNotNull(e.getCause().getCause());
        Assert.assertEquals(GoofyException.class, e.getCause().getCause().getClass());
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) Map(java.util.Map) Test(org.junit.Test)

Aggregations

PersistenceException (javax.persistence.PersistenceException)191 Test (org.junit.Test)73 Session (org.hibernate.Session)57 EntityManager (javax.persistence.EntityManager)39 Transaction (org.hibernate.Transaction)34 EntityTransaction (javax.persistence.EntityTransaction)21 IOException (java.io.IOException)18 Query (javax.persistence.Query)18 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)18 List (java.util.List)16 ArrayList (java.util.ArrayList)14 TypedQuery (javax.persistence.TypedQuery)11 JPAQuery (org.datanucleus.api.jpa.JPAQuery)10 StaleObjectStateException (org.hibernate.StaleObjectStateException)10 SQLGrammarException (org.hibernate.exception.SQLGrammarException)8 HashMap (java.util.HashMap)6 Iterator (java.util.Iterator)6 Person (org.datanucleus.samples.annotations.models.company.Person)6 MessagesEvent (com.openmeap.event.MessagesEvent)5 SQLException (java.sql.SQLException)5