Announcement Announcement Module
Collapse
No announcement yet.
Unable to use JNDI DataSource provided by Tomcat in Spring? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to use JNDI DataSource provided by Tomcat in Spring?

    I want to use JNDI DataSource provided by Tomcat in Spring based application and use Tomcat 7 JDBC pool.

    I created META-INF\context.xml with content:

    Code:
        <Resource name="jdbc/ApsuserAtAzistst"
                  auth="Container"
                  type="org.apache.tomcat.jdbc.pool.DataSource"
                  driverClassName="oracle.jdbc.OracleDriver"
                  url="jdbc:oracle:thin:@10.0.153.10:1525:AZISTST"
                  username="APSUSER"
                  password="PASSWORDOFAPSUSER"
                  initialSize="2"
                  minIdle="2"
                  maxIdle="10"
                  maxActive="20"
                  maxWait="1000"
                  validationQuery="select 1 from dual"
                  jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1500)"
     
                />
    and configured applicationContext.xml

    Code:
        <beans profile="dev,test,default">
            <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
                <property name="jndiName" value="java:comp/env/jdbc/ApsuserAtAzistst"/>
            </bean>
        </beans>
    When I run applcation I receive an error:

    Code:
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public final void org.mybatis.spring.support.SqlSessionDaoSupport.setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    	... 45 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:876)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:551)
    	... 47 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    	... 60 more
    Caused by: javax.naming.NamingException: Cannot create resource instance
    	at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:146)
    	at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
    	at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
    	at javax.naming.InitialContext.lookup(Unknown Source)
    	at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154)
    	at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
    	at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
    	at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
    	at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
    	at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
    	at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
    	at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    	... 67 more
    Everything works when I change applicationContext.xml to:

    Code:
    <beans profile="dev,test,default">
        <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
            <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
            <property name="url" value="jdbc:oracle:thin:@10.0.153.10:1525:AZISTST"/>
            <property name="username" value="APSUSER"/>
            <property name="password" value="PASSWORDOFAPSUSER"/>
            <property name="initialSize" value="2"/>
            <property name="minIdle" value="2"/>
            <property name="maxIdle" value="2"/>
            <property name="maxActive" value="3"/>
            <property name="maxWait" value="1000"/>
            <property name="validationQuery" value="select 1 from dual"/>
            <property name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1500)"/>
        </bean>
    </beans>
    But I want to configure JNDI DataSource and use Tomcat 7 JDBC pool. Please help to solve this issue. Currenly I replaced type="org.apache.tomcat.jdbc.pool.DataSource" with type="javax.sql.DataSource", but it used tomcat-dbcp instead of tomcat-jdbc. Reason of JNDI usage is desire use psi probe to see pool usage.

  • #2
    I suggest using the jee namespace instead of the factory bean and don't include the java:comp/env part spring can handle that for you.

    Code:
    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/ApsuserAtAzistst" resource-ref="true" />
    Also make sure your resource is configured correctly as the exception seems to originate from tomcat whilst creating or looking up the resource.

    Comment


    • #3
      The reason was that, I missed factory="org.apache.tomcat.jdbc.pool.DataSourceFac tory". Right definition of resource must be:

      Code:
         <Resource name="jdbc/ApsuserAtAzistst"
                    auth="Container"
                    type="javax.sql.DataSource"
                    factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
                    driverClassName="oracle.jdbc.OracleDriver"
                    url="jdbc:oracle:thin:@10.0.153.10:1525:AZISTST"
                    username="APSUSER"
                    password="PASSWORDOFAPSUSER"
                    initialSize="2"
                    minIdle="2"
                    maxIdle="10"
                    maxActive="20"
                    maxWait="1000"
                    validationQuery="select 1 from dual"
                    jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1500)"
       
                  />

      Comment

      Working...
      X