use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class EntityManagerContextTest method testCommitRetaining.
@Test
public void testCommitRetaining() throws Exception {
Transaction tx = cont.persistence().createTransaction();
try {
cont.persistence().getEntityManager();
EntityManagerContext ctx = cont.persistence().getEntityManagerContext();
ctx.setAttribute(ATTR, obj1);
ctx = cont.persistence().getEntityManagerContext();
assertTrue(ctx.getAttribute(ATTR) == obj1);
tx.commitRetaining();
cont.persistence().getEntityManager();
ctx = cont.persistence().getEntityManagerContext();
assertNull(ctx.getAttribute(ATTR));
ctx.setAttribute(ATTR, obj2);
tx.commit();
} finally {
tx.end();
}
tx = cont.persistence().createTransaction();
try {
cont.persistence().getEntityManager();
EntityManagerContext ctx = cont.persistence().getEntityManagerContext();
Object obj = ctx.getAttribute(ATTR);
assertNull(obj);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class EntityManagerContextTest method test.
@Test
public void test() throws Exception {
Transaction tx = cont.persistence().createTransaction();
try {
cont.persistence().getEntityManager();
EntityManagerContext ctx = cont.persistence().getEntityManagerContext();
ctx.setAttribute(ATTR, obj1);
cont.persistence().getEntityManager();
ctx = cont.persistence().getEntityManagerContext();
assertTrue(ctx.getAttribute(ATTR) == obj1);
tx.commit();
} finally {
tx.end();
}
tx = cont.persistence().createTransaction();
try {
cont.persistence().getEntityManager();
EntityManagerContext ctx = cont.persistence().getEntityManagerContext();
Object obj = ctx.getAttribute(ATTR);
assertNull(obj);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class EntityFetcher method fetchReloaded.
protected void fetchReloaded(Entity entity, View view, Map<Instance, Set<View>> visited, boolean optimizeForDetached, Consumer<Entity> managedEntityConsumer) {
if (!optimizeForDetached || needReloading(entity, view)) {
if (log.isTraceEnabled()) {
log.trace("Object " + entity + " is detached, loading it");
}
String storeName = metadata.getTools().getStoreName(entity.getMetaClass());
if (storeName != null) {
try (Transaction tx = persistence.getTransaction(storeName)) {
EntityManager em = persistence.getEntityManager(storeName);
// noinspection unchecked
Entity managed = em.find(entity.getClass(), entity.getId());
if (managed != null) {
// the instance here can be null if it has been deleted
managedEntityConsumer.accept(managed);
fetch(managed, view, visited, optimizeForDetached);
}
tx.commit();
}
}
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class FileLoaderImpl method checkIfFileDescriptorExists.
protected void checkIfFileDescriptorExists(FileDescriptor fd) throws FileStorageException {
try (Transaction tx = persistence.getTransaction()) {
FileDescriptor existingFile = persistence.getEntityManager().find(FileDescriptor.class, fd.getId());
if (existingFile == null || entityStates.isDeleted(existingFile)) {
throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fd.getName());
}
tx.commit();
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class Emailer method migrateAttachmentsBatch.
protected int migrateAttachmentsBatch() {
List<SendingAttachment> resultList;
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
String qstr = "select a from sys$SendingAttachment a where a.content is not null";
TypedQuery<SendingAttachment> query = em.createQuery(qstr, SendingAttachment.class);
query.setMaxResults(50);
query.setViewName(View.MINIMAL);
resultList = query.getResultList();
tx.commit();
} finally {
tx.end();
}
if (!resultList.isEmpty()) {
emailer.migrateAttachmentsToFileStorage(resultList);
}
return resultList.size();
}
Aggregations