Discussion:
[jboss-user] [JNDI and Naming] - spring 3.x + jboss 7.1.1: how to get the datasource from jndi
settholo
2013-01-24 15:17:21 UTC
Permalink
settholo [https://community.jboss.org/people/settholo] created the discussion

"spring 3.x + jboss 7.1.1: how to get the datasource from jndi"

To view the discussion, visit: https://community.jboss.org/message/794098#794098

--------------------------------------------------------------
Hello everyone,
I have to go into production with my web application. The application server is JBoss 7.1.1.
So I was looking for moving the datasources from the spring application context to the container.
I configured the datasource in jboss:


<datasource jta="false" jndi-name="java:jboss/datasources/sampleDS" pool-name="sampleDSPool" enabled="true" use-java-context="true" use-ccm="false">
                    <connection-url>jdbc:postgresql://localhost:5432/sampleDB</connection-url>
                    <driver-class>org.postgresql.Driver</driver-class>
                    <driver>postgresql</driver>
                    <pool>
                        <min-pool-size>2</min-pool-size>
                        <max-pool-size>20</max-pool-size>
                    </pool>
                    <security>
                        <user-name>*****</user-name>
                        <password>*****</password>
                    </security>
                    <validation>
                        <validate-on-match>false</validate-on-match>
                        <background-validation>false</background-validation>
                        <background-validation-millis>1</background-validation-millis>
                    </validation>
                    <statement>
                        <prepared-statement-cache-size>0</prepared-statement-cache-size>
                        <share-prepared-statements>false</share-prepared-statements>
                    </statement>
                </datasource>




Well, when jboss starts it confirms that my datasource it's bound to java:jboss/datasources/sampleDS.


Now I wanted to move the configuration:


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                              <property name="driverClassName" value="${db.driver.prod}" />
                              <property name="url" value="${db.url.prod}" />
                              <property name="username" value="${db.username.prod}" />
                              <property name="password" value="${db.password.prod}" />
                    </bean>




to


<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/sampleDS"/>



or


<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
                              <property name="jndiName">
                                        <value>java:jboss/datasources/sampleDS</value>
                              </property>
                    </bean>



There is no way of getting the datasource from jboss, I always end with name not found ( I tried almost every combination ).
Caused by: javax.naming.NameNotFoundException: jboss/datasources/sampleDS -- service jboss.naming.context.java.jboss.exported.jboss.datasources.sampleDS
Is there someone that is able to use the datasource fomr joss through jndi?

You can find the thread also in the spring forum at: http://forum.springsource.org/showthread.php?134298-spring-3-2-jboss-7-1-1-how-to-get-the-datasource-from-jndi http://forum.springsource.org/showthread.php?134298-spring-3-2-jboss-7-1-1-how-to-get-the-datasource-from-jndi


Thank you for your help.


Daniele
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/794098#794098]

Start a new discussion in JNDI and Naming at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2083]
Kylin Soong
2013-06-20 09:13:17 UTC
Permalink
Kylin Soong [https://community.jboss.org/people/kylin] created the discussion

"Re: spring 3.x + jboss 7.1.1: how to get the datasource from jndi"

To view the discussion, visit: https://community.jboss.org/message/824041#824041

--------------------------------------------------------------
Do you sure your datasource deploy successful? I have done a test:
1. deploy datasource, the jbdi name like 'java:jboss/datasources/SpringTravelDS', deploy datasource(copy SpringTravel-ds.xml to jboss-eap-6.1/standalone/deployments), datasource content like [1]
2. define jpa persistence.xml file, the persistence.xml should be in app classpath, the content like [2]
3. define spring spring.xml file, the spring.xml should be in app classpath, the content like [3]
After above procedure done, it works pretty fine, I would like post the git url, if I have time to extract a simple one.

[1] SpringTravel-ds.xml
---
<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns=" http://www.jboss.org/ironjacamar/schema http://www.jboss.org/ironjacamar/schema"
   xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation=" http://www.jboss.org/ironjacamar/schema http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
   <!-- The datasource is bound into JNDI at this location. We reference this in META-INF/persistence.xml -->
   <!--H2 -->
   <datasource jndi-name="java:jboss/datasources/SpringTravelDS" pool-name="spring-travel-pool" enabled="true" use-java-context="true">
      <connection-url>jdbc:h2:mem:spring-travel;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1</connection-url>
      <driver>h2</driver>
      <security>
         <user-name>sa</user-name>
         <password>sa</password>
      </security>
   </datasource>
</datasources>
---

[2] persistence.xml
---
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
   xmlns=" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

   <persistence-unit name="org.springframework.samples.travel">
      <jta-data-source>java:jboss/datasources/SpringTravelDS</jta-data-source>
      <properties>
           <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
           <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
      </properties>
   </persistence-unit>

</persistence>
---

[3] spring.xml
---
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans"
    xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc=" http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc"   
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
    </bean>
     <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>java:jboss/datasources/SpringTravelDS</value>
        </property>
    </bean>
</beans>
---
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/824041#824041]

Start a new discussion in JNDI and Naming at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2083]
Loading...