JBoss Community

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

created by Kylin Soong in JNDI and Naming - View the full discussion

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"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema 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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="

        http://java.sun.com/xml/ns/persistence

        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"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:jdbc="http://www.springframework.org/schema/jdbc"   

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

            http://www.springframework.org/schema/jdbc 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

Start a new discussion in JNDI and Naming at Community