configuring a shared MySQL datasource in tomcat

Posted: 11 April 2011 in Teamsite

I’ve been using a datasource and somehow lost some of my settings which meant that it stopped working. In order to get my configuration back, I did the following:

In server.xml (tomcat/conf/server.xml), I declared the following datasource (in the GlobalNamingResources section):

 <Resource name="jdbc/mydb"
 type="javax.sql.DataSource"
 auth="Container"
 driverClassName="com.mysql.jdbc.Driver"
 maxActive="20"
 maxIdle="10"
 maxWait="10000"
 username="myuser"
 password="****"
 url="jdbc:mysql://localhost:3306/mydb"
 />

in context.xml (src/main/webapp/META-INF/context.xml), I declared the following resource link:

  <ResourceLink
 name="jdbc/mydb"
 global="jdbc/mydb"
 type="javax.sql.DataSource"
 />

in web.xml (src/main/webapp/WEB-INF/web.xml), I use the following resource reference:

 <resource-ref>
 <res-ref-name>jdbc/mydb</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
 <res-sharing-scope>Shareable</res-sharing-scope>
 </resource-ref>

in persistence.xml, I use the following:

<persistence version="1.0" 
        xmlns="http://java.sun.com/xml/ns/persistence" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
 <persistence-unit name="mydbPU" transaction-type="RESOURCE_LOCAL">
 <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
 <non-jta-data-source>java:/comp/env/jdbc/mydb</non-jta-data-source>
 <exclude-unlisted-classes>false</exclude-unlisted-classes>
 <properties>
 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
 <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
 </properties>
 </persistence-unit>
</persistence>

If you do not configure the data source correctly, you will get some strange errors of the likes:

  • Illegal access: this web application instance has been stopped already. Could not load com.mysql.jdbc.SQLError.
  • Could not find datasource: java:/comp/env/jdbc/xxxx
  • javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
  • java.sql.SQLException: No suitable driver
  • org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
  • java.sql.SQLException: Cannot create JDBC driver of class ” for connect URL ‘null’
  • Context [/xxxx] startup failed due to previous errors
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s