0

I have an enterprise Web Application, let's call it AppOne, containing multiple WARs and one EJB, that uses JPA 2 to perform CRUD operations on a set of Entities.

Now I need to build another enterprise Web Application, let's call it AppTwo, with a similar structure (EAR containing n WARS + EJB) that will need to share the same entities and database of AppOne.

Both AppOne and AppTwo will reside on the same application server.

I'm using JBoss 7, Hibernate 4 as JPA 2 provider, my entities are auto scanned.

My persistence.xml is very small:

<?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="FoobarService"  transaction-type="JTA">
      <jta-data-source>java:jboss/datasources/foobarDS</jta-data-source>      
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
         <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
         <property name="hibernate.show_sql"    value="true" />
         <property name="format_sql"            value="true" />
         <property name="use_sql_comments"      value="true" />      
         <!-- when using type="yes_no" for booleans, the line below allows booleans in HQL expressions: -->
         <property name="hibernate.query.substitutions" value="true 'Y', false 'N'"/>
      </properties>
   </persistence-unit>
</persistence>

I was thinking to "clone" (read: copy&paste) the part of AppOne's EJB containing the Entities and the relevant DAO parts into AppTwo's EJB, but since I've never done this with JPA, I'm wondering if this is possible/advisable (or which are the alternatives, considering that AppOne is already deployed in production so I can't take its EJB out to make it a shared one), and specifically if:

  1. Can both AppOne and AppTwo Persistence Units cohexist ? (I've read of problems but I'm not sure if they're related to this or if they've been resolved meanwhile)
  2. How will the transactions be handled ? I mean, if two different PU from two different EJBs are trying to update the same entity at the same time, what happens ?
  3. Can you think of any problem with this approach ? Apart from the obvious redundancy that would force me to update twice the files in case of future entities/database modifications.
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • The biggest issue you will find with jpa is that one application updates may not be reflected on the other applications (especially if this happened from different servers to the same db) unless you force refresh manually – maress Sep 17 '14 at 13:58
  • @AndreiI, feel free to add it as an answer, I'll upvote any useful hint ;) – Andrea Ligios Sep 17 '14 at 14:20
  • @maress : feel free to add it as an answer too, and please expand the concept of manual refreshing... do you mean that if AppOne reads data, AppTwo alters it, App A can't see the new state even after re-reading the data ? – Andrea Ligios Sep 17 '14 at 14:22
  • @maress probably you mean that the changes are not reflected in same moment in a live transaction of the other application. Otherwise, if you do a fetch from DB, you will see the changes made by the other aplication. – V G Sep 17 '14 at 14:37
  • @AndreiI thanks for the rare cavalry shown, btw don't worry and feel free to post it when you wish – Andrea Ligios Sep 17 '14 at 14:44
  • @AndreaLigios could you share your experience with this problem? So that other people with the same question get more details? – V G Oct 02 '14 at 07:31
  • Of course, once I'll have started this thing... The project is being delayed multiple times due to others works with higher priority... Meanwhile, I've accepted your answer :) – Andrea Ligios Oct 02 '14 at 07:49

1 Answers1

1

I never tried it, but I think it should be no (big) problem.

  1. Yes, they can coexist. E.g if for every deployment a separated ClassLoader is used.
  2. Transactions from each application will NOT know of each other. It will be the same as two users would click one after another on the same button.
  3. I personally don't see any problems. If your server is strange configured, you will have to configure JBoss to isolate every application.
V G
  • 18,822
  • 6
  • 51
  • 89