2

I have a method using a @Transactional by DeltaSpike, but inside this method i need call a save() and force commit, independent of any exception after. If i use save and after force a expcetion the DeltaSpike rollback everything, like this:

@Transacional
public void method(){
save(myBean);
throw new MyException("THIS IS NECESSARY");
}

So my ideia is call another method to save my bean with another transacion, like this:

    @Transacional
    public void method(){
    save(myBean);
    throw new MyException("THIS IS NECESSARY");
    }

    @Transacional(REQUIRES_NEW)
   public void saveHere(Object bean){save(bean);}

But DeltaSpike don't have attribute REQUIRES_NEW and any other to create a new transacion. How can i do it ?

Ronaldo Lanhellas
  • 2,975
  • 5
  • 46
  • 92

1 Answers1

0

I believe there is no way. Do you use JTA 1.2(Part of Java EE 7)? So you can use @javax.transaction.Transactional. It works with DeltaSpike also.

...
public @interface Transactional {
...

    TxType value() default TxType.REQUIRED;

    public enum TxType {
        REQUIRED,
        REQUIRES_NEW,
        MANDATORY,
        SUPPORTS,
        NOT_SUPPORTED,
        NEVER
    }
}

Try @javax.transaction.Transactional(value=TxType.REQUIRES_NEW)

jklee
  • 2,198
  • 2
  • 15
  • 25
  • I'm not using JTA, just CDI with DeltaSpike. – Ronaldo Lanhellas Feb 14 '17 at 14:47
  • Java EE 7 or spring? The jta dependency belongs More to jpa. – jklee Feb 14 '17 at 14:55
  • Java EE 7, i'm not using Spring. – Ronaldo Lanhellas Feb 14 '17 at 17:29
  • Can i use @Transacional of JTA in DeltaSpike context ? – Ronaldo Lanhellas Feb 14 '17 at 17:53
  • Yes, you can use it. JTA 1.2 is a part Java EE 7. JTA Transactional is almost successor from DeltaSpike Transactional. More differences can you find here https://struberg.wordpress.com/2015/04/21/transaction-and-exception-handling-in-ejbs-and-javaee7-transactional/ – jklee Feb 14 '17 at 18:05
  • So, should i replace DeltaSpike with JTA or use both ? I'm not understand how can i use Transacional from DeltaSpike and Transacional from JTA in same time. – Ronaldo Lanhellas Feb 14 '17 at 18:24
  • I would be in a project only one annotation. I use JTA transactional with DeltaSpike Data und JPA. Both activate "only" a transaction, no magic. So you can use both at various methodes/classes, but not recommend. – jklee Feb 14 '17 at 20:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135720/discussion-between-scientist-and-jklee). – Ronaldo Lanhellas Feb 14 '17 at 20:51