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:
- 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)
- 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 ?
- 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.