0

I trying understand how EJB JTA works. I have a simple Bean:

@Stateless
public class DictionariesCategoriesFacade extends AbstractFacade<DictionariesCategories> {

     @PersistenceContext(unitName = "org.web_system-ejb_ejb_1.0-SNAPSHOTPU")
     private EntityManager em;    


     @TransactionAttribute(TransactionAttributeType.REQUIRED)
     public void save(DictionariesCategories dictionariesCategory, DictionariesCategories parentCategory, DictionariesCategoriesNames dictionariesCategoriesNames) {

            // here no transaction
            em.merge(dictionariesCategory); // this works
            em.persist(dictionariesCategoriesNames); // here i get constraint from database beacuse dictionariesCategoriesNames dont have set a name value. This value is required (NOT NULL) i do that deliberately.

     }

}

After call this method in my database is persisted first command

    em.merge(dictionariesCategory);

but second is failed. (here i get javax.validation.ConstraintViolationException)

   em.persist(dictionariesCategoriesNames); // throws javax.validation.ConstraintViolationException

I expect that, the PersistenContext will do rollback automatic and first command dont be persisted in database. Why my transaction here dosent work? Can someone help with this issue i will greatfuly for help

here my persisten.xml:

 <?xml version="1.0" encoding="UTF-8"?>
 <persistence version="2.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_2_0.xsd">
    <persistence-unit name="org.web_system-ejb_ejb_1.0-SNAPSHOTPU" transaction-type="JTA">
      <jta-data-source>java:/jboss/datasources/postgresql</jta-data-source>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties/>
    </persistence-unit>
  </persistence>
Michał Ziembiński
  • 1,124
  • 2
  • 10
  • 31
  • 1
    Michał, check this [post](http://stackoverflow.com/questions/26151636/transactions-dont-rollback/26319117#26319117) , if you are using JBoss, it might be a bad `jta` setting in your datasource configuration. – Gas Feb 23 '15 at 11:29
  • 1
    I'm glad it worked. This is a bad setting in JBoss, confuses a lot of people. You can upvote that answer, if it was helpful for you. – Gas Feb 23 '15 at 12:52

1 Answers1

0

following to Gas Answer i change in standalone.xml file datasource line from:

  <datasource jta="false" ..

to

  <datasource jta="true" 

and now Rollback works fine. Thanks again!

Michał Ziembiński
  • 1,124
  • 2
  • 10
  • 31