problem configure JBoss to work with JNDI

2019-02-18 15:17发布

问题:

I am trying to bind connection to the DB using JNDI in my application that runs on JBoss. I did the following:

  1. I created the datasource file oracle-ds.xml filled it with the relevant xml elements:
<datasources>
   <local-tx-datasource>
     <jndi-name>bilby</jndi-name>
     ...
    </local-tx-datasource>
</datasources>

and put it in the folder \server\default\deploy

  1. Added the relevant oracle jar file

  2. than in my application I performed:

JndiObjectFactoryBean factory = new JndiObjectFactoryBean();

 factory.setJndiName("bilby");
 try{
     factory.afterPropertiesSet();
     dataSource = factory.getObject();
 }
 catch(NamingException ne) {
     ne.printStackTrace();
 }

and this cause the error:

javax.naming.NameNotFoundException: bilby not bound

then in the output after this error occured I saw the line:

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

So what is my configuration problem? I think that it may be that JBoss first loads and runs the .war file of my application and only then it loads the oracle-ds.xml that contain my data-source definition. The problem is that they are both located in the same folder. Is there a way to define priority of loading them, or maybe this is not the problem at all.

Any idea?

回答1:

You should use such construction to call Datasource: java:bilby.

You can read more about that here:

Naming and Directory (JNDI) - JBOSS jndi Datasource: jdbc not bound



回答2:

To check how the datasource is bound in the JNDI tree you should use the jmx-console http://localhost8080/jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss%3Aservice%3DJNDIView and invoke the list() method.

Datasources are registered under "jdbc". In your case "jdbc/bilby"

EDIT: That was an example that works for me without spring. Now found this example which injects a more complete JNDI name.

<bean id="idDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/bilby" />
</bean>


标签: java jboss jndi