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

I am developing a dynamic web project (Java EE) using JSF, PrimeFaces, JPA and working 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 a JPA piece of software. In the past, when I developed some little things (like exercises) in Java SE, I used 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 in Java EE.

In Pro JPA 2, Mastering the Perspective API for Java Trading, chapter 3, paragraph β€œPackaging It Up,” you can read:

In Java EE, many of the properties required in the persistence.xml file for Java SE may be omitted. In Listing 3-32, see the persistence.xml file in Listing 2-11, which was converted for deployment as part of a Java EE application. Instead of the JDBC properties to create the connection, we now declare that the manager object should use the data source name "jdbc / EmployeeDS". If the data source was defined as accessible in the application namespace instead of the naming context of local components, then we could instead use the data source name "java: app / jdbc / EmployeeDS". The transaction type attribute has also been removed to default to JTA. The application server will automatically find entity classes, so even the list of classes has been deleted. This example is an ideal minimum Java EE configuration. Since business logic using this persistence block is implemented in a session without a bean state, the persistence.xml file is usually located in the META-INF directory corresponding to the EJB JAR.

Listing 3-32. Defining a Save 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 persistence.xml

 <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 the general persistence.xml file in the Java EE environment to connect to the MySQL / JDBC database using the properties entered at the beginning of the message?

+4
source share
1 answer

This example requires the JTA, Java Transaction API. He delegates transaction management to the container. If JTA is enabled, if you use @Stateless EJB, then one call to the method by default is considered the only complete transaction. This allows you to write clean code without any tx.begin, tx.commit, tx.rollback templates, etc.

Like JSF, EJB, and JPA, JTA is not available by default in the JSP / barebones servlet container like Tomcat and Jetty. Like JSF, EJB, and JPA, you need to install JTA separately on Tomcat.

An alternative is to switch from a JSP / Servlet container to a real Java EE container (web profile) such as Glassfish, JBoss AS, and TomEE. It offers everything right out of the box regarding Java EE. Please note that JBoss AS and TomEE mainly use the Tomcat JSP / Servlet engine under covers.

+4
source

Source: https://habr.com/ru/post/1488532/


All Articles