You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by PatLaPatate <pa...@scd.desjardins.com> on 2009/12/07 19:30:19 UTC

JNDI lookup...

Hi David and all,

I got my things to work but I still have a question regarding the
configuration of datasources:

Here's the setup for my datasource:

       properties.put("db2x070", "new://Resource?type=DataSource");
       properties.put("db2x070.JdbcDriver", "com.ibm.db2.jcc.DB2Driver");
       properties.put("db2x070.JdbcUrl",
"jdbc:db2://db2d070n0:50127/db2d070");
       properties.put("db2x070.UserName", "pblais");
       properties.put("db2x070.Password", "xxxxxx");       
       initialContext = new InitialContext(properties);

	}

and here's my test:

	public void testAjouterApplication() throws Exception {
		
		Object object = initialContext.lookup("ConsoleGestionServiceImplLocal");
		ConsoleGestionService service = (ConsoleGestionService) object;
		try {
			Application a = new Application();
			a.setId(1L);
			a.setName("fat/ECS1DDD");
			
			service.ajouterApplication(a);
			
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}

My persistence.xml file contains this:
   <persistence-unit name="cgeEntityManagerFactory" transaction-type="JTA">
   	  <provider>org.hibernate.ejb.HibernatePersistence</provider>
   	  <jta-data-source>jdbc/db2x070</jta-data-source>
      <properties>
        <property name="hibernate.ejb.cfgfile"
value="com/desj/visa/srv/cge/orm/config/hibernate.cfg.xml"/>
      </properties>
   </persistence-unit>

The Junit works but I can see that there are 2 lines that I cannot explain
in the log :
"Adjusting PersistenceUnit" from "jdbc/db2x070"...

If I understand correctly  <jta-data-source>jdbc/db2x070</jta-data-source> 
corresponds to the jndi name, why does openEJB changes the values? I would
like to setup my datasource name in the persistence.xml file and map the
datasource in my Junit test to that jndi name so that the code in my
existing application and my junit test map to the same thing... 

Is my explanation clear?

INFO - Auto-creating a container for bean ConsoleGestionServiceImpl:
Container(type=STATELESS, id=Default Stateless Container)
INFO - Configuring PersistenceUnit(name=cgeEntityManagerFactory,
provider=org.hibernate.ejb.HibernatePersistence)
INFO - Auto-creating a Resource with id 'db2x070NonJta' of type 'DataSource
for 'cgeEntityManagerFactory'.
INFO - Configuring Service(id=db2x070NonJta, type=Resource,
provider-id=db2x070)
INFO - Adjusting PersistenceUnit cgeEntityManagerFactory <jta-data-source>
to Resource ID 'db2x070' from 'jdbc/db2x070'
INFO - Adjusting PersistenceUnit cgeEntityManagerFactory
<non-jta-data-source> to Resource ID 'db2x070NonJta' from 'null'


Thanks again.

Patrick
-- 
View this message in context: http://old.nabble.com/JNDI-lookup...-tp26681793p26681793.html
Sent from the OpenEJB User mailing list archive at Nabble.com.


Re: JNDI lookup...

Posted by David Blevins <da...@visi.com>.
On Dec 7, 2009, at 7:30 PM, PatLaPatate wrote:

>
> Hi David and all,
>
> I got my things to work but I still have a question regarding the
> configuration of datasources:
>
> Here's the setup for my datasource:
>
>       properties.put("db2x070", "new://Resource?type=DataSource");
>       properties.put("db2x070.JdbcDriver",  
> "com.ibm.db2.jcc.DB2Driver");
>       properties.put("db2x070.JdbcUrl",
> "jdbc:db2://db2d070n0:50127/db2d070");
>       properties.put("db2x070.UserName", "pblais");
>       properties.put("db2x070.Password", "xxxxxx");
>       initialContext = new InitialContext(properties);
>
> 	}
[...]
> My persistence.xml file contains this:
>   <persistence-unit name="cgeEntityManagerFactory" transaction- 
> type="JTA">
>   	  <provider>org.hibernate.ejb.HibernatePersistence</provider>
>   	  <jta-data-source>jdbc/db2x070</jta-data-source>
>      <properties>
>        <property name="hibernate.ejb.cfgfile"
> value="com/desj/visa/srv/cge/orm/config/hibernate.cfg.xml"/>
>      </properties>
>   </persistence-unit>
>
> The Junit works but I can see that there are 2 lines that I cannot  
> explain
> in the log :
> "Adjusting PersistenceUnit" from "jdbc/db2x070"...
>
> If I understand correctly  <jta-data-source>jdbc/db2x070</jta-data- 
> source>
> corresponds to the jndi name, why does openEJB changes the values?

The values of <jta-data-source> and <non-jta-data-source> are vendor  
specific.  We don't have a "one true jndi name" that everything maps  
to, so we do the best to resolve it as we think you intended it.

The "adjust" log message is just info level, not an error, and just  
tells you exactly what data source we resolved the reference to.  You  
can feel free to ignore the message or if it gets annoying we can find  
a way to shut it off for you.

Alternatively you can name your datasource "jdbc/db2x070" like so:

   properties.put("jdbc/db2x070", "new://Resource?type=DataSource");

Just as long as it's understood this isn't a JNDI name and doesn't  
have impact on your applications.

> I would
> like to setup my datasource name in the persistence.xml file and map  
> the
> datasource in my Junit test to that jndi name so that the code in my
> existing application and my junit test map to the same thing...


It's fine to have all your EJBs and Servlets use "jdbc/db2x070" in the  
@Resource refs, but that's really just style choice.  If you also had  
a few beans like this in your app, we'd have to support it (the java  
ee tck even tests for stuff like this):

     @Stateless
     @Resource(name = "jdbc/db2x070", type = String.class)
     public class OrangeBean {

         @Resource
         SessionContext sessionContext;

         public void someBusinessMethod() throws Exception {

             String myString = (String) sessionContext.lookup("jdbc/ 
db2x070");

             InitialContext initialContext = new InitialContext();
             String sameString = (String)  
initialContext.lookup("java:comp/env/jdbc/db2x070");
         }
     }

     @Stateless
     @Resource(name = "jdbc/db2x070", type = URL.class)
     public class BlueBean {

         @Resource
         SessionContext sessionContext;

         public void someBusinessMethod() throws Exception {

             URL myURL = (URL) sessionContext.lookup("jdbc/db2x070");

             InitialContext initialContext = new InitialContext();
             URL sameURL = (URL) initialContext.lookup("java:comp/env/ 
jdbc/db2x070");
         }
     }


Hope I'm not confusing you.  People naturally want JNDI to be global  
but in Java EE 5 it's not.  Names are relative to the component doing  
the asking (at least for EJB, Servlets are a little better).

In Java EE 6 there finally will be a global JNDI feature.  So at some  
point in the future you could standardly configure jndi entries at an  
server, app, or module level and look them up like "java:global/ 
db2x070" or "java:app/db2x070" or "java:module/db2x070".


-David