tags:

views:

165

answers:

2

in continuation to the question from last week: http://stackoverflow.com/questions/2828237/problem-configure-jboss-to-work-with-jndi

I'm trying to bind datasource in JBoss and use it in my application. In my struggling, I already managed to avoid the javax.naming.NameNotFoundException by:
1. using in java new InitialContext().lookup(connection);
instead of new JndiObjectFactoryBean().setJndiName(connection);
2. changing the connection name from: 'jndi-name' to 'java:jndi-name'

Now the problem is that the datasouce that I get from the lookup is null. I created the datsource file:

 <datasources>  
   <local-tx-datasource>
    <jndi-name>bilby</jndi-name>
    <connection-url>jdbc:oracle:myURL</connection-url>
    <driver-class>oracle.jdbc.OracleDriver </driver-class>
    <user-name>myUsername</user-name>
    <password>myPassword</password>        
    <exception-sorter-class- name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>

            <metadata>
         <type-mapping>Oracle9i</type-mapping>
      </metadata>   
</local-tx-datasource>

</datasources>

and put it under \server\default\deploy\oracle-ds.xml
I get during runtime the line:

18:37:56,560 INFO [ConnectionFactoryBindingService] Bound ConnectionManager 'jb oss.jca:service=DataSourceBinding,name=bilby' to JNDI name 'java:bilby'

So my question is - why do I get null as my datasource???

A: 

Firstly, I just want to make sure that when you said

changing the connection name from: 'jndi-name' to 'java:jndi-name'

that you really meant java:bilby, right?

I'm not sure specifically why this is happening, but I can suggest a workaround.

Add the following line to your oracle-ds.xml, after the <jndi-name> element:

<use-java-context>false</use-java-context>

When this is deployed, it should remove the java: prefix from the JNDI binding. You should then be able to use:

new InitialContext().lookup("bilby")

and get your DataSource back.

skaffman
It didn't work. even after adding the 'use-java-context' to the oracle-ds.xml file lookup("bibly") caused jdbc not bound exception <br>and lookup("java:bilby") returned null object
Spiderman
I saw that Jboss also print the following info message, perhaps it's related to this issue?13:37:13,870 INFO [STDOUT] WARN - The Oracle9Dialect dialect has been deprecated; use either Oracle9iDialect or Oracle10gDialect instead
Spiderman
perhaps there is a security issue that doesn't allow application recieve java resource automatically and I still need to cofigure something else to allow it?
Spiderman
I forgot to mention that I am working with JBoss version 4.2.3.GA
Spiderman
@Spiderman This warning is just saying that this Hibernate Dialect is deprecated, that's all, nothing to do with your problem. Oh, by the way, I ran the code of my answer on JBoss 4.2.3.GA.
Pascal Thivent
A: 

First of all, I suggest looking at the samples available in JBOSS_DIST/docs/examples/jca for all configuration options (including pool sizing parameters).

Secondly, I did a quick test with the default datasource configured in hsqldb-ds.xml which is defined as follow:

<datasources>
   <local-tx-datasource>

      <!-- The jndi name of the DataSource, it is prefixed with java:/ -->
      <!-- Datasources are not available outside the virtual machine -->
      <jndi-name>DefaultDS</jndi-name>

      ...

</datasource>

And the following lookup just works:

DataSource ds = null;
Connection conn = null;
try {
    ds = (DataSource) new InitialContext().lookup("java:/DefaultDS");
    conn = ds.getConnection();
    // ...
    conn.close();
} catch (Exception e) {
    // handle me
}

If this doesn't for you, maybe clarify what you are passing to the lookup method exactly.

Pascal Thivent
I can't use the default datasource because it is not good for connecting Oracle database.Second - when I perform lookup("bilby") I get javax.naming.NameNotFoundException: jdbc not boundand when I perform lookup("java:bilby") it return null object
Spiderman
@Spiderman This was just sample, you're obviously not going to use the datasource for HSQLDB if you want to connect to Oracle... The idea was to put some emphasis on the XML comment and to show you a working piece of code.
Pascal Thivent
Pascal you are right. I get null value also when trying to lookup for "java:/DefaultDS" even though this resource in availble under java namespace. Why is that??
Spiderman
Perhaps I need to init the InitialContext not with empty constructor but with some environment?
Spiderman
@Spiderman: If you run your application inside JBoss, it shouldn't be necessary. Are you trying to get this datasource from outside JBoss?
Pascal Thivent
My application is inside JBoss. (the .war is deployed in <JBoss-root>/server/default/deploy)The datasource files (ends with -ds.xml) are also located in the same folder.Is this what you mean?
Spiderman
@Spiderman I wanted to know if the lookup was done inside JBoss or from a standalone application running outside JBoss. I have my answer. Honestly, this is weird and I'm running out of ideas. Just in case, this is the servlet I used for testing: http://pastebin.com/BnkARUiw
Pascal Thivent