Question

i am creating a web service using 3rd party wsdl that helps me in getting a kind of notification. now i have to save that notification in DB and perform several other operations related to DataBase. in My persistence.xml there are two persistence units as following:

    <persistence-unit name="PU1" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/vsDS</jta-data-source>
    <class>com.Response</class>
    <class>com.Request</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.logging.level" value="INFO"/>
    </properties>
</persistence-unit>


<persistence-unit name="PU2" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/vdDS</jta-data-source>
    <class>com.LogRequest</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.logging.level" value="INFO"/>
    </properties>
</persistence-unit>

till yet i have made a class named Service.java that have all functions i need to perform on DB as following:

public class Service {

private static Logger logger = Logger.getLogger(Service.class);

private EntityManagerFactory PU1;
private EntityManagerFactory PU2;


public Service(){
    System.out.println("in service's constructer");
    PU1=Persistence.createEntityManagerFactory("PU1");
    PU2=Persistence.createEntityManagerFactory("PU2");
}
public void logSubRequest(String msg){
    EntityManager em= PU1.createEntityManager();
    try{
        em.getTransaction().begin();
        Request req=new Request();
        req.setMessage(msg);
        req.setStatus("Y");
        em.persist(req);

        em.getTransaction().commit();
    }catch(Exception e){
        logger.error("In logSubRequest="+e.getMessage()); 
        e.printStackTrace();
    }finally{
        if(em.isOpen()){
            em.close();
        }
    }
}
   // there are several other methods of such kind that either persist entities or executeUpdate
   }

My Question is: Am i going with standard approach to implement JPA with webservice or should i go with some other technology/method. kindly suggest and guide if i am doing any thing out of standard.

Was it helpful?

Solution

The combination of a Web services and JPA is often used, I see no reason to choose some other persistence mechanism. I'd recommend sticking with your current design until your requirements get more complex.

Larger applications where the equivalent of your Request object is more complex and the resulting business logic more challenging will add more layers of objects in order to control the structure and enable reuse.

I tend to think like this: The Web service interface is just one possible "way in" to the my business logic. So I might also have a RESTful service, or a JMS interface. Hence I tend to put my business logic inside an EJB, in simple cases a "No Interface" EJB. I focus on the EJB as a reusable piece of business logic that I can test in isolation. It will do all the JPA work. Then my Web service just uses that EJB to do its work. The EJB looks after the transactions so I don't need to write the transaction control code you have.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top