1

I have recently migrated to JPA 2.0 (EclipseLink 2.4.2 Juno) in TomEE 1.7 in Eclipse IDE, As i am storing the values they are getting stored and retrieved fine, but they are not persisting into the database when is use entitymanager.flush()it is showing javax.persistence.TransactionRequiredException Here is my code

Create.java (register method)

public static int register(String first, String last, String email, String date, String phone, String address, String pin, Login login) { try { System.out.println("registering persisting the entity"); EntityManagerFactory emf = Persistence .createEntityManagerFactory("FirstEE"); EntityManager manager = emf.createEntityManager(); manager.getTransaction().begin(); // // Query query = manager // .createQuery("select l from Truck l"); Login log = login; System.out.println(log.getUsername() + "username" + log.getPassword() + "password"); User reg = new User(); reg.setLogin(log); reg.setDate(date); reg.setEmail(email); reg.setFirst(first); reg.setLast(last); reg.setPhone(phone); reg.setAddress(address); reg.setPin(pin); manager.persist(reg); manager.getTransaction().commit(); manager.flush(); manager.close(); emf.close(); // FacesContext.getCurrentInstance().addMessage("reg:result", // new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Message", // "Registered Successfully")); FacesContext facesContext = FacesContext.getCurrentInstance(); FacesMessage facesMessage = new FacesMessage( "Registered Successfully"); facesContext.addMessage(null, facesMessage); System.out.println("after message global"); return 1; } catch (Exception e) { System.out.println("hai this is exception caught:" + e); System.out.println("hai" + e.getMessage()); FacesContext.getCurrentInstance().addMessage( "reg:result", new FacesMessage("Something went wrong", "\tSomething went wrong\t")); // FacesContext facesContext = FacesContext.getCurrentInstance(); // FacesMessage facesMessage = new // FacesMessage("Something went wrong"); // facesContext.addMessage(null, facesMessage); } return 0; } 

persistence.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="FirstEE" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <non-jta-data-source>FirstEE</non-jta-data-source> <!-- <exclude-unlisted-classes>false</exclude-unlisted-classes> --> <class>com.jason.Entity.User</class> <class>com.jason.ManagedBean.Login</class> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/yash" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="root" /> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="eclipselink.logging.level" value="FINEST" /> <property name="eclipselink.ddl-generation" value="create-tables" /> </properties> </persistence-unit> </persistence> 

I cant figure out the problem data is getting retreived and stored but it is not updating and not getting persisted in database

6
  • Why do you have both a non-jta-datasource and jdbc properties defined in there? Which connections do you really want the persistence unit to use, and how are you verifying that updating/persisting isn't working? Commented Jul 24, 2015 at 14:01
  • @yashoon - manager.getTransaction().commit(); will flush the changes tothe DB - you don't need to call flush() again. Can you clarify what is not being persisted and what is being stored? Does the reg entity appear in the DB? Commented Jul 24, 2015 at 14:12
  • did you expect the flush() to clear the entitymanager? If so, use clear() Commented Jul 24, 2015 at 15:04
  • 1
    For future questions, please carefully read stackoverflow.com/help/mcve Commented Jul 24, 2015 at 22:02
  • @Chris I am new to JPA i dont the exact difference between JTA and non JTA could you please give me an example how to apply them so that i could have a better idea? and also i don't know where the values are stored. But they are not actually persisting the values in the MySql database Commented Jul 25, 2015 at 4:14

2 Answers 2

1

The commit()method is committing your transaction immediately. Therefore, the changes are writting into the database & your previously open transaction also ends in this moment. When you call flush() afterwards, there is no open transaction so the flush() operation - complains as you experience it - with a javax.persistence.TransactionRequiredException. See also javax.persistence Interface EntityManager#flush()

void flush()

Synchronize the persistence context to the underlying database.

Throws: TransactionRequiredException - if there is no transaction

You should call the commit() method after you synced the state of your EntityManager, like so

manager.getTransaction.begin(); // ... // do something with your entities in between // ... manager.persist(reg); manager.flush(); // in your case: could also be skipped // finally - if nothing fails - we are safe to commit manager.getTransaction().commit(); 

Another hint:

You should avoid to mix your UI-side code (JSF...) with Backend code. This is generally considered 'spaghetti' anti-pattern here.

Moreover, do not open and close EntityManagerFactory every time (!) you call this UI-bound method (register(...)), as this is a performance killer - at least it produces unnecessary processing every time somebody tries to register here. At least, you should create an instance of EntityManagerFactory (or: EntityManager) as a field (via a DBService class) and reuse this to communicate with the application backend.

Sign up to request clarification or add additional context in comments.

6 Comments

@MWiesner as i am new beginner to some extent in programming i don't know the best or even better practices for like patterns and all could you please suggest me any examples to know the best practices for a web application with all the possible patterns
That's a bit hard as I don't know what your current skills are. Yet, here is a link which I think is a good starting point for you to find examples on JPA programming: howtodoinjava.com/jpa-tutorials-and-examples - it lists typical problems/things one wants to achieve with JPA. Still, much depends on the technologies you use in your web application, like for example: 3rd party dependencies of frameworks (Spring/JEE?) or general approach of your architecture. A classic idea of a Java like Web-app is visualized here: howtodoinjava.com/2015/04/17/how-you-should-unit-test-dao-layer
Maybe another good starting point - even though it is a bit old now - is the tutorial of TomEE developers: tomee.apache.org/examples-trunk/jpa-eclipselink/README.html - It is helpful for some ideas and code fragments of a "persistence layer" which is then used from or "injected" into your UI layer to avoid spaghetti structures. Check out the other tutorials there - they will be helpful for you, I guess.
@MWiesner my isn't solved even though i delete the database the code is working and i able to retrieve and store data. any way it is not persisted into the database
@yashoon Still: The TransactionRequiredException is gone, right? If so, this question is answered. Maybe you post a new question with your current code that is not working/persisting...
|
0

The error indicates you cant call flush outside of a transaction. Your code clearly shows you are calling flush right after calling manager.getTransaction().commit(). There is no need to call flush after commit, since the entityManager synchronizes to the database on commit or flush. You only use flush if you have a long running transaction and you want to synchronize at particular stages.

For the rest, turn on EclipseLink logging https://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging and it will show you want it is doing on the commit/flush calls.

1 Comment

I cant figure out why the data is not storing in the database. Even there is no database server running it is still storing the values all i could know application is running independent of the database. :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.