Announcement Announcement Module
No announcement yet.
How to create a jndi using spring datasource? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to create a jndi using spring datasource?

    Hi everyone:

    I have not config datasource jndi in my server. But I want to use jndi to lookup datasource in jsp. I have spring config the following:
    <bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <!-- results in a setDriverClassName&#40;String&#41; call -->
            <property name="driverClassName">
            <property name="url">
            <property name="username">
    		<property name="password">
    		<property name="defaultAutoCommit"> 
    Could I add a jndi property to this bean? Could spring registe a jndi in server for me to lookup? Thks!

  • #2
    But I want to use jndi to lookup datasource in jsp.
    Why? This is definitely not good practice: you shouldn't do data access operations in views such as JSPs.

    Spring doesn't offer an option to automatically register a DataSource with JNDI. This should be done by the server if you want JNDI registration.

    Your options are to:
    - Use a JNDI container datasource
    - Programmatically register the datasource. Spring's JndiTemplate will help.



    • #3

      Thank you. I will try the JndiTemplate. ;-)


      • #4

        There is an problem. I write this code in API:
        Properties p = new Properties&#40;&#41;;
        JndiTemplate jndi = new JndiTemplate&#40;p&#41;;
        Properties env = jndi.getEnvironment&#40;&#41;;
        try &#123;
        jndi.bind&#40;"jdbc/mysql", something&#41;; <---- what's the something
        What's the somthing ? What should I write here? I mean that the "something" is the jndi object ? THks


        • #5
          Replace your bean "MyDataSource" of spring conf with this bean:

          <bean id="serverDataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
          <property name="jndiName" value="java:comp/env/jdbc/blah"/>


          • #6
            Don't forget &lt;resource-ref&gt; in the web.xml file

            Did you check your web.xml file and made sure you have a resource-ref entry there point to your data source? This is where your application references a resource, in this case a database data source, existing in the container.

            <description>Oracle Datasource</description>


            • #7
              The other day I was trying to implement a managed data source into the Oracle OC4J to be accessed by my
              web JSP application.

              Configuring the container was easy through the OC4J Web app console.

              It produced the following datasource.xml file:

              File [oc4j-home]\j2ee\home\config\data-sources.xml :

              <?xml version = '1.0' encoding = 'UTF-8'?>

              <data-sources xmlns:xsi="" ... >

              <managed-data-source connection-pool-name="development" jndi-name="java:/comp/env/jdbc/mydatasourceDS" name="mydatasourceDS"/>

              <connection-pool name="development">
              <connection-factory factory-class="oracle.jdbc.pool.OracleDataSource" user="myusername" password="mypass" url=" pus"/>



              Then I wrote the following in the dataAccessContext.xml of my application



              <!-- ========================= DATA SOURCE ========================= -->
              <!-- Container OC4J - Managed Data Source -->
              <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
              <property name="resourceRef">
              <property name="jndiName">

              <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
              <property name="dataSource">
              <ref local="dataSource" />


              Uppon deployment I kept receiving the following message from the OC4J container:

              Error creating bean with name 'dataSource' defined in class path resource [dataAccessContext.xml]:
              Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion:
              Invalid property 'mydatasourceDS' of bean class [org.springframework.jndi.JndiObjectFactoryBean]:
              Bean property 'mydatasourceDS' is not writable or has an invalid setter method:
              Does the parameter type of the setter match the return type of the getter?

              Finally a friend of mine instructed me to put a resource-ref entry into the web.xml file of my application.

              <description>Oracle Datasource</description>

              After that everything worked fine and I managed to have my application deployed succesfully into OC4J.

              I have read the "Spring - Java/J2EE Application Framework Reference Documentation", version 1.1.5, available
              in the web at

              I am not an expert in JMS but I noticed that the documentation did mention the resource-ref in the JMS part of the text.
              Reading the example provided in the "Sample application context" of the Tapestry implementation (though I am not using Tapestry) I noticed that the excerpt of xml lines defining a data source there (using the JndiObjectFactoryBean class) was the one I needed for my application. However I couldn't find in the text an explicit reference for an example using the resource-ref entry for the web.xml file. Was that available in the documentation it would have helped a lot.


              • #8
                Hi All,

                I want to read Tomcat datasource via JNDI in my Spring configuration i am using oracle toplink
                in spring applicationContext.xml i am using like below

                <bean id="UserDatabase" class="org.springframework.jndi.JndiObjectFactoryB ean">
                <property name="jndiName" value="java:comp/env/jdbc/ISM_rep_user"></property>
                <property name="lookupOnStartup" value="true"></property>
                <property name="cache" value="true"></property>
                <property name="proxyInterface" value="javax.sql.DataSource"></property>

                and in tomcat/conf/context.xml i am using below

                <Resource name="jdbc/ISM_rep_user"
                driverClassName="oracle.jdbc.OracleDriver" url="jdbcracle:thin:@xxx.gggg/>

                and in web.xml i am using

                <description>Connection Pool</description>

                I am still getting problem please give me some example

                I am getting below Problem

                [INFO] [talledLocalContainer] SEVERE: Exception sending context initialized even
                t to listener instance of class org.springframework.web.context.ContextLoaderLis
                [INFO] [talledLocalContainer] org.springframework.beans.factory.BeanCreationExce
                ption: Error creating bean with name 'org.springframework.context.weaving.Aspect
                JWeavingEnabler#0': Initialization of bean failed; nested exception is org.sprin
                gframework.beans.factory.BeanCreationException: Error creating bean with name 'l
                oadTimeWeaver': Initialization of bean failed; nested exception is java.lang.Ill
                egalStateException: ClassLoader [org.apache.catalina.loader.WebappClassLoader] d
                oes NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a cust
                om LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaa
                [INFO] [talledLocalContainer] at
                tractAutowireCapableBeanFactory.doCreateBean(Abstr actAutowireCapableBeanFactory.