Search code examples
jakarta-eetomcatjpajdbcjta

How to adapt persistence.xml file to connect JPA to a JDBC/MySQL database in Java EE environment (Tomcat + JSF)


I am developing a Dynamic web project (Java EE) using JSF, PrimeFaces, JPA, and running on Tomcat 7. The project development is based on http://www.simtay.com/simple-crud-web-application-with-jsf-2-1-primefaces-3-5-maven-and-jpa/
Now I am developing the JPA part of the software. In past, when I developed some little things (as exercises) in Java SE, I used to use the following database properties:

jdbc.drivers=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=password

But now I am learning JPA on Java EE.

In the book "Pro JPA 2 Mastering the Java trade Persistance API", Chapter 3, paragraph "Packaging It Up" you can read:

In the Java EE environment, many properties required in the persistence.xml file for Java SE can be omitted. In Listing 3-32, you see the persistence.xml file from Listing 2-11 converted for deployment as part of a Java EE application. Instead of JDBC properties for creating a connection, we now declare that the entity manager should use the data source name “jdbc/EmployeeDS”. If the data source was defined to be available in the application namespace instead of the local component naming context then we might instead use the data source name of “java:app/jdbc/EmployeeDS”. The transaction- type attribute has also been removed to allow the persistence unit to default to JTA. The application server will automatically find entity classes, so even the list of classes has been removed. This example represents the ideal minimum Java EE configuration. Because the business logic that uses this persistence unit is implemented in a stateless session bean, the persistence.xml file would typically be located in the META-INF directory of the corresponding EJB JAR.

Listing 3-32. Defining a Persistence Unit in Java EE

<persistence>
    <persistence-unit name="EmployeeService">
        <jta-data-source>jdbc/EmployeeDS</jta-data-source>
    </persistence-unit>
</persistence>

Listing 2-11. Elements in the persistence.xml File

<persistence>
    <persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL">
        <class>examples.model.Employee</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527 EmpServDB;create=true"/>
            <property name="javax.persistence.jdbc.user" value="APP"/>
            <property name="javax.persistence.jdbc.password" value="APP"/>
        </properties>
    </persistence-unit>
</persistence>

My question is: how can I adapt a generic persistence.xml file in Java EE environment to connect to a MySQL/JDBC database using the properties I entered at the top of the post?


Solution

  • The given example requires JTA, Java Transaction API. It will delegate the transaction management to the container. With JTA enabled, if you're using a @Stateless EJB, then a single method call counts by default as a single complete transaction. This allows you to write clean code without any tx.begin, tx.commit, tx.rollback, etc boilerplate.

    Like JSF, EJB and JPA, JTA is by default not available on a barebones JSP/Servlet container as Tomcat and Jetty. Like JSF, EJB and JPA, you'd need to install JTA separately on Tomcat.

    An alternative is to switch from a JSP/Servlet container to a real Java EE (web profile) container, such as Glassfish, JBoss AS and TomEE. It offers everything directly out the box with regard to Java EE. Note that JBoss AS and TomEE basically use Tomcat's JSP/Servlet engine under the covers.