tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Blevins <david.blev...@visi.com>
Subject Re: Configuring DataSources in a managed environment
Date Mon, 17 Dec 2007 08:13:27 GMT
On Dec 16, 2007, at 10:32 PM, Alexander Saint Croix wrote:

> I am deploying in an embedded OpenEJB environment in Tomcat.


>    ...
>        // inside of the doGet(...) method:
>
>        FirstEntity ent1 = new FirstEntity();
>        ent1.setName("Entity name");
>        ent1.setDescription("Entity description");
>        ent1.setType("Entity type");
>
>        mgr.persist(ent1);
>
>        SecondEntity ent2 = new SecondEntity();
>        ent2.setName("Entity name 2");
>        ent2.setDescription("Entity description 2");
>        ent2.setSecondType("Entity type 2");
>
>        mgr.persist(ent2);


> @Stateless
> public class DefaultBeanManager implements BeanManager {
>
>    @PersistenceUnit(unitName="corm-base")
>    EntityManagerFactory emf;
>
>    @PersistenceUnit(unitName="corm-party")
>    EntityManagerFactory emf2;
>
>    public void persist(FirstEntity a) {
>
>        System.setProperty("openjpa.ConnectionDriverName", "
> com.mysql.jdbc.Driver");
>        System.setProperty("openjpa.ConnectionURL",
> "jdbc:mysql://localhost/corm");
>        System.setProperty("openjpa.jdbc.SynchronizeMappings",
> "buildSchema");
>        System.setProperty("openjpa.ConnectionUserName", "root");
>        System.setProperty("openjpa.ConnectionPassword",  
> "n00p455wyrd");
>
>        EntityManager em = emf.createEntityManager();
>        em.getTransaction().begin();
>        em.persist(a);
>        em.getTransaction().commit();
>        em.close();
>
>    }
>
>    public void persist(SecondEntity a) {
>
>        System.setProperty("openjpa.ConnectionDriverName", "
> com.mysql.jdbc.Driver");
>        System.setProperty("openjpa.ConnectionURL",
> "jdbc:mysql://localhost/corm");
>        System.setProperty("openjpa.jdbc.SynchronizeMappings",
> "buildSchema");
>        System.setProperty("openjpa.ConnectionUserName", "root");
>        System.setProperty("openjpa.ConnectionPassword",  
> "n00p455wyrd");
>
>        EntityManager em = emf2.createEntityManager();
>        em.getTransaction().begin();
>        em.persist(a);
>        em.getTransaction().commit();
>        em.close();
>    }
> }


> <persistence xmlns="http://java.sun.com/xml/ns/persistence"  
> version="1.0">
>    <persistence-unit name="corm-party" transaction- 
> type="RESOURCE_LOCAL">
>    </persistence-unit>
> </persistence>
>
> How should I correctly configure the ConnectionDriverName property for
> multiple persistence units so that these component jars can cooperate?

Definitely do not set ConnectionDriverName at all, likewise for any of  
the OpenJPA properties that smell of "let us make the datasource for  
you".  You need to configure your datasource in Tomcat or OpenEJB  
(either is fine as we sync the environments together), then you *must*  
either a) set these two system properties:

  javax.persistence.jtaDataSource=java:openejb/Resource/myDataSource
  javax.persistence.nonJtaDataSource=java:openejb/Resource/ 
myNonJtaDataSource

or b) set them in each persistence.xml via:
    <persistence-unit name="corm-party">
      <jta-data-source>java:openejb/Resource/myDataSource</jta-data- 
source>
      <non-jta-data-source>=java:openejb/Resource/myNonJtaDataSource</ 
non-jta-data-source>
    </persistence-unit>

The second and more important.  Why do you want to split these Entity  
beans up into so many persitence-units.  Is this a packaging concern  
or is it really your intent to have several completely separate  
caches, each with it's own connection to the database, and each  
managing it's own transaction with no cooperation and visibility to  
the other?  I suspect the answer is no as you have attempted to point  
them all at the same database.  If you start sharing data between them  
you could wind up with some serious data integrity issues.

I can recommend a better approach once I better understand your  
motivation.

-David


Mime
View raw message