-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to specify persistence units for different environments
PostPosted: Mon Oct 13, 2008 7:52 am 
Regular
Regular

Joined: Sun Sep 25, 2005 11:35 pm
Posts: 57
I am trying to specify different JPA persistence units for my testing and deployment environment: a) hsqldb based for testing and b) mysql based for deployment. What is the best way to do this?

I tried putting two persistence units in the same persistence.xml as follows:

Code:
<persistence-unit name="myappDatabase">
    <jta-data-source>java:/myappDatasource</jta-data-source>
    <properties>
        <property name="jboss.entity.manager.factory.jndi.name" value="java:/myappEntityManagerFactory"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
    </properties>
</persistence-unit>

<persistence-unit name="myappLocalDatabase" transaction-type="RESOURCE_LOCAL">
    <properties>
        <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
        <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:jbpm"/>
        <property name="hibernate.connection.username" value="sa"/>
        <property name="hibernate.connection.password" value=""/>

        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
</persistence-unit>


This works fine when I am running my tests, however when JBoss starts up my app it gives the following exception

java.lang.RuntimeException: You have not defined a non-jta-data-source for a RESOURCE_LOCAL enabled persistence context named: myappLocalDatabase
at org.jboss.ejb3.entity.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:228)

How do I solve this issue?

Thanks.
Naresh


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2008 12:04 am 
Regular
Regular

Joined: Sun Sep 25, 2005 11:35 pm
Posts: 57
I have made a small progress in answering my own question :-), but not quite there yet. Basically, if I add a <non-jta-data-source> element in myappLocalDatabase and now JBoss is happy with both data sources. However my unit tests are broken - I get a NoInitialContextException:

Code:
Could not find datasource: java:/myappLocalDatabase
javax.naming.NoInitialContextException: Need to specify class name in environment or system property,
or as an applet parameter, or in an application resource file:  java.naming.factory.initial
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
        at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
        at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
        at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
        at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
        at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2073)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1298)
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)


So is there a way to specify an InitialContext when running in a test environment? Am I even on the right track?

This is how I am creating an EntityManagerFactory in my test code:

Code:
EntityManagerFactory entityManagerFactory = new HibernatePersistence()
    .createEntityManagerFactory("myappLocalDatabase", null);


and this is how my persistence.xml looks at this point:

Code:
<persistence-unit name="myappDatabase">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/myappDatasource</jta-data-source>
    <properties>
        <property name="jboss.entity.manager.factory.jndi.name" value="java:/myappEntityManagerFactory"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
</persistence-unit>

<persistence-unit name="myappLocalDatabase" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:/myappLocalDatabase</non-jta-data-source>
    <properties>
        <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
        <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:myapp"/>
        <property name="hibernate.connection.username" value="sa"/>
        <property name="hibernate.connection.password" value=""/>

        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
</persistence-unit>


Thanks.
Naresh

P.S. Is this the right forum to ask Hibernate EntityManager questions?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.