0

I am using JTA and my persistence.xml is as follows

<?xml version="1.0" encoding="UTF-8" ?>
<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" version="1.0"
             xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="test" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/testDS</jta-data-source>
    <class>test.entity.Employees</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.target-server" value="OC4J"/>
      <property name="javax.persistence.target-database" value="Oracle"/>
      <property name="javax.persistence.logging.parameters" value="TRUE"/>
    </properties>
  </persistence-unit>
</persistence>

As I am using JTA, I do not have EntityTransaction.

Java code snippet

    Employees emp = new Employees();
    emp.setEmpNumber(empPOJO.getEmpNumber());
    final EntityManager em = getEntityManager();
    try {           
            em.merge(vehicleInspection);            
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

However I am not able to persist changes in database.Is it because of not having a commit?

Jacob
  • 14,463
  • 65
  • 207
  • 320
  • 1
    Are you calling this from a transactional method? Also what is with the nested `try/finally`? – M. Deinum Sep 09 '15 at 11:22
  • @M.Deinum What do you mean by transactional method? – Jacob Sep 09 '15 at 11:28
  • Is this done through a test code. If so they get rollback. So you would not see when you query later – Sajith Silva Sep 09 '15 at 11:29
  • @SajithSilva I am not using any test code. – Jacob Sep 09 '15 at 11:31
  • 2
    This code needs to be called in a method that is transactional, it has to have a `@Transactional` or `@TransactionAttributes` annotations. If this is in an EJB (is it?) then it should be transactional by default (if I recall correctly). – M. Deinum Sep 09 '15 at 11:33
  • @M.Deinum I am using EJB, I have added TransactionAttribute to method, still not able to persist data. – Jacob Sep 09 '15 at 11:37
  • Can you add the whole method? As it doesn't make sense right now, you are creating a `new Employee()` but aren't using that anywere else? You are merging a `vehicleInspection`? What is `getEntityManager` doing? You should be using `@PesistenceContext` to inject a transactional entity manager and not creating your own instances. – M. Deinum Sep 09 '15 at 11:40
  • Add catch block to your code to see if merge method throws any exceptions. If I would be guessing, I would say, that merging fails – Tinki Sep 09 '15 at 11:45
  • @Tinki I have added catch to tbe block and no errors are throwing for merge statement. – Jacob Sep 09 '15 at 11:58
  • Merge/persist do little more then register the entity with the context. If you want SQL issued to the database, you need to commit the transaction (this is controlled by JTA semantics of the app) or force the SQL to be issued by calling em.flush(). Flush will cause exceptions if the entityManager is not joined to a JTA transaction, and you haven't shown how you are getting the EntityManager so it might need more configuration – Chris Sep 09 '15 at 14:19
  • You might want to see some of the examples for your server available here http://wiki.eclipse.org/EclipseLink/Examples/JPA – Chris Sep 09 '15 at 14:22

1 Answers1

1

With transactions enabled you have to handle them properly. Check this wiky and JPA sample code for more details.

Tomas Kraus
  • 466
  • 2
  • 6