[jboss-user] [EJB 3.0] - One PersistenceUnit, multiple DataSources

7rond do-not-reply at jboss.com
Wed Feb 21 04:28:48 EST 2007


I'm currently working on a EJB3-based application where I have the following project layout of my EAR:

  | -> myproject.ear
  |   -> api.jar
  |        -> dao interfaces and ejb entities
  |   -> ejb.jar
  |        -> ejb3 implementation of the dao interfaces
  |   -> persistence.jar
  |        -> contains persistence.xml and such

This is deployed standalone to JBoss-4.0.5-GA and works as intended. Now, the plan was to have several self-contained webapps (deployed in .wars, or optionally separate .ear's - whatever works), like this:

  | -> webapp1.war
  | -> webapp2.war
  | -> (...)

The wish is that every webapp uses the persistence-manager configured in persistence.xml in persistence.jar, but that each webapp has a separate DataSource so I can let every webapp use the same schema structure and same entity beans, but separate databases. My current persistence.xml looks like this;

  | <?xml version="1.0" encoding="UTF-8"?>
  | <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
  | 	<persistence-unit name="em" transaction-type="JTA">
  | 		<jta-data-source>java:/TestDataSource</jta-data-source>
  | 		<jar-file>../api.jar</jar-file>
  | 		<properties>
  | 			<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
  | 			<property name="hibernate.show_sql" value="true" />
  | 			<property name="hibernate.hbm2ddl.auto" value="update" />
  | 			<property name="jboss.entity.manager.jndi.name" value="java:/TestEM" />
  | 		</properties>
  | 	</persistence-unit>
  | </persistence>

My DAOs get a hold of their entitymanager like this:

  | @Resource(mappedName = "java:/TestEM")
  | private EntityManager em;

What I'm trying to achieve is to more or less set up a new web application with the minimal amount of hassle. One possible way of doing it would perhaps be setting up a new persistence-context in persistence.xml for each new website, but that would require rebuilding and redeploying the entire EAR, which I'd really like to avoid.

I guess one way of getting around this is to deploy the webapp with a "overriding" persistence.xml in a ejb-jar and package it together with the webapp in a separate .ear (though I don't know if this will work either, I haven't tested this yet), but this has some drawbacks as well;
 - I would have to duplicate the persistence-unit-properties and jar-file-properties for each webapp, making maintenance hard if there's a change in the properties there. It would require that each and every webapp is rebuilt and redeployed - not just the EAR.
 - It's a more complex deployment than just dropping in a .war in the deployment folder - though this I can live with.

There will be a -ds.xml in the deploy/ folder for each webapp, containing information about each application's data source. If there would be some way of "overriding" the jta-data-source for the EntityManager on a site-basis, or injecting a new DataSource into the EntityManager, I guess that would be a way of getting this to work.

I'm kind of new to JBoss and EJB3 in general, and I'm trying to work out the "best practice" way of doing things to avoid clutter later down the road. If any of the above is a shitty way of solving what I intend to do, do say so.

Anyways, this separate-db-per-webapp-thing is a requirement for this app, and it's also a requirement that it involves as little work as possible to get a new webapp up and running - e.g. too much rebuilding and redeployment of the application core is out of the question.

Any pointers on which direction I should go in would be very appreciated.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4019781#4019781

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4019781

More information about the jboss-user mailing list