better transaction handling

This commit is contained in:
Peter Hormanns 2023-08-04 20:34:34 +02:00
parent 432904eef1
commit 4e7c0f204b
5 changed files with 27 additions and 16 deletions

View File

@ -5,7 +5,7 @@
<groupId>de.hsadmin</groupId>
<artifactId>hsar</artifactId>
<packaging>war</packaging>
<version>4.0.12</version>
<version>4.0.13</version>
<name>HSAdmin Stable Backend Webapp</name>
<url>http://maven.apache.org</url>
<properties>
@ -36,12 +36,12 @@
<dependency>
<groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-util</artifactId>
<version>4.0.12</version>
<version>4.0.13</version>
</dependency>
<dependency>
<groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-qserv</artifactId>
<version>4.0.12</version>
<version>4.0.13</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.hsadmin</groupId>
<artifactId>hsadmin-qserv</artifactId>
<version>4.0.12</version>
<version>4.0.13</version>
<packaging>jar</packaging>
<name>HSAdmin-QServ</name>
@ -20,7 +20,7 @@
<dependency>
<groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-qserv</artifactId>
<version>4.0.12</version>
<version>4.0.13</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
@ -84,7 +84,7 @@
<type>file</type>
<mapper>
<type>perm</type>
<prefix>/lib/systemd/system/</prefix>
<prefix>/lib/systemd/system</prefix>
<filemode>644</filemode>
</mapper>
</data>

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-qserv</artifactId>
<version>4.0.12</version>
<version>4.0.13</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version>
@ -13,7 +13,7 @@
<dependency>
<groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-util</artifactId>
<version>4.0.12</version>
<version>4.0.13</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>

View File

@ -12,6 +12,7 @@ import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
@ -33,10 +34,10 @@ public class Transaction {
private InitialContext ctx;
public Transaction(String loginName) {
transactionActive = false;
this.transactionActive = false;
this.entityManager = PersistenceManager.getEntityManager("hsadmin");
this.loginName = loginName;
taskStores = new HashMap<String, QueueTaskStore>();
this.taskStores = new HashMap<String, QueueTaskStore>();
try {
ctx = new InitialContext();
Context env = (Context) ctx.lookup("java:comp/env");
@ -104,24 +105,34 @@ public class Transaction {
public void beginTransaction() {
transactionActive = true;
entityManager.getTransaction().begin();
final EntityTransaction tx = entityManager.getTransaction();
if (!tx.isActive()) {
tx.begin();
}
}
public void commitTransaction() {
try {
entityManager.getTransaction().commit();
} catch (Exception e) {
throw new TechnicalException(e);
final EntityTransaction tx = entityManager.getTransaction();
if (tx.isActive()) {
tx.commit();
}
sendAll();
} catch (Exception e) {
throw new TechnicalException(e);
} finally {
transactionActive = false;
}
}
public void rollbackTransaction() {
taskStores.clear();
transactionActive = false;
try {
entityManager.getTransaction().rollback();
final EntityTransaction tx = entityManager.getTransaction();
if (tx.isActive()) {
tx.rollback();
}
} catch (IllegalStateException e) {
throw new TechnicalException(e);
}

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.hsadmin.core</groupId>
<artifactId>hsadmin-util</artifactId>
<version>4.0.12</version>
<version>4.0.13</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version>