Mar 23, 2011 0
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
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.


