user1050134
user1050134

Reputation: 582

Correct usage of JPA in jsp application

I'm trying to develop a simple JSP based web application with JPA and would like to know the correct usage for developing one.

In my sample application I have two JSP pages and a simple Java class to perform database operations. Both the JSP files use this Java class to perform DB operations.

I've annotated this class with @Stateless and injected an Entity manager as follows:

@PersistenceContext(unitName = "myjpa") 
EntityManager em;

In my persistence.xml I've set the following property:

<property 
    name="hibernate.transaction.jta.platform"
    value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"
/>

I'm calling the class in JSP using JNDI (as the class is annotated for a stateless session bean) as follows:

InitialContext ic = new InitialContext();

Sample sample = (Sample) ic.lookup("java:app/" + application.getContextPath() + "/Sample");

I'm facing the following scenarios:

  1. When I try to use a transaction em.getTransaction().begin()/commit() for insert and update, it says can not use transaction with JTA case.

  2. So in the constructor code of my Java class I use the following code:

Properties properties = new Properties();

properties.put("javax.persistence.transactionType", "RESOURCE_LOCAL");

emf = Persistence.createEntityManagerFactory("myjpa",properties);

em = emf.createEntityManager();

I tried to use transactions like em.getTransaction().begin()/commit(). But in this case the pages become very slow after 2-3 database update and load operations. Though I'm not getting any exception. Overall in my table I'm having less than 25 records.

To me it seems as if it is waiting internally for some operation to complete.

At the same time I also feel that the way I'm using JPA is wrong and hence soliciting advice for the correct approach for doing even simple web apps with JSP and JPA.

While I'm still exploring Java EE, in case you have any specific reference for such cases I'll like to read and look them too.

Upvotes: 1

Views: 4297

Answers (2)

Arjan Tijms
Arjan Tijms

Reputation: 38163

At the same time I also feel that the way I'm using JPA is wrong

Your usage indeed seems wrong. If you're using a (stateless) session bean you do not have to fiddle with em.getTransaction().begin()/commit() and you definitely don't have to use code such as Persistence.createEntityManagerFactory.

You also don't have to set the property org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform.

A session bean automatically manages the transaction for you, and within a Java EE AS (such as JBoss AS) you don't have to configure any transaction manager or similar things.

An example:

@Stateless
public class UserDAO {

    @PersistenceContext
    private EntityManager entityManager;

    public void add(User user) {
        entityManager.persist(user);
    }
}

As for the persistence.xml file, something like the following should be enough to get started:

<?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="somePU">
        <jta-data-source>java:app/someDS</jta-data-source>
    </persistence-unit>
</persistence>

Some more examples:

Upvotes: 1

dinukadev
dinukadev

Reputation: 2297

You should always strive to use JTA transactions which means the container will handle the transaction demarcations. In your case if you want to handle transactions by your self, you need to define it as a bean managed transaction. So in your EJB class, after the @Stateless annoattions, you should define the following annotation;

@TransactionManagement(TransactionManagementType.BEAN)  

The usual best practice is to let the container handle the transactions, unless there is some explicit reason for you to use Bean managed transactions.

Upvotes: 1

Related Questions