You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@geronimo.apache.org by Siraj Mohamed <Si...@virtusa.com> on 2006/03/23 15:05:58 UTC
how to lookup a datasource from standalone java class
Hi All,
I have created a data source and I tried to look up this from a
standalone java class like this:
try {
Context jndiCntx = new InitialContext();
DataSource ds = (DataSource) jndiCntx.lookup("MyDatasource");
DataSource ds = (DataSource)obj;
System.out.println("Connected ..... ");
return ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
My class path has jndi.properties for Geronimo and I am getting
following exception in my class.
javax.naming.NameNotFoundException: /MyDatasource not found
at
org.openejb.client.JNDIContext.lookup(JNDIContext.java:257)
at
javax.naming.InitialContext.lookup(InitialContext.java:361)
And this is the server side error:
ERROR [ContainerIndex] contianerId is not a valid ObjectName:
MyDatasource
But I was able to lookup this data source from an entity bean like:
new InitialContext().lookup("java:comp/env/jdbc/MyExampleDS ");
This the openejb-jar.xml for my EJB
<openejb-jar
xmlns="http://www.openejb.org/xml/ns/openejb-jar"
xmlns:naming="http://geronimo.apache.org/xml/ns/naming"
xmlns:security="http://geronimo.apache.org/xml/ns/security"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment"
configId="geronimo/CustomerEJB/1.0/car"
parentId="geronimo/system-database/1.0/car">
<enterprise-beans>
<entity>
<ejb-name>CustomerEJB</ejb-name>
<jndi-name>CustomerHomeRemote</jndi-name>
<local-jndi-name>CustomerRemote</local-jndi-name>
<resource-ref>
<ref-name>jdbc/MyExampleDS</ref-name>
<resource-link>MyDatasource</resource-link>
</resource-ref>
</entity>
</enterprise-beans>
</openejb-jar>
Could any one explain me the differences between two attempts?
This is the data source deployment plan.
<?xml version="1.0"?>
<connector xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector"
configId="MyDatasource"
parentId="geronimo/j2ee-server/1.0/car">
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql</artifactId>
<version>3.0</version>
</dependency>
<resourceadapter>
<outbound-resourceadapter>
<connection-definition>
<connectionfactory-interface>
javax.sql.DataSource
</connectionfactory-interface>
<connectiondefinition-instance>
<name>MyDatasource</name>
<config-property-setting name="UserName">
xmeta
</config-property-setting>
<config-property-setting name="Password">
xmeta
</config-property-setting>
<config-property-setting name="Driver">
com.mysql.jdbc.Driver
</config-property-setting>
<config-property-setting name="ConnectionURL">
jdbc:mysql://localhost:3306/xmeta
</config-property-setting>
<config-property-setting name="CommitBeforeAutocommit">
false
</config-property-setting>
<config-property-setting name="ExceptionSorterClass">
org.tranql.connector.NoExceptionsAreFatalSorter
</config-property-setting>
<connectionmanager>
<local-transaction/>
<single-pool>
<max-size>10</max-size>
<min-size>1</min-size>
<blocking-timeout-milliseconds>
5000
</blocking-timeout-milliseconds>
<idle-timeout-minutes>
30
</idle-timeout-minutes>
<match-one/>
</single-pool>
</connectionmanager>
</connectiondefinition-instance>
</connection-definition>
</outbound-resourceadapter>
</resourceadapter>
</connector>
Thanks,
Siraj
Re: how to lookup a datasource from standalone java class
Posted by David Jencks <da...@yahoo.com>.
On Mar 23, 2006, at 6:05 AM, Siraj Mohamed wrote:
> Hi All,
>
>
>
> I have created a data source and I tried to look up this from a
> standalone java class like this:
>
>
>
> try {
>
> Context jndiCntx = new InitialContext();
>
> DataSource ds = (DataSource) jndiCntx.lookup("MyDatasource");
>
> DataSource ds = (DataSource)obj;
>
> System.out.println("Connected ….. ");
>
> return ds.getConnection();
>
> } catch (Exception e) {
>
> e.printStackTrace();
>
> }
This won't work. Datasources deployed in geronimo are only available
in the j2ee environment in which they are deployed: either a server
or a j2ee app client in which you have deployed a datasource. There
is no way to access one from a different vm.
Furthermore you can only use jndi to look one up in a thread that has
gone through a j2ee component such as an ejb or web app (or j2ee app
client).
thanks
david jencks
>
>
> My class path has jndi.properties for Geronimo and I am getting
> following exception in my class.
>
>
>
> javax.naming.NameNotFoundException: /MyDatasource not found
>
> at org.openejb.client.JNDIContext.lookup
> (JNDIContext.java:257)
>
> at javax.naming.InitialContext.lookup
> (InitialContext.java:361)
>
>
>
> And this is the server side error:
>
> ERROR [ContainerIndex] contianerId is not a valid ObjectName:
> MyDatasource
>
>
>
> But I was able to lookup this data source from an entity bean like:
>
> new InitialContext().lookup("java:comp/env/jdbc/MyExampleDS ");
>
>
>
> This the openejb-jar.xml for my EJB
>
>
>
> <openejb-jar
>
> xmlns="http://www.openejb.org/xml/ns/openejb-jar"
>
> xmlns:naming="http://geronimo.apache.org/xml/ns/naming"
>
> xmlns:security="http://geronimo.apache.org/xml/ns/security"
>
> xmlns:sys="http://geronimo.apache.org/xml/ns/deployment"
>
> configId="geronimo/CustomerEJB/1.0/car"
>
> parentId="geronimo/system-database/1.0/car">
>
> <enterprise-beans>
>
> <entity>
>
> <ejb-name>CustomerEJB</ejb-name>
>
> <jndi-name>CustomerHomeRemote</jndi-name>
>
> <local-jndi-name>CustomerRemote</local-jndi-name>
>
> <resource-ref>
>
> <ref-name>jdbc/MyExampleDS</ref-name>
>
> <resource-link>MyDatasource</resource-link>
>
> </resource-ref>
>
> </entity>
>
> </enterprise-beans>
>
> </openejb-jar>
>
>
>
> Could any one explain me the differences between two attempts?
>
>
>
> This is the data source deployment plan.
>
>
>
> <?xml version="1.0"?>
>
>
>
> <connector xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector"
>
> configId="MyDatasource"
>
> parentId="geronimo/j2ee-server/1.0/car">
>
> <dependency>
>
> <groupId>mysql</groupId>
>
> <artifactId>mysql</artifactId>
>
> <version>3.0</version>
>
> </dependency>
>
> <resourceadapter>
>
> <outbound-resourceadapter>
>
> <connection-definition>
>
> <connectionfactory-interface>
>
> javax.sql.DataSource
>
> </connectionfactory-interface>
>
> <connectiondefinition-instance>
>
> <name>MyDatasource</name>
>
> <config-property-setting name="UserName">
>
> xmeta
>
> </config-property-setting>
>
> <config-property-setting name="Password">
>
> xmeta
>
> </config-property-setting>
>
> <config-property-setting name="Driver">
>
> com.mysql.jdbc.Driver
>
> </config-property-setting>
>
> <config-property-setting name="ConnectionURL">
>
> jdbc:mysql://localhost:3306/xmeta
>
> </config-property-setting>
>
> <config-property-setting name="CommitBeforeAutocommit">
>
> false
>
> </config-property-setting>
>
> <config-property-setting name="ExceptionSorterClass">
>
> org.tranql.connector.NoExceptionsAreFatalSorter
>
> </config-property-setting>
>
>
>
> <connectionmanager>
>
> <local-transaction/>
>
> <single-pool>
>
> <max-size>10</max-size>
>
> <min-size>1</min-size>
>
> <blocking-timeout-milliseconds>
>
> 5000
>
> </blocking-timeout-milliseconds>
>
> <idle-timeout-minutes>
>
> 30
>
> </idle-timeout-minutes>
>
> <match-one/>
>
> </single-pool>
>
> </connectionmanager>
>
> </connectiondefinition-instance>
>
> </connection-definition>
>
> </outbound-resourceadapter>
>
> </resourceadapter>
>
> </connector>
>
>
>
>
>
> Thanks,
>
> Siraj
>
>