Results 1 to 7 of 7

Thread: spring 2.5 jndi/jdbc tomcat5.5

  1. #1

    Default spring 2.5 jndi/jdbc tomcat5.5

    Dear all,
    I am trying to configure my spring web application to use the JNDI/JDBC datasource
    from Tomcat, but I get an error message saying:

    Code:
    ERROR [Thread-1] [2008-08-29 22:29:42,687] (ContextLoader.java:215) - Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/eliot-servlet.xml]: Invocation of init method failed; nested exception is javax.naming.NamingException: Could not create resource factory instance [Root exception is java.lang.ClassCastException: org.springframework.jndi.JndiObjectFactoryBean]
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1337)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3764)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216)
    	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
    	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
    	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
    	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
    	at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
    	at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
    	at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
    	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
    	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
    Caused by: javax.naming.NamingException: Could not create resource factory instance [Root exception is java.lang.ClassCastException: org.springframework.jndi.JndiObjectFactoryBean]
    	at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:102)
    	at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:793)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
    	at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    	at org.apache.naming.SelectorContext.lookup(SelectorContext.java:137)
    	at javax.naming.InitialContext.lookup(Unknown Source)
    	at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
    	at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:88)
    	at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:153)
    	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:200)
    	at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:186)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
    	... 39 more
    Caused by: java.lang.ClassCastException: org.springframework.jndi.JndiObjectFactoryBean
    	at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:98)

    My environment:

    JDK 1.5.0
    Tomcat 5.5
    Windows XP
    Spring 2.5



    Here below is my configuration:

    part of the web app's web.xml

    Do we actually need this, or is Spring taking care of it?
    Code:
    <resource-ref>
    	<description>DB Connection</description>
    	<res-ref-name>jdbc/testDB</res-ref-name>
    	<res-type>javax.sql.DataSource</res-type>
    	<res-auth>Container</res-auth>
    </resource-ref>

    part of the web apps application-servlet.xml
    Code:
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    	<property name="jndiName"><value>java:comp/env/jdbc/testDB</value></property>
    </bean>

    web app's META-INF/context.xml

    pay attention to values of the factory and type attributes (I hope they are right.)
    Code:
    <Context>
    	<Resource 
    		name="jdbc/testDB" 
    		factory="org.springframework.jndi.JndiObjectFactoryBean"
    		auth="Container" 
    		type="javax.sql.DataSource" 
    		maxActive="10" 
    		maxIdle="5" 
    		maxWait="10000" 
    		removeAbandoned="true"
    		removeAbandonedTimeout="60"
    		logAbandoned="true"
    		username="username" 
    		password="password" 
    		driverClassName="com.mysql.jdbc.Driver" 
    		url="jdbc:mysql://localhost:3306/test" />
    </Context>
    What is the problem?

    I have googled everywhere and I have read the Spring docs, but I need a step-by-step guide.

    Hope you can help.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Your configuration of your datasource is wrong. It should configure a DataSource NOT a JndiObjectFactoryBean...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Do you mean I should remove the factory value from the resource element
    i.e,
    remove :
    factory="org.springframework.jndi.JndiObjectFactor yBean"
    Code:
    <Context>
    	<Resource 
    		name="jdbc/testDB" 
    		auth="Container" 
    		type="javax.sql.DataSource" 
    		maxActive="10" 
    		maxIdle="5" 
    		maxWait="10000" 
    		removeAbandoned="true"
    		removeAbandonedTimeout="60"
    		logAbandoned="true"
    		username="username" 
    		password="password" 
    		driverClassName="com.mysql.jdbc.Driver" 
    		url="jdbc:mysql://localhost:3306/test" />
    </Context>

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Exactly what I meant.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    Thanks, just two questions.

    Is it necessary to include this in web.xml
    or does Spring take care of it.
    Code:
    <resource-ref>
    	<description>DB Connection</description>
    	<res-ref-name>jdbc/testDB</res-ref-name>
    	<res-type>javax.sql.DataSource</res-type>
    	<res-auth>Container</res-auth>
    </resource-ref>

    Secondly, I assume the connection pooling management is either done
    by tomcat or Spring, i.e, I don't have to do any specific programmatically,
    apart from retrieving the datasource to get a connection.

    Thanks.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Is it necessary to include this in web.xml
    It depends if it is a app-server wide or application specific JNDI entry. In your case it probably isn't needed, just try.

    Secondly, I assume the connection pooling management is either done
    by tomcat or Spring, i.e, I don't have to do any specific programmatically,
    apart from retrieving the datasource to get a connection.
    Spring doesn't do anything in that regard, it just uses the configured datasource. If that is a pooling one fine if it isn't a pooling one fine also. So it depends on what you have defined. By default tomcat uses the Commons DBCP for pooling so in this case tomcat has a pooled JNDI DataSource.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default

    Thanks,
    that was very useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •