I m new to JSF and primefaces... I m currently working on a JSF project with primefaces wherein i have to access 2 databases A and B . I have create 2 persistent units in persistence.xml as follows
1) is it OK for me to use <non-jta-data-source> ? Shud i change to <jta-data-source> ?
2) Is the content of -ds.xml files correct ?
3) I have created 2 entityManagers( em1 and em2 ) one for each persistent unit. and am using the annotation @PersistenceContext(unitName = "") before my entityManager declaration. In my case when i first use em1 and then try to fire a query using em2 , the query does not get fired and i get "javax.faces.el.EvaluationException: java.lang.IllegalStateException: Transaction not active" error. How to solve this ?
4) Do i have to add any separate configuration file to my project ... During googling i came across one post when it says we have to add "applicationContext.xml" file to our project.
Wait!! You are calling EntityManager directly from your JSF Managed Bean?. In that case this is a JSF question (Sorry Tim).
The core problem is that you are trying to do some modifications in the database without a transaction. I know almost next to nothing about JSF, so I have no idea how transactions are handled in JSF. Does the container automatically weave transaction code for you? Or do you have to explicitly start/stop transaction yourself?
AFAIK. the standard practice is that the JSF Managed Bean should call a service. The service can be an EJB or a Spring loaded bean, and the container that manages the service will weave transaction management code for you. Again, I know nothing about JSF, so I don;t know if the container treats JSF managed beans like EJBs
It is bad practice to put persistence code directly into JSF backing beans. Yes, you can inject an entityManager directly into a backing bean if you are using Spring to wire it (which is what the applicationContext.xml file is for) in AND you have installed the Spring-JSF EL Resolver by configuring it in the faces-config.xml file. But I recommend putting all of the persistence code in a separate service class similar to a Session EJB.
I run multiple tiers. The lowest level is the object model itself. Next up are the DAOs, which handle the individual table finder and update methods. Next up from that are the persistence Services, which use the DAOs to manipulate complex table relationships within a database transaction. This tier is also providing the transaction-bounded business logic, since that usually is closely tied to the object relationships. The Service modules I inject into the JSF backing beans. Only the DAO level uses EntityManager. All database transactions are delimited at the Service methods. A transaction starts when the method is invoked and is terminated when the method returns. I use detached objects within the JSF code itself, as I think that working with attached objects at the UI levels is not only sloppy, but unsafe.
Joined: Jan 02, 2013
Hello all ,
I am not creating the entityManager in the JSF managed bean. I create it in the service class and and all the DB operations are done here in the service class.