Question

I would like to know how to create a JNDI entry for a MySQL database using Eclipse Kepler and Tomcat 7 (Is there a plugin for Eclipse?).

When I created the Dynamic Web Project using Eclipse, I did NOT checked the "Generate web.xml deployment descriptor", so, I do not have a web.xml file in my project.

Was it helpful?

Solution

The JDNI resource definition is declared in Tomcat's <Context> element in context.xml 1, not your new application's web.xml. So whether it's generated or not, it doesn't actually matter.

<Context>
<Resource name="jdbc/YourDB" auth="Container" 
          type="javax.sql.DataSource" 
          username="username" password="password"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://dbserver:dbport/actualDbname"/>
</Context>

If you had web.xml in your application, you will need an entry like this to refer to the datasource mentioned above:

<resource-ref>
  <res-ref-name>jdbc/YourDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

Since you opted not to have web.xml, you can use annotations:

@Resource(lookup = "java:comp/env/jdbc/YourDB")
private DataSource dataSource;

1 There are multiple ways to define the Context, refer to Tomcat's definition on Defining a Context.

OTHER TIPS

When I posted the question, I forgot to say that I'm using Hibernate and to include the files.

The hibernate.cfg.xml is:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE 
hibernate-configuration 
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.datasource">java:comp/env/jdbc/ExemplosWeb</property>

    <property name="hibernate.connection.username">TheUsername</property>
    <property name="hibernate.connection.password">ThePassword</property>

    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hibernate.use_sql_comments">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.hbm2ddl.auto">create-drop</property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <!-- One to One Unidirecional -->
    <mapping class="relacionamentoHibernate.bean.PaiOneToOneUnidirecional"/>
    <mapping class="relacionamentoHibernate.bean.FilhoOneToOneUnidirecional"/>
</session-factory>

There is an Hibernate util class:

package hibernateUtil;

import javax.annotation.Resource;
import org.hibernate.*;
import org.hibernate.cfg.*;

@Resource(lookup="java:comp/env/jdbc/exemplosweb")
@SuppressWarnings("deprecation")
public class HibernateUtil 
{
    private static final SessionFactory sessionFactory;
    static 
    {
        try 
        {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } 
        catch (HibernateException e) 
        {
            // Make sure you log the exception, as it might be swallowed
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
        catch (NoClassDefFoundError e) 
        {
            e.printStackTrace();
            throw new NoClassDefFoundError("static HibernateUtil");
        }
    }

    public static SessionFactory getSessionFactory() 
    {
        return sessionFactory;
    }
}

and I got this exception:

Fev 26, 2014 7:02:46 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Fev 26, 2014 7:02:46 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
Fev 26, 2014 7:02:46 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Fev 26, 2014 7:02:46 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Fev 26, 2014 7:02:46 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Fev 26, 2014 7:02:46 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Fev 26, 2014 7:02:46 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace      http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead.  Refer to Hibernate 3.6 Migration Guide!
Fev 26, 2014 7:02:46 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
org.hibernate.service.jndi.JndiException: Unable to lookup JNDI name [java:comp/env/jdbc/ExemplosWeb]
    at org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:68)
    at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:116)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top