GeeCON Prague 2014*
The moose likes JDBC and the fly likes datasource using tomcat in spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "datasource using tomcat in spring" Watch "datasource using tomcat in spring" New topic
Author

datasource using tomcat in spring

jk
Greenhorn

Joined: Feb 18, 2006
Posts: 3
how to use datasource using tomcat in spring
i created spring

<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>bookDataSource</value>
</property>
</bean>


this is my server.xml file

<?xml version="1.0" encoding="UTF-8"?>
<Server>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
<GlobalNamingResources>
<Environment
name="simpleValue"
type="java.lang.Integer"
value="30"/>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
pathname="conf/tomcat-users.xml"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
<Resource
name="bookDataSource"
type="javax.sql.DataSource"
password="admin"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
validationQuery="select * from book"
username="root"
url="jdbc:mysql://localhost:3306/hibernate?autoReconnect=true "
maxActive="4"/>


</GlobalNamingResources>


<Context path="/bookDataSource" docBase="bookDataSource" debug="0"
reloadable="true" crossContext="true">


<Resource name="bookDataSource" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="bookDataSource">
<parameter><name>username</name><value>root</value></parameter>
<parameter><name>password</name><value>admin</value></parameter>
<parameter><name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value></parameter>
<parameter><name>url</name>
<value>jdbc:mysql://localhost:3306/hibernate?autoReconnect=true</value></parameter>
</ResourceParams>

</Context>

<Service
name="Catalina">
<Connector
port="8080"
redirectPort="8443">
</Connector>
<Connector
port="8009"
protocol="AJP/1.3"
protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler"
redirectPort="8443">
</Connector>
<Engine
defaultHost="localhost"
name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
<Host
appBase="webapps"
name="localhost">

</Host>
</Engine>
</Service>
</Server>



this my web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
springbook</display-name>
<servlet>
<description>
</description>
<display-name>
bookservlet</display-name>
<servlet-name>bookservlet</servlet-name>
<servlet-class>book.bookservlet</servlet-class>
</servlet>
<servlet>
<description>
</description>
<display-name>
programServlet</display-name>
<servlet-name>programServlet</servlet-name>
<servlet-class>
book1.programServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bookservlet</servlet-name>
<url-pattern>/bookservlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>programServlet</servlet-name>
<url-pattern>/programServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<resource-ref>
<description>DB Connection</description>
<res-ref-name>bookDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

</web-app>


but error comes
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is javax.naming.NameNotFoundException: Name DataSource is not bound in this Context

how to solve this
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

"karthik jk", you have previously been warned on multiple occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional, and this is your final warning. Adjust your display name to comply with the required standards prior to your next post.

Failure to comply will result in the removal of your account.

bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
GeeCON Prague 2014
 
subject: datasource using tomcat in spring