Icon

Software Engineering, Architecture, Web Development and beyond…

JNDI Configuration, globally defined Data Sources in Tomcat

JNDI is such an issue which developers configure once and don’t need to take care of it anymore. However, you’ll need to look at documentation everytime it must be configured to refresh your knowledge. But, everytime i need to configure my tomcat, i get headaches. For that reason here is my blog about defining data sources globally and to make them available for all application.

I have read all sections in official tomcat documentations to get my project work with JNDI resources, but it suffers with exception as soon as the application inits. If i put my just into the tomcat’s context.xml, i get an exception “NameNotFoundException: Datasource not bound” while tomcat starts. I have tried tousands of variations to make JNDI resources to make available for all applications under webapps.
Here is the way which works really:

1. Define your JNDI sources under /conf/server.xml

  <GlobalNamingResources>
 
      <Resource acquireIncrement="1"
                auth="Container"
                description="example datasource"
                driverClass="com.mysql.jdbc.Driver"
                factory="org.apache.naming.factory.BeanFactory"
                idleConnectionTestPeriod="600"
                initialPoolSize="1"
                jdbcUrl="jdbc:mysql://127.0.0.1:3306/mydb?autoReconnect=true"
                maxIdleTime="600"
                maxPoolSize="2"
                maxStatements="50"
                minPoolSize="1"
                name="mydatasource"
                password="4sfS345se3E"
                preferredTestQuery="SELECT 1"
                type="com.mchange.v2.c3p0.ComboPooledDataSource"
                user="myuser"/>
 
  </GlobalNamingResources>

2. add resource links into /conf/context.xml

<ResourceLink global="mydatasource" name="mydatasource"/>

3. don’t forget to add data source libraries (jars) into tomcats /lib for tomcat 6, /shared/lib for tomcat 5. In my case, i am using pooled data source of c3p0.

After restarting tomcat, my application starts without any exception and my database connection is just there.

erhan

Author


Hello, I'm Erhan Bagdemir and this is my blog. I talk about Java, J2EE, Frameworks, web application development, OOAD and various other topics often related to programming.

Erhan Bagdemir  Profil von Erhan Bagdemir auf LinkedIn anzeigen

ebagdemir on Stackoverflow

ebagdemir on Twitter

    Hamburg

    Slideshow