3 Replies Latest reply on May 16, 2013 11:39 AM by Paul Robinson

    Problem with rollback a transaction

    Jerson Viveros Newbie



      I have a problem. I want to manage a transaction in an EJB method. I prove forcing a rollback, but I dont know why the rollback doesn't work.

      I'm using jboss 7.1.1. And this is the code:





      public class ProfesionalServices {



          @PersistenceContext(unitName = "MHPU")

          private EntityManager em;




          private UserTransaction jts;


          public void create(Profesional entity) throws LlaveDuplicadaException{

              try {


                  entity = em.merge(entity);


                      throw new LlaveDuplicadaException("otra cosa!");


              }catch (Exception e){

                  try {


                  } catch (Exception ex) {



                  throw new LlaveDuplicadaException("");






      I hope that the entity is not saved, but after I check my database, the entity is saved! What could be the problem?, Is necesary any sort of configuration in the server? Can someone notice something wrong in the code?


      Please Help!!

        • 1. Re: Problem with rollback a transaction
          Paul Robinson Master



          From your code above, it looks like it should do as you expect.


          Can you provide your persistence.xml and your datasource configuration (either in your standalone.xml or maybe as a deployable datasource). It's possible that you are not using an XA datasource. In which case, I'm pretty sure the EntityManager will use a local transaction with your database and not participate in the JTA transaction.



          • 2. Re: Problem with rollback a transaction
            Jerson Viveros Newbie

            Thank you for the answer:


            The configuration of datasource is:


            <subsystem xmlns="urn:jboss:domain:datasources:1.0">



                            <datasource jta="false" jndi-name="java:jboss/datasources/PostgreSQLDS" pool-name="PostgreSQLDS" enabled="true" use-ccm="false">





















            And my persitence.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="MHPU" transaction-type="JTA">








            Perhaps is the datasource...  jta=false... Let me check!!

            • 3. Re: Problem with rollback a transaction
              Paul Robinson Master



              Yes, It looks to me like setting jta="true" would cause the datasource to use the JTA implementation, and thus solve your issue.


              If you want to  use multiple resources (e.g. another database or a message server) in the same transaction, you would need to configure an XA datasource. You could take a look at this discussion, It has an example of an XA PostgresSQL DS: https://community.jboss.org/thread/222474.