【问题标题】:How to create a JNDI using Eclipse and Tomcat for a Dynamic Web Project without web.xml如何使用 Eclipse 和 Tomcat 为没有 web.xml 的动态 Web 项目创建 JNDI
【发布时间】:2014-03-22 05:37:18
【问题描述】:

我想知道如何使用 Eclipse Kepler 和 Tomcat 7 为 MySQL 数据库创建 JNDI 条目(是否有 Eclipse 插件?)。

当我使用 Eclipse 创建动态 Web 项目时,我没有检查“生成 web.xml 部署描述符”,所以我的项目中没有 web.xml 文件。

【问题讨论】:

    标签: java mysql eclipse tomcat jndi


    【解决方案1】:

    JDNI 资源定义在 context.xml1 的 Tomcat 的 <Context> 元素中声明,而不是在新应用程序的 web.xml 中声明。所以不管是不是生成的,其实都无所谓。

    <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>
    

    如果您的应用程序中有 web.xml,则需要这样的条目来引用上述数据源:

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

    由于您选择不使用 web.xml,因此您可以使用注释:

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

    1 Context的定义有多种方式,参考Tomcat在Defining a Context上的定义。

    【讨论】:

      【解决方案2】:

      当我发布问题时,我忘了说我正在使用 Hibernate 并包含文件。

      hibernate.cfg.xml 是:

      <?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>
      

      有一个 Hibernate util 类:

      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;
          }
      }
      

      我得到了这个例外:

      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)
      

      【讨论】:

        猜你喜欢
        • 2023-03-09
        • 2015-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-19
        相关资源
        最近更新 更多