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();
}
}
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();
}
}
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;
}
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);
}
}
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());
}
}
Aggregations