Using TopLink persistence RI in Tomcat

New EJB 3.0 persistence can be used in web module as well but all samples show this new feature in JavaEE5 projects. I assume that many users want to use this cool feature in web projects that have J2EE 1.4 version. I would like to show in this post how to use persistence in web project that is deployed in Tomcat. Of course, on the Tomcat we can't use container managed entity manager. Therefore, the application managed EM is used in this sample. You can use JTA entity managers or resource-local entity manager. Since the Tomcat doesn't support JTA we can use only resource-local entity manager. Let's create new web project with J2EE 1.4 specification and target server Tomcat in NetBeans 5.5. Don't forget that project should have defined 1.5 jdk as platform and we should change source level to 1.5. Of course, the Tomcat server must run on jdk 1.5 as well. Add JavaEE5 library and JDBC driver on project's classpath.Then, add persistence unit and choose application managed entity manager and generate entity classes from database or create them from scratch. In this post is presented using entity manager in servlet but similar approch might be used in JSF managed bean or in class too. The Entity manager might be used as follows:
    EntityManagerFactory emF = Persistence.createEntityManagerFactory("Advertisement");
        EntityManager  em = emF.createEntityManager();
        try{
            em.getTransaction().begin();
            List advertisements =
                 em.createQuery("SELECT a FROM Advertisement a").getResultList();
            for (Advertisement elem : advertisements) {
                out.println(elem.getDescription() +  ", " + elem.getId() + "
"); } em.getTransaction().commit(); }catch(Exception ex) { em.getTransaction().rollback(); }finally { em.close(); }
Build and run project. This works on Tomcat but I encountered security issues on Sun Application server 8.2. The workaround is that you should grant permissions for web module in server.policy. Add following lines in this file:
    permission java.lang.RuntimePermission  "createClassLoader";
    permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
  
Now, you can use Toplink persistence in existing web projects instead of Hibernate :-).
Comments:

Very intriguing- excellent information here. Would you be able to list what Jar files from the "JavaEE5" library are required to be able to run this in a stand-alone tomcat outside the IDE?

Posted by D. J. on April 05, 2006 at 10:32 PM CEST #

Good to know that Java Persistence API can be used in Tomcat. It is worth clarifying that:

In Tomcat, the web app can use Java Persistence API as if it were a Java application using Java Persistence API in \*Java SE\* environment. That means, \*no injection\* of emf or em. It is already mentioned in the blog that container managed em can not be used and so is JTA entity manager. More over, all the persistence classes need to be enumerated in persistence.xml file. I hope NetBeans takes care of this last requirement.

Some questions:

1. Why do I need Java EE 5 libraries when javax.persistence classes are part of toplink-essentials.jar?

2. It is not clear where are the toplink classes? Is toplink-essentials.jar kept in war/WEB-INF/lib? Or is Tomcat started with toplink-essentaisl-agent.jar as javaagent? If Tomcat is not started with javaagent, then lazy loading of 1:1 relationships may not be possible using TopLink.

Thanks, Sahoo

Posted by Sahoo on April 06, 2006 at 01:37 AM CEST #

Hi Sahoo, I meant by JavEE library the predefined library name in NetBeans. This name was changed to persistence library in last builds. I added librar in WEB-INF/lib directory. Thanks for noticing the issue with javaagent.

Posted by pblaha on April 13, 2006 at 03:21 PM CEST #

EJB3 and TopLink on Tomcat 5.5

Where should I put the persistence.xml file? I have tried WEB-INF, and other locations, but all I get is:

060424 141148 10 SEVERE StandardWrapper.Throwable javax.persistence.PersistenceException: No Persistence provider for EntityManager named persistence

I have added the ‘javaagent’ to the JVM:

-javaagent:C:/Sun/AppServer/lib/toplink-essentials-agent.jar

Regards, nps

Posted by Nps on April 24, 2006 at 07:20 AM CEST #

EJB3 and TopLink on Tomcat 5.5

Where should I put the persistence.xml file? I have tried WEB-INF, and other locations, but all I get is:

060424 141148 10 SEVERE StandardWrapper.Throwable javax.persistence.PersistenceException: No Persistence provider for EntityManager named persistence

I have added the ‘javaagent’ to the JVM:

-javaagent:C:/Sun/AppServer/lib/toplink-essentials-agent.jar

Regards, nps

Posted by Nps on April 24, 2006 at 07:21 AM CEST #

Put the persistence.xml file in WEB-INF/classes/META-INF directory. More details about packaging is in Persistence specification, section 6.2.

Posted by PetrB on April 26, 2006 at 08:25 AM CEST #

Post a Comment:
Comments are closed for this entry.
About

pblaha

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks