Announcement Announcement Module
Collapse
No announcement yet.
How to create a jndi using spring datasource? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:
    Code:
    <bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <!-- results in a setDriverClassName&#40;String&#41; call -->
            <property name="driverClassName">
                <value>com.mysql.jdbc.Driver</value>
            </property>
            <property name="url">
                <value>jdbc&#58;mysql&#58;//127.0.0.1&#58;3306/DEVCENTRY?useUnicode=true&amp;characterEncoding=gb2312</value>
            </property>
            <property name="username">
                <value>lyo</value>
            </property>
    		<property name="password">
                <value>lyo</value>
            </property>
    		
    		<property name="defaultAutoCommit"> 
                <value>true</value> 
            </property>
    		
        </bean>
    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.

    Rgds
    Rod

    Comment


    • #3


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

      Comment


      • #4


        There is an problem. I write this code in API:
        Code:
        Properties p = new Properties&#40;&#41;;
        p.setProperty&#40;"java.naming.factory.initial",
        "org.jnp.interfaces.NamingContextFactory"&#41;;
        p.setProperty&#40;"java.naming.provider.url",
        "jnp&#58;//localhost&#58;1099"&#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

        Comment


        • #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"/>
          </bean>

          Comment


          • #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.

            <resource-ref>
            <description>Oracle Datasource</description>
            <res-ref-name>jdbc/mydatasourceDS</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
            </resource-ref>

            Comment


            • #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="http://www.w3.org/2001/XMLSchema-instance" ... >

              <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="jdbcracle:thin:@volga.company.gov.br:1532 pus"/>
              </connection-pool>

              </data-sources>

              ------------------------------------------------------------------------------------------------------------------------

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

              -------------------------------------------------------

              <beans>

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

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

              </beans>
              ---------------------------------------------------------

              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.

              <resource-ref>
              <description>Oracle Datasource</description>
              <res-ref-name>jdbc/mydatasourceDS</res-ref-name>
              <res-type>javax.sql.DataSource</res-type>
              <res-auth>Container</res-auth>
              </resource-ref>

              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 http://static.springsource.org/sprin...nce/index.html

              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.

              Comment


              • #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>
                </bean>

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

                <Resource name="jdbc/ISM_rep_user"
                auth="Container"
                type="javax.sql.DataSource"
                removeAbandoned="true"
                removeAbandonedTimeout="60"
                logAbandoned="true"
                maxActive="10"
                maxIdle="5"
                maxWait="15000"
                username="ism_cust2"
                password="iamse2_"
                driverClassName="oracle.jdbc.OracleDriver" url="jdbcracle:thin:@xxx.gggg/>

                and in web.xml i am using

                <resource-ref>
                <description>Connection Pool</description>
                <res-ref-name>jdbc/ISM_rep_user</res-ref-name>
                <res-type>javax.sql.Datasource</res-type>
                <res-auth>Container</res-auth>
                </resource-ref>

                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
                tener
                [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
                gent:spring-agent.jar
                [INFO] [talledLocalContainer] at org.springframework.beans.factory.support.Abs
                tractAutowireCapableBeanFactory.doCreateBean(Abstr actAutowireCapableBeanFactory.

                Comment

                Working...
                X