3 Replies Latest reply on May 16, 2013 11:39 AM by paul.robinson

    Problem with rollback a transaction

      Hello!

       

      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:

       

      @Stateless

      @LocalBean

      @TransactionManagement(TransactionManagementType.BEAN)

      public class ProfesionalServices {

       

       

          @PersistenceContext(unitName = "MHPU")

          private EntityManager em;

        

         

          @Resource

          private UserTransaction jts;

         

          public void create(Profesional entity) throws LlaveDuplicadaException{

              try {

                  jts.begin();

                  entity = em.merge(entity);

                  if(0==0)

                      throw new LlaveDuplicadaException("otra cosa!");

                  jts.commit();

              }catch (Exception e){

                  try {

                     jts.rollback();

                  } catch (Exception ex) {

                      ex.printStackTrace();

                  }

                  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

          Jerson,

           

          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.

           

          Paul.

          • 2. Re: Problem with rollback a transaction

            Thank you for the answer:

             

            The configuration of datasource is:

             

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

                        <datasources>

                            .......

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

                                <connection-url>jdbc:postgresql://localhost:5432/medicaldb1</connection-url>

                                <driver-class>org.postgresql.Driver</driver-class>

                                <driver>postgresql-9.1-901.jdbc4.jar</driver>

                                <security>

                                    <user-name>postgres</user-name>

                                    <password>pgsql</password>

                                </security>

                                <validation>

                                    <validate-on-match>false</validate-on-match>

                                    <background-validation>false</background-validation>

                                </validation>

                                <statement>

                                    <share-prepared-statements>false</share-prepared-statements>

                                </statement>

                            </datasource>

                            ......

                        </datasources>

                    </subsystem>

             

             

            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">

                <jta-data-source>java:/jboss/datasources/PostgreSQLDS</jta-data-source>

                <exclude-unlisted-classes>false</exclude-unlisted-classes>

                <properties/>

              </persistence-unit>

            </persistence>

             

             

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

            • 3. Re: Problem with rollback a transaction
              paul.robinson

              Jerson,

               

              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.

               

              Paul.