You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Karsten Ohme <ko...@mms-dresden.de> on 2009/05/13 13:55:10 UTC

Using Multiple PersistenceUnits

Hi,

I quote from the spec:

EJB Persistence Spec 6.2:

A persistence unit must have a name. Only one persistence unit of any
given name may be defined
within a single EJB-JAR file, within a single WAR file, within a single
application client jar, or within
an EAR (in the EAR root or lib directory). See Section 6.2.2,
“Persistence Unit Scope”.

So, it is not possible to define multiple persistence units?

But I still tried it with OpenEJB:

> > @Stateless
> > public class HistoryBLWLServiceImpl implements HistoryBLWLService {
> >
> > @PersistenceContext(unitName = "HistoryBLWLCalldataServices")
private EntityManager entityManagerCalldata;

@PersistenceContext(unitName = "HistoryBLWLCockpitServices")
private EntityManager entityManagerCockpit;
...

persistence.xml:

...
<persistence-unit name="HistoryBLWLCalldataServices"
transaction-type="JTA">
<jta-data-source>monaco/datasource/calldata</jta-data-source>

<non-jta-data-source>monaco/datasource/calldata-unmanaged</non-jta-data-source>


<class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.KEP</class>


<class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.KEPHistory</class>

<class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.CallerHistory</class>

<class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.BonusPoints</class>

<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
<persistence-unit name="HistoryBLWLCockpitServices"
transaction-type="JTA">
<jta-data-source>monaco/datasource/cockpit</jta-data-source>

 <non-jta-data-source>monaco/datasource/cockpit-unmanaged</non-jta-data-source>

<class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.ActionType</class>


<class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.KEPAction</class>

 <exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
...

OpenEJB Config:
>
> > ...
> > <Resource id="HistoryBLWLCalldataServices" type="javax.sql.DataSource">
> >   JdbcDriver com.mysql.jdbc.Driver
> >   JdbcUrl
> jdbc:mysql://wum08071.mms-dresden.de:3307/ssc_monaco_0_9_calldata_test
> >   UserName cgsdb
> >   Password cgsdb
> >   JtaManaged true
> > </Resource>
> > <Resource id="HistoryBLWLCockpitServices" type="javax.sql.DataSource">
> >   JdbcDriver com.mysql.jdbc.Driver
> >   JdbcUrl
> jdbc:mysql://wum08071.mms-dresden.de:3307/ssc_monaco_0_9_cockpit_test
> >   UserName cgsdb
> >   Password cgsdb
> >   JtaManaged true
> > </Resource>
> > ...
>

Log:

>
> > Configuring Service(id=HistoryBLWLCalldataServices, type=Resource,
> provider-id=Default JDBC Database)
> > Configuring Service(id=HistoryBLWLCockpitServices, type=Resource,
> provider-id=Default JDBC Database)
> > Found EjbModule in classpath:
> D:\cgm\monaco_0.9.0\workspace\monaco\impl\test\history_bl_wl\service\target\classes
>
> > Found EjbModule in classpath:
> D:\repository\de\mms_dresden\test.ejb.openejb\0.0.3\test.ejb.openejb-0.0.3.jar
>
> > Beginning load:
> D:\cgm\monaco_0.9.0\workspace\monaco\impl\test\history_bl_wl\service\target\classes
>
> > Beginning load:
> D:\repository\de\mms_dresden\test.ejb.openejb\0.0.3\test.ejb.openejb-0.0.3.jar
>
> > Configuring enterprise application: classpath.ear
> > Configuring Service(id=Default Stateless Container, type=Container,
> provider-id=Default Stateless Container)
> > Auto-creating a container for bean classes/HistoryBLWLServiceImpl:
> Container(type=STATELESS, id=Default Stateless Container)
> > Configuring PersistenceUnit(name=HistoryBLWLCalldataServices)
> > Auto-creating a Resource with id 'HistoryBLWLCalldataServicesNonJta'
> of type 'DataSource for 'HistoryBLWLCalldataServices'.
> > Configuring Service(id=HistoryBLWLCalldataServicesNonJta,
> type=Resource, provider-id=HistoryBLWLCalldataServices)
> > Adjusting HistoryBLWLCalldataServices <jta-data-source> to
> 'HistoryBLWLCalldataServices'
> > Adjusting HistoryBLWLCalldataServices <non-jta-data-source> to
> 'HistoryBLWLCalldataServicesNonJta'
> > Configuring PersistenceUnit(name=HistoryBLWLCockpitServices)
> > Adjusting HistoryBLWLCockpitServices <jta-data-source> to
> 'HistoryBLWLCalldataServices'
> > Adjusting HistoryBLWLCockpitServices <non-jta-data-source> to
> 'HistoryBLWLCalldataServicesNonJta'

The last two lines are confusing.

> When I make a call:
>
> > JDBCExceptionReporter: Table
> 'ssc_monaco_0_9_calldata_test.test_kep_action' doesn't exist
>

What's the problem?

BR,
Karsten

Re: Using Multiple PersistenceUnits

Posted by Karsten Ohme <ko...@mms-dresden.de>.
Hi David,

Thanks a lot.

Your idea of adding a hint in the log output would be nice, e.g.:

 - Adjusting Persistence Unit 'HistoryBLWLCockpitServices'
<jta-data-source> to Resource ID 'HistoryBLWLCalldataServices'

Thanks,
Karsten

Your idea of naming
David Blevins schrieb:
> Hi Karsten,
> 
> You can indeed have as many persistence-unit declarations in your
> persistence.xml file as you want.  The spec section you quote is phrased
> perhaps in a way that doesn't make it clear what can be done.
> 
> Long story short, the rules for persistence-unit names in the
> persistence.xml are the same for ejb-names in the ejb-jar.xml:
> 
>   1. you can have an unlimited number of persistence.xml files, one per
> module or jar.
>   2. you can have as many persistence-units as you want in the
> persistence.xml
>   3. persistence-unit names must be unique inside the persistence.xml
> (i.e. using the same name in a different persistence.xml is fine)
> 
>  - - -
> 
> Regarding the configuration.  The problem is simply that the datasource
> names in your persistence.xml do not match the names you have your
> datasources.  When there's just one datasource configured we assume that
> is the one you want.  When there is more than one configured, we need
> more of a hint.  Without the hint we give you the first datasource you
> have configured in your openejb.xml file.
> 
> Based on the names in your persistence.xml:
>   <jta-data-source>monaco/datasource/calldata</jta-data-source>
>  
> <non-jta-data-source>monaco/datasource/calldata-unmanaged</non-jta-data-source>
> 
>   ...
>   <jta-data-source>monaco/datasource/cockpit</jta-data-source>
>  
> <non-jta-data-source>monaco/datasource/cockpit-unmanaged</non-jta-data-source>
> 
> 
> A configuration like this will do the trick:
> 
>   <Resource id="calldata" type="javax.sql.DataSource">
>      //.. same as before
>   </Resource>
>   <Resource id="cockpit" type="javax.sql.DataSource">
>      //.. same as before
>   </Resource>
> 
> Note we're pretty flexible on the names.  You could call them
> "monaco/datasource/calldata" and "monaco/datasource/cockpit" as well.
> 
>  - - -
> 
> Regarding the log message:
> 
>  - Adjusting HistoryBLWLCockpitServices <jta-data-source> to
> 'HistoryBLWLCalldataServices'
>  - Adjusting HistoryBLWLCockpitServices <non-jta-data-source>
> to'HistoryBLWLCalldataServicesNonJta'
> 
> Let's pretend your persistence unit names ended in "PU" and your data
> source names ended in "DS", that message would have looked like this:
> 
>  - Adjusting HistoryBLWLCockpitServicesPU <jta-data-source> to
> 'HistoryBLWLCalldataServicesDS'
>  - Adjusting HistoryBLWLCockpitServicesPU <non-jta-data-source>
> to'HistoryBLWLCalldataServicesDSNonJta'
> 
> That likely would have been a lot clearer.  Definitely with the units
> and datasources using the same name this particular message is indeed
> confusing.  Maybe we can strategically add the words 'unit' and
> 'Resource' in there to keep things clear even in this scenario.
> 
> On the idea of naming the datasources and the persistence units the
> same, that certainly is an interesting way to do implicit matching.  We
> could probably add support for that as a fallback when matching the
> names in the <jta-data-source> and <non-jta-data-source> doesn't turn up
> anything.
> 
> 
> -David
> 
> On May 13, 2009, at 4:55 AM, Karsten Ohme wrote:
> 
>> Hi,
>>
>> I quote from the spec:
>>
>> EJB Persistence Spec 6.2:
>>
>> A persistence unit must have a name. Only one persistence unit of any
>> given name may be defined
>> within a single EJB-JAR file, within a single WAR file, within a single
>> application client jar, or within
>> an EAR (in the EAR root or lib directory). See Section 6.2.2,
>> “Persistence Unit Scope”.
>>
>> So, it is not possible to define multiple persistence units?
>>
>> But I still tried it with OpenEJB:
>>
>>>> @Stateless
>>>> public class HistoryBLWLServiceImpl implements HistoryBLWLService {
>>>>
>>>> @PersistenceContext(unitName = "HistoryBLWLCalldataServices")
>> private EntityManager entityManagerCalldata;
>>
>> @PersistenceContext(unitName = "HistoryBLWLCockpitServices")
>> private EntityManager entityManagerCockpit;
>> ...
>>
>> persistence.xml:
>>
>> ...
>> <persistence-unit name="HistoryBLWLCalldataServices"
>> transaction-type="JTA">
>> <jta-data-source>monaco/datasource/calldata</jta-data-source>
>>
>> <non-jta-data-source>monaco/datasource/calldata-unmanaged</non-jta-data-source>
>>
>>
>>
>> <class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.KEP</class>
>>
>>
>>
>> <class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.KEPHistory</class>
>>
>>
>> <class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.CallerHistory</class>
>>
>>
>> <class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.BonusPoints</class>
>>
>>
>> <exclude-unlisted-classes>true</exclude-unlisted-classes>
>> </persistence-unit>
>> <persistence-unit name="HistoryBLWLCockpitServices"
>> transaction-type="JTA">
>> <jta-data-source>monaco/datasource/cockpit</jta-data-source>
>>
>> <non-jta-data-source>monaco/datasource/cockpit-unmanaged</non-jta-data-source>
>>
>>
>> <class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.ActionType</class>
>>
>>
>>
>> <class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.KEPAction</class>
>>
>>
>> <exclude-unlisted-classes>true</exclude-unlisted-classes>
>> </persistence-unit>
>> ...
>>
>> OpenEJB Config:
>>>
>>>> ...
>>>> <Resource id="HistoryBLWLCalldataServices" type="javax.sql.DataSource">
>>>>  JdbcDriver com.mysql.jdbc.Driver
>>>>  JdbcUrl
>>> jdbc:mysql://wum08071.mms-dresden.de:3307/ssc_monaco_0_9_calldata_test
>>>>  UserName cgsdb
>>>>  Password cgsdb
>>>>  JtaManaged true
>>>> </Resource>
>>>> <Resource id="HistoryBLWLCockpitServices" type="javax.sql.DataSource">
>>>>  JdbcDriver com.mysql.jdbc.Driver
>>>>  JdbcUrl
>>> jdbc:mysql://wum08071.mms-dresden.de:3307/ssc_monaco_0_9_cockpit_test
>>>>  UserName cgsdb
>>>>  Password cgsdb
>>>>  JtaManaged true
>>>> </Resource>
>>>> ...
>>>
>>
>> Log:
>>
>>>
>>>> Configuring Service(id=HistoryBLWLCalldataServices, type=Resource,
>>> provider-id=Default JDBC Database)
>>>> Configuring Service(id=HistoryBLWLCockpitServices, type=Resource,
>>> provider-id=Default JDBC Database)
>>>> Found EjbModule in classpath:
>>> D:\cgm\monaco_0.9.0\workspace\monaco\impl\test\history_bl_wl\service\target\classes
>>>
>>>
>>>> Found EjbModule in classpath:
>>> D:\repository\de\mms_dresden\test.ejb.openejb\0.0.3\test.ejb.openejb-0.0.3.jar
>>>
>>>
>>>> Beginning load:
>>> D:\cgm\monaco_0.9.0\workspace\monaco\impl\test\history_bl_wl\service\target\classes
>>>
>>>
>>>> Beginning load:
>>> D:\repository\de\mms_dresden\test.ejb.openejb\0.0.3\test.ejb.openejb-0.0.3.jar
>>>
>>>
>>>> Configuring enterprise application: classpath.ear
>>>> Configuring Service(id=Default Stateless Container, type=Container,
>>> provider-id=Default Stateless Container)
>>>> Auto-creating a container for bean classes/HistoryBLWLServiceImpl:
>>> Container(type=STATELESS, id=Default Stateless Container)
>>>> Configuring PersistenceUnit(name=HistoryBLWLCalldataServices)
>>>> Auto-creating a Resource with id 'HistoryBLWLCalldataServicesNonJta'
>>> of type 'DataSource for 'HistoryBLWLCalldataServices'.
>>>> Configuring Service(id=HistoryBLWLCalldataServicesNonJta,
>>> type=Resource, provider-id=HistoryBLWLCalldataServices)
>>>> Adjusting HistoryBLWLCalldataServices <jta-data-source> to
>>> 'HistoryBLWLCalldataServices'
>>>> Adjusting HistoryBLWLCalldataServices <non-jta-data-source> to
>>> 'HistoryBLWLCalldataServicesNonJta'
>>>> Configuring PersistenceUnit(name=HistoryBLWLCockpitServices)
>>>> Adjusting HistoryBLWLCockpitServices <jta-data-source> to
>>> 'HistoryBLWLCalldataServices'
>>>> Adjusting HistoryBLWLCockpitServices <non-jta-data-source> to
>>> 'HistoryBLWLCalldataServicesNonJta'
>>
>> The last two lines are confusing.
>>
>>> When I make a call:
>>>
>>>> JDBCExceptionReporter: Table
>>> 'ssc_monaco_0_9_calldata_test.test_kep_action' doesn't exist
>>>
>>
>> What's the problem?
>>
>> BR,
>> Karsten
>>
> 
> 
> 


-- 
Karsten Ohme
T-Systems Multimedia Solutions GmbH
Portal Technologies, Applications & Appliances
Hausanschrift: Riesaer Strasse 5, 01129 Dresden
Postanschrift: Postfach 10 02 24, 01072 Dresden
Telefon: +49 351 28 20 - 2123
Mobil: +49 171 553 31 27
E-Mail: karsten.ohme@t-systems.com

Re: Using Multiple PersistenceUnits

Posted by David Blevins <da...@visi.com>.
Hi Karsten,

You can indeed have as many persistence-unit declarations in your  
persistence.xml file as you want.  The spec section you quote is  
phrased perhaps in a way that doesn't make it clear what can be done.

Long story short, the rules for persistence-unit names in the  
persistence.xml are the same for ejb-names in the ejb-jar.xml:

   1. you can have an unlimited number of persistence.xml files, one  
per module or jar.
   2. you can have as many persistence-units as you want in the  
persistence.xml
   3. persistence-unit names must be unique inside the persistence.xml  
(i.e. using the same name in a different persistence.xml is fine)

  - - -

Regarding the configuration.  The problem is simply that the  
datasource names in your persistence.xml do not match the names you  
have your datasources.  When there's just one datasource configured we  
assume that is the one you want.  When there is more than one  
configured, we need more of a hint.  Without the hint we give you the  
first datasource you have configured in your openejb.xml file.

Based on the names in your persistence.xml:
   <jta-data-source>monaco/datasource/calldata</jta-data-source>
   <non-jta-data-source>monaco/datasource/calldata-unmanaged</non-jta- 
data-source>
   ...
   <jta-data-source>monaco/datasource/cockpit</jta-data-source>
   <non-jta-data-source>monaco/datasource/cockpit-unmanaged</non-jta- 
data-source>

A configuration like this will do the trick:

   <Resource id="calldata" type="javax.sql.DataSource">
      //.. same as before
   </Resource>
   <Resource id="cockpit" type="javax.sql.DataSource">
      //.. same as before
   </Resource>

Note we're pretty flexible on the names.  You could call them "monaco/ 
datasource/calldata" and "monaco/datasource/cockpit" as well.

  - - -

Regarding the log message:

  - Adjusting HistoryBLWLCockpitServices <jta-data-source> to  
'HistoryBLWLCalldataServices'
  - Adjusting HistoryBLWLCockpitServices <non-jta-data-source>  
to'HistoryBLWLCalldataServicesNonJta'

Let's pretend your persistence unit names ended in "PU" and your data  
source names ended in "DS", that message would have looked like this:

  - Adjusting HistoryBLWLCockpitServicesPU <jta-data-source> to  
'HistoryBLWLCalldataServicesDS'
  - Adjusting HistoryBLWLCockpitServicesPU <non-jta-data-source>  
to'HistoryBLWLCalldataServicesDSNonJta'

That likely would have been a lot clearer.  Definitely with the units  
and datasources using the same name this particular message is indeed  
confusing.  Maybe we can strategically add the words 'unit' and  
'Resource' in there to keep things clear even in this scenario.

On the idea of naming the datasources and the persistence units the  
same, that certainly is an interesting way to do implicit matching.   
We could probably add support for that as a fallback when matching the  
names in the <jta-data-source> and <non-jta-data-source> doesn't turn  
up anything.


-David

On May 13, 2009, at 4:55 AM, Karsten Ohme wrote:

> Hi,
>
> I quote from the spec:
>
> EJB Persistence Spec 6.2:
>
> A persistence unit must have a name. Only one persistence unit of any
> given name may be defined
> within a single EJB-JAR file, within a single WAR file, within a  
> single
> application client jar, or within
> an EAR (in the EAR root or lib directory). See Section 6.2.2,
> “Persistence Unit Scope”.
>
> So, it is not possible to define multiple persistence units?
>
> But I still tried it with OpenEJB:
>
>>> @Stateless
>>> public class HistoryBLWLServiceImpl implements HistoryBLWLService {
>>>
>>> @PersistenceContext(unitName = "HistoryBLWLCalldataServices")
> private EntityManager entityManagerCalldata;
>
> @PersistenceContext(unitName = "HistoryBLWLCockpitServices")
> private EntityManager entityManagerCockpit;
> ...
>
> persistence.xml:
>
> ...
> <persistence-unit name="HistoryBLWLCalldataServices"
> transaction-type="JTA">
> <jta-data-source>monaco/datasource/calldata</jta-data-source>
>
> <non-jta-data-source>monaco/datasource/calldata-unmanaged</non-jta- 
> data-source>
>
>
> < 
> class>de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.KEP</ 
> class>
>
>
> < 
> class 
> > 
> de 
> .mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.KEPHistory</ 
> class>
>
> < 
> class 
> > 
> de 
> .mms_dresden 
> .cgm_platt.monaco.api.test.history_bl_wl.vo.CallerHistory</class>
>
> < 
> class 
> > 
> de 
> .mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.BonusPoints</ 
> class>
>
> <exclude-unlisted-classes>true</exclude-unlisted-classes>
> </persistence-unit>
> <persistence-unit name="HistoryBLWLCockpitServices"
> transaction-type="JTA">
> <jta-data-source>monaco/datasource/cockpit</jta-data-source>
>
> <non-jta-data-source>monaco/datasource/cockpit-unmanaged</non-jta- 
> data-source>
>
> < 
> class 
> > 
> de 
> .mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.ActionType</ 
> class>
>
>
> < 
> class 
> > 
> de.mms_dresden.cgm_platt.monaco.api.test.history_bl_wl.vo.KEPAction</ 
> class>
>
> <exclude-unlisted-classes>true</exclude-unlisted-classes>
> </persistence-unit>
> ...
>
> OpenEJB Config:
>>
>>> ...
>>> <Resource id="HistoryBLWLCalldataServices"  
>>> type="javax.sql.DataSource">
>>>  JdbcDriver com.mysql.jdbc.Driver
>>>  JdbcUrl
>> jdbc:mysql://wum08071.mms-dresden.de:3307/ 
>> ssc_monaco_0_9_calldata_test
>>>  UserName cgsdb
>>>  Password cgsdb
>>>  JtaManaged true
>>> </Resource>
>>> <Resource id="HistoryBLWLCockpitServices"  
>>> type="javax.sql.DataSource">
>>>  JdbcDriver com.mysql.jdbc.Driver
>>>  JdbcUrl
>> jdbc:mysql://wum08071.mms-dresden.de:3307/ssc_monaco_0_9_cockpit_test
>>>  UserName cgsdb
>>>  Password cgsdb
>>>  JtaManaged true
>>> </Resource>
>>> ...
>>
>
> Log:
>
>>
>>> Configuring Service(id=HistoryBLWLCalldataServices, type=Resource,
>> provider-id=Default JDBC Database)
>>> Configuring Service(id=HistoryBLWLCockpitServices, type=Resource,
>> provider-id=Default JDBC Database)
>>> Found EjbModule in classpath:
>> D:\cgm\monaco_0.9.0\workspace\monaco\impl\test\history_bl_wl\service 
>> \target\classes
>>
>>> Found EjbModule in classpath:
>> D:\repository\de\mms_dresden\test.ejb.openejb 
>> \0.0.3\test.ejb.openejb-0.0.3.jar
>>
>>> Beginning load:
>> D:\cgm\monaco_0.9.0\workspace\monaco\impl\test\history_bl_wl\service 
>> \target\classes
>>
>>> Beginning load:
>> D:\repository\de\mms_dresden\test.ejb.openejb 
>> \0.0.3\test.ejb.openejb-0.0.3.jar
>>
>>> Configuring enterprise application: classpath.ear
>>> Configuring Service(id=Default Stateless Container, type=Container,
>> provider-id=Default Stateless Container)
>>> Auto-creating a container for bean classes/HistoryBLWLServiceImpl:
>> Container(type=STATELESS, id=Default Stateless Container)
>>> Configuring PersistenceUnit(name=HistoryBLWLCalldataServices)
>>> Auto-creating a Resource with id 'HistoryBLWLCalldataServicesNonJta'
>> of type 'DataSource for 'HistoryBLWLCalldataServices'.
>>> Configuring Service(id=HistoryBLWLCalldataServicesNonJta,
>> type=Resource, provider-id=HistoryBLWLCalldataServices)
>>> Adjusting HistoryBLWLCalldataServices <jta-data-source> to
>> 'HistoryBLWLCalldataServices'
>>> Adjusting HistoryBLWLCalldataServices <non-jta-data-source> to
>> 'HistoryBLWLCalldataServicesNonJta'
>>> Configuring PersistenceUnit(name=HistoryBLWLCockpitServices)
>>> Adjusting HistoryBLWLCockpitServices <jta-data-source> to
>> 'HistoryBLWLCalldataServices'
>>> Adjusting HistoryBLWLCockpitServices <non-jta-data-source> to
>> 'HistoryBLWLCalldataServicesNonJta'
>
> The last two lines are confusing.
>
>> When I make a call:
>>
>>> JDBCExceptionReporter: Table
>> 'ssc_monaco_0_9_calldata_test.test_kep_action' doesn't exist
>>
>
> What's the problem?
>
> BR,
> Karsten
>