Grundsätzliche Frage zu JPA, mehreren Enitäten und Transaktionen

Veit

Mitglied
Hallo,

folgendes Szenario:

Eine Anwendung hat zwei Entitäten der Klasse Konto. Von der einen Entität wird ein Betrag abgebucht, auf der anderen aufgebucht. Gleichzeitig soll eine Entity der Klasse Kontobewegung erzeugt werden, die nur die IDs der beiden Konten und den Betrag enthält. In dem Code den ich vorliegen habe passiert folgendes:

kontoVon.setzeNeuenKontostand(AlterKontoStand-Betrag);
kontoZu.SetzeNeuenKontoStand(AlterKontoStand+Betrag);
kontoBewegung = new KontoBewegung(KontoVon.id, KontoZu.id)

em.merge(KontoVon)
em.merge(KontoZu)
em.merge(kontoBewegung)

(Entschuldigt den Pseudo-Code)

Jetzt wird doch aber intern keine Transaktion draus, oder? Muss aber, denn die Kontostände sollen nur geändert werden, wenn beide gesetzt wurden. Wie kann man sowas umsetzen?
Vielen Dank für eure Hilfe!
 

Veit

Mitglied
Hallo DanZ,
danke für deine Antwort.

das hab ich versucht, allerdings bekomm ich dann immer die Fehlermeldung:

Cannot use an EntityTransaction while using JTA.
 
M

maki

Gast
Solltest ein bisschen mehr erzählen... was für ein Enviironment? (JBoss, Glassfish, Spring, Guice, etc. pp.)

Generell sollte man Transaktionen nicht mit em.getTransaction().begin() etc. starten, sondern deklerativ per Annotation.
 

DanZ

Bekanntes Mitglied
Ok, ich war jetzt von einer standalone applikation ausgegangen. Dann wären ein paar mehr Informationen notwendig:Java SE applikation oder enterprise - wenn ja web, ejb oder was sonst?, persistence.xml und die Klasse in der der Code steht wären auch hilfreich :)
 

Veit

Mitglied
Es handelt sich um eine EEE-Anwendung mit EJB3 auf dem OC4J. JPA-Implementierung ist TopLink.

EJB-Code :

Java:
@Stateless
@Remote(de.fpm.rp2.ejbremote.AccountRemote.class)
@TransactionManagement(TransactionManagementType.CONTAINER)
public class AccountBean implements AccountRemote
{
	private static final Log log = LogFactory.getLog(AccountBean.class);

	@PersistenceContext(unitName = Application.PERSISTENCE_UNIT_NAME)
	private EntityManager em;

	@PersistenceUnit
	EntityManagerFactory emf;

	@PostConstruct
	public void postConstruct()
	{
		if (log.isDebugEnabled())
			log.debug("@PostConstruct");
	}

	@PreDestroy
	public void preDestroy()
	{
		if (log.isDebugEnabled())
			log.debug("@PreDestroy");
	}



	/**
	 * Set new balance-values
	 */
	@Override
	public void setAccountBalance(BigDecimal ammount, String process, User user, Location location, String keyFrom, String keyTo)
	{

			Account accountFrom;
			Account accountTo;

			// get accounts
			accountFrom = this.getAccountByLocation(location.getId(), keyFrom);
			accountTo = this.getAccountByLocation(location.getId(), keyTo);

			
			// set new balance, setup accounttransaction
			if (accountFrom != null)
			{
				accountFrom.setBalance(accountFrom.getBalance().subtract(ammount));
				accTrans.setRpAccount1(accountFrom);
				this.setupForSave(accountFrom);
				this.em.joinTransaction();
				this.em.merge(accountFrom);
			}

			
			
			if (accountTo != null)
			{
				accountTo.setBalance(accountTo.getBalance().add(ammount));
				accTrans.setRpAccount2(accountTo);
				this.setupForSave(accountTo);
				this.em.joinTransaction();
				this.em.merge(accountTo);
			}


			em.flush();
		}
}

persitence.xml
Java:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="FPMPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/EnterpriseSmallApps</jta-data-source>
    <class>de.fpm.rp.persistence.Account</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.logging.level" value="INFO" />
      <property name="eclipselink.logging.logger" value="ServerLogger" />
      <property name="eclipselink.logging.session" value="false" />
      <property name="eclipselink.logging.exceptions" value="false" />
      <property name="eclipselink.logging.thread" value="true" />
      <property name="eclipselink.logging.timestamp" value="true" />
      <property name="eclipselink.target-database" value="Oracle" />
      <property name="eclipselink.target-server" value="OC4J" />
      <property name="eclipselink-session-name" value="RpSession" />
      <property name="eclipselink.cache.type.default" value="NONE"/>
    </properties>
  </persistence-unit>
</persistence>

Wenn ich jetzt nach dem speichern von accountFrom eine Exception erzeuge, wird accountTo nicht aktualisiert, während accountFrom den neuen Wert behält.

Gruß,
Veit
 

DanZ

Bekanntes Mitglied
Versuch mal die Methode mit der Logik mit einem "@TransactionAttribute(TransactionAttributeType.REQUIRED)" zu annotieren
 

Veit

Mitglied
Hab ich versucht, bringt folgende Fehlermeldung:

Unerwarteter Fehler - java.lang.IllegalStateException: Cannot use resource level transactions with a container managed EntityManager

???:L
 

DanZ

Bekanntes Mitglied
Ein kompletter Stracktrace wäre gut

Edit: Das mit dem "joinTransaction" würd ich bei CMT aber auch rausnehmen
 

Veit

Mitglied
puhhh...ich hoffe du kannst damit etwas anfangen

Java:
2012-02-17 21:06:08.899 ERROR J2EE EJB-08006 [AccountBean:public void de.ospkdd.rp2.ejb.AccountBean.setAccountBalance(java.math.BigDecimal,java.lang.String,de.ospkdd.rp2.persistence.User,de.ospkdd.rp2.persistence.Location,java.lang.String,java.lang.String)] Exception beim Aufruf von Methode aufgetreten: javax.transaction.TransactionRolledbackException: 
Exception Description: Cannot use an EntityTransaction while using JTA.; nested exception is: 
	java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.
2012-02-17 21:06:08.899 WARNING J2EE RMI-00009 Exception von Remote-Server zurückgegeben: {0}
12/02/17 21:06:08 javax.ejb.EJBException: 
Exception Description: Cannot use an EntityTransaction while using JTA.; nested exception is: 
	java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.; nested exception is: javax.transaction.TransactionRolledbackException: 
Exception Description: Cannot use an EntityTransaction while using JTA.; nested exception is: 
	java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.
12/02/17 21:06:08 javax.transaction.TransactionRolledbackException: 
Exception Description: Cannot use an EntityTransaction while using JTA.; nested exception is: 
	java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.
12/02/17 21:06:08 	at com.evermind.server.ejb.EJBUtils.getUserException(EJBUtils.java:338)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.AbstractTxInterceptor.convertAndHandleMethodException(AbstractTxInterceptor.java:75)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.TxBeanManagedInterceptor.invoke(TxBeanManagedInterceptor.java:55)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:119)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.DMSInterceptor.invoke(DMSInterceptor.java:52)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:119)
12/02/17 21:06:08 	at com.evermind.server.ejb.InvocationContextPool.invoke(InvocationContextPool.java:55)
12/02/17 21:06:08 	at com.evermind.server.ejb.StatelessSessionEJBObject.OC4J_invokeMethod(StatelessSessionEJBObject.java:87)
12/02/17 21:06:08 	at AccountBean_RemoteProxy_120i08f.setAccountBalance(Unknown Source)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
12/02/17 21:06:08 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
12/02/17 21:06:08 	at java.lang.reflect.Method.invoke(Method.java:597)
12/02/17 21:06:08 	at com.evermind.server.rmi.RmiMethodCall.run(RmiMethodCall.java:53)
12/02/17 21:06:08 	at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
12/02/17 21:06:08 	at java.lang.Thread.run(Thread.java:662)
12/02/17 21:06:08 Caused by: java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.
12/02/17 21:06:08 	at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.getTransaction(JTATransactionWrapper.java:65)
12/02/17 21:06:08 	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.getTransaction(EntityManagerImpl.java:867)
12/02/17 21:06:08 	at de.fpm.rp.ejb.AccountBean.setAccountBalance(AccountBean.java:197)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
12/02/17 21:06:08 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
12/02/17 21:06:08 	at java.lang.reflect.Method.invoke(Method.java:597)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.joinpoint.EJBJoinPointImpl.invoke(EJBJoinPointImpl.java:35)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:119)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.SetContextActionInterceptor.invoke(SetContextActionInterceptor.java:44)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:119)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.DMSInterceptor.invoke(DMSInterceptor.java:52)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:119)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.TxBeanManagedInterceptor.invoke(TxBeanManagedInterceptor.java:53)
12/02/17 21:06:08 	... 13 more
12/02/17 21:06:08 javax.ejb.EJBException: 
Exception Description: Cannot use an EntityTransaction while using JTA.; nested exception is: 
	java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.; nested exception is: javax.transaction.TransactionRolledbackException: 
Exception Description: Cannot use an EntityTransaction while using JTA.; nested exception is: 
	java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.
12/02/17 21:06:08 	at com.evermind.server.ejb.EJBUtils.createEJBException(EJBUtils.java:365)
12/02/17 21:06:08 	at com.evermind.server.ejb.EJBUtils.createEJBException(EJBUtils.java:356)
12/02/17 21:06:08 	at com.evermind.server.ejb.AbstractEJBObject.OC4J_handleUncheckedException(AbstractEJBObject.java:396)
12/02/17 21:06:08 	at AccountBean_RemoteProxy_120i08f.setAccountBalance(Unknown Source)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
12/02/17 21:06:08 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
12/02/17 21:06:08 	at java.lang.reflect.Method.invoke(Method.java:597)
12/02/17 21:06:08 	at com.evermind.server.rmi.RmiMethodCall.run(RmiMethodCall.java:53)
12/02/17 21:06:08 	at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
12/02/17 21:06:08 	at java.lang.Thread.run(Thread.java:662)
12/02/17 21:06:08 	at com.evermind.server.rmi.RMICall.warningExceptionOriginatesFromTheRemoteServer(RMICall.java:109)
12/02/17 21:06:08 	at com.evermind.server.rmi.RMICall.throwRecordedException(RMICall.java:129)
12/02/17 21:06:08 	at com.evermind.server.rmi.RMIClientConnection.obtainRemoteMethodResponse(RMIClientConnection.java:571)
12/02/17 21:06:08 	at com.evermind.server.rmi.RMIClientConnection.invokeMethod(RMIClientConnection.java:515)
12/02/17 21:06:08 	at com.evermind.server.rmi.RemoteInvocationHandler.invoke(RemoteInvocationHandler.java:63)
12/02/17 21:06:08 	at com.evermind.server.rmi.RecoverableRemoteInvocationHandler.invoke(RecoverableRemoteInvocationHandler.java:28)
12/02/17 21:06:08 	at com.evermind.server.ejb.StatelessSessionRemoteInvocationHandler.invoke(StatelessSessionRemoteInvocationHandler.java:43)
12/02/17 21:06:08 	at __Proxy63.setAccountBalance(Unknown Source)
12/02/17 21:06:08 	at de.fpm.rp2.webapp.MgmtCashEmptySafeController.emptySafeReturnListener(MgmtCashEmptySafeController.java:152)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
12/02/17 21:06:08 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
12/02/17 21:06:08 	at java.lang.reflect.Method.invoke(Method.java:597)
12/02/17 21:06:08 	at com.sun.el.parser.AstValue.invoke(AstValue.java:187)
12/02/17 21:06:08 	at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
12/02/17 21:06:08 	at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
12/02/17 21:06:08 	at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:69)
12/02/17 21:06:08 	at org.apache.myfaces.trinidad.component.UIXComponentBase.broadcastToMethodBinding(UIXComponentBase.java:1170)
12/02/17 21:06:08 	at org.apache.myfaces.trinidad.component.UIXCommand.broadcast(UIXCommand.java:177)
12/02/17 21:06:08 	at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:115)
12/02/17 21:06:08 	at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:191)
12/02/17 21:06:08 	at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
12/02/17 21:06:08 	at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:105)
12/02/17 21:06:08 	at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:80)
12/02/17 21:06:08 	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:143)
12/02/17 21:06:08 	at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:64)
12/02/17 21:06:08 	at de.fpm.util.servlet.SecurityFilter.doFilter(SecurityFilter.java:51)
12/02/17 21:06:08 	at com.evermind.server.http.EvermindFilterChain.doFilter(EvermindFilterChain.java:15)
12/02/17 21:06:08 	at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._invokeDoFilter(TrinidadFilterImpl.java:261)
12/02/17 21:06:08 	at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._doFilterImpl(TrinidadFilterImpl.java:218)
12/02/17 21:06:08 	at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl.doFilter(TrinidadFilterImpl.java:172)
12/02/17 21:06:08 	at org.apache.myfaces.trinidad.webapp.TrinidadFilter.doFilter(TrinidadFilter.java:92)
12/02/17 21:06:08 	at com.evermind.server.http.EvermindFilterChain.doFilter(EvermindFilterChain.java:17)
12/02/17 21:06:08 	at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:392)
12/02/17 21:06:08 	at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:621)
12/02/17 21:06:08 	at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:370)
12/02/17 21:06:08 	at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:871)
12/02/17 21:06:08 	at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:453)
12/02/17 21:06:08 	at com.evermind.server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:221)
12/02/17 21:06:08 	at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:122)
12/02/17 21:06:08 	at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:111)
12/02/17 21:06:08 	at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260)
12/02/17 21:06:08 	at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
12/02/17 21:06:08 	at java.lang.Thread.run(Thread.java:662)
12/02/17 21:06:08 Caused by: javax.transaction.TransactionRolledbackException: 
Exception Description: Cannot use an EntityTransaction while using JTA.; nested exception is: 
	java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.
12/02/17 21:06:08 	at com.evermind.server.ejb.EJBUtils.getUserException(EJBUtils.java:338)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.AbstractTxInterceptor.convertAndHandleMethodException(AbstractTxInterceptor.java:75)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.TxBeanManagedInterceptor.invoke(TxBeanManagedInterceptor.java:55)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:119)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.DMSInterceptor.invoke(DMSInterceptor.java:52)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:119)
12/02/17 21:06:08 	at com.evermind.server.ejb.InvocationContextPool.invoke(InvocationContextPool.java:55)
12/02/17 21:06:08 	at com.evermind.server.ejb.StatelessSessionEJBObject.OC4J_invokeMethod(StatelessSessionEJBObject.java:87)
12/02/17 21:06:08 	at AccountBean_RemoteProxy_120i08f.setAccountBalance(Unknown Source)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
12/02/17 21:06:08 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
12/02/17 21:06:08 	at java.lang.reflect.Method.invoke(Method.java:597)
12/02/17 21:06:08 	at com.evermind.server.rmi.RmiMethodCall.run(RmiMethodCall.java:53)
12/02/17 21:06:08 	... 2 more
12/02/17 21:06:08 Caused by: java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.
12/02/17 21:06:08 	at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.getTransaction(JTATransactionWrapper.java:65)
12/02/17 21:06:08 	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.getTransaction(EntityManagerImpl.java:867)
12/02/17 21:06:08 	at de.fpm.rp.ejb.AccountBean.setAccountBalance(AccountBean.java:197)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
12/02/17 21:06:08 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
12/02/17 21:06:08 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
12/02/17 21:06:08 	at java.lang.reflect.Method.invoke(Method.java:597)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.joinpoint.EJBJoinPointImpl.invoke(EJBJoinPointImpl.java:35)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:119)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.SetContextActionInterceptor.invoke(SetContextActionInterceptor.java:44)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:119)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.DMSInterceptor.invoke(DMSInterceptor.java:52)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:119)
12/02/17 21:06:08 	at com.evermind.server.ejb.interceptor.system.TxBeanManagedInterceptor.invoke(TxBeanManagedInterceptor.java:53)
12/02/17 21:06:08 	... 13 more
 

DanZ

Bekanntes Mitglied
Versuch erstmal wieder alles mit "em.getTransaction()" und "em.joinTransaction()" wieder rauszunehmen(wie gesagt, ich dachte das wäre Java SE - auf einem App Server mit Container Managed Transactions macht das keinen Sinn).

Ansonsten wäre natürlich noch interessant, was genau in Zeile 197 steht... die Zeilennummerierung ist beim pasten deines Codes natürlich nicht erhalten geblieben.
 

Veit

Mitglied
Ich hab die Methode jetzt nochmal nach deinen Vorgaben angepasst (also alles mit em.getTransaction() entfernt. Jetzt funktioniert das Ganze...allerdings, wenn ich nach dem ersten Merge eine Exception werfe, wird nix zurückgerollt. Damit fehlt mir die Buchung auf dem zweiten Konto:

Java:
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public void setAccountBalance(BigDecimal ammount, String process, User user, Location location, String keyFrom, String keyTo)
	{	
		try
		{
			// TODO:PROTOKOLL
			Account accountFrom;
			Account accountTo;


			// get accounts
			accountFrom = this.getAccountByLocation(location.getId(), keyFrom);
			accountTo = this.getAccountByLocation(location.getId(), keyTo);



			
			// set new balance, setup accounttransaction
			if (accountFrom != null)
			{
				accountFrom.setBalance(accountFrom.getBalance().subtract(ammount));
				this.setupForSave(accountFrom);
				this.em.merge(accountFrom);
				throw new Exception();
			}
			
			
			if (accountTo != null)
			{
				accountTo.setBalance(accountTo.getBalance().add(ammount));
				this.setupForSave(accountTo);
				this.em.merge(accountTo);
			}
			em.flush();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
 

DanZ

Bekanntes Mitglied
klar, du hast ja auch ein try catch darum. Deine Exception wird von deiner Methode direkt wieder abgefangen, der Container kriegt davon garnichts mit... dann kann er auch kein rollback machen
 

Veit

Mitglied
gott bin ich doof.

jetzt kommt:

Unerwarteter Fehler - javax.persistence.TransactionRequiredException: Exception Description: No externally managed transaction is currently active for this thread
 

Veit

Mitglied
Kommando zurück...es geht mit deiner genannten Annotation. Ich hab die Exception einfach mal anders ausgelöst...und siehe da: er macht einen Rollback.

Danke! :) Du hast mir das Wochenende gerettet!
 

Ähnliche Java Themen

Neue Themen


Oben