【问题标题】:Java application deployed on Tomcat doesn't connect to jdbc-mysql部署在 Tomcat 上的 Java 应用程序未连接到 jdbc-mysql
【发布时间】:2011-10-19 23:31:28
【问题描述】:

我正在尝试恢复我们的 tomcat 服务器,但是这个应用程序无法正确连接到 MySQL。

这是发生了什么:

我有一个 Java + Flex 应用程序。整个应用程序被放置在一个目录中(不是 .war 文件)。

$TOMCAT_WEBAPP/myflex_app/WEB-INF/lib/ -> JDBC mysql driver goes here.

这是我的应用程序配置:

    <database>
            <rpgByMoodle user="moodle" password="moodle">
                    <url>jdbc:mysql://localhost:3306/rpgbymoodle</url>
            </rpgByMoodle>
            <moodle user="moodle" password="moodle">
                    <url>jdbc:mysql://localhost:3306/moodle</url>
            </moodle>
    </database>

所以,我可以通过命令行客户端连接 mysql,但应用程序不能。

我在互联网上发现有人告诉使用一些 Netbeans(或 Eclipse)菜单“添加新连接”。但是,我无法访问应用程序的源代码。

我在 Linux 上运行 tomcat。我检查了/var/log/tomcat6/ 的tomcat 日志文件,没有发现任何关于jdbc 的信息。

我的卡塔利娜日志:

Aug 4, 2011 9:24:25 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.20
Aug 4, 2011 9:24:26 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Aug 4, 2011 9:24:26 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 977 ms

【问题讨论】:

  • 什么是堆栈跟踪?考虑使用 JNDI 和 DataSource,而不是创建自己的连接管理。
  • @Michael-O 抱歉,我无权访问源代码。我所知道的是,当我运行应用程序时,会出现一条用户消息“连接失败,请联系管理员”。如果你能帮我找到堆栈跟踪或任何有帮助的日志文件......
  • 检查 logs 中的 catalina.out 和其他文件。如果什么都没有,那你就不走运了:-(这似乎是一个黑匣子。
  • 我添加了我的 catalina.*log 但是,它看起来不太有用:-(
  • 你有问题。如果没有别的。你被困住了。检查 Flex 文档以获取调试标志或类似内容。

标签: java mysql jdbc tomcat6


【解决方案1】:

尝试将 JDBC jar 文件放入 tomcat-dir/common/lib 并重新启动 Tomcat。
比较问题Managing libraries in Tomcat。

如果现在仍然有效,请从 Tomcat 日志文件中发布摘录,寻找 connection 而不是 jdbc 作为关键字。

【讨论】:

  • Andreas,将 lib 文件复制到目录 tomcat-dir/lib 不起作用。我检查了 tomcat 日志,我得到的只是一些策略错误,我修复了所有策略文件的设置 permission java.security.AllPermission;。现在,我完全失明了。日志文件不显示任何内容,任何错误。
  • OP 正在使用 Tomcat 6。您建议的文件夹特定于 Tomcat 5.5 或更早版本。
【解决方案2】:

我之前遇到过完全相同的问题并设法解决了它。 Apache 需要额外的 jdbc-msql 配置步骤。 首先,确保您为 SQL 数据库设置了正确的 GRANT 权限。

接下来,按照他的解决方案取自:http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-j2ee.html#connector-j-usagenotes-tomcat

首先,将 Connector/J 自带的 .jar 文件安装到 $CATALINA_HOME/common/lib 中,以便容器中安装的所有应用程序都可以使用它。

接下来,通过将声明资源添加到定义 Web 应用程序的上下文中的 $CATALINA_HOME/conf/server.xml 来配置 JNDI 数据源:

<Context ....>

...

<Resource name="jdbc/MySQLDB"
           auth="Container"
           type="javax.sql.DataSource"/>

<!-- The name you used above, must match _exactly_ here!

   The connection pool will be bound into JNDI with the name
   "java:/comp/env/jdbc/MySQLDB"
-->

<ResourceParams name="jdbc/MySQLDB">
<parameter>
  <name>factory</name>
  <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- Don't set this any higher than max_connections on your
     MySQL server, usually this should be a 10 or a few 10's
     of connections, not hundreds or thousands -->

<parameter>
  <name>maxActive</name>
  <value>10</value>
</parameter>

<!-- You don't want to many idle connections hanging around
     if you can avoid it, only enough to soak up a spike in
     the load -->

<parameter>
  <name>maxIdle</name>
  <value>5</value>
</parameter>

<!-- Don't use autoReconnect=true, it's going away eventually
     and it's a crutch for older connection pools that couldn't
     test connections. You need to decide whether your application
     is supposed to deal with SQLExceptions (hint, it should), and
     how much of a performance penalty you're willing to pay
     to ensure 'freshness' of the connection -->

<parameter>
  <name>validationQuery</name>
  <value>SELECT 1</value> <-- See discussion below for update to this option -->
</parameter>

<!-- The most conservative approach is to test connections
    before they're given to your application. For most applications
    this is okay, the query used above is very small and takes
    no real server resources to process, other than the time used
    to traverse the network.

    If you have a high-load application you'll need to rely on
    something else. -->

 <parameter>
  <name>testOnBorrow</name>
  <value>true</value>
 </parameter>

 <!-- Otherwise, or in addition to testOnBorrow, you can test
    while connections are sitting idle -->

  <parameter>
   <name>testWhileIdle</name>
   <value>true</value>
  </parameter>

 <!-- You have to set this value, otherwise even though
     you've asked connections to be tested while idle,
     the idle evicter thread will never run -->

 <parameter>
   <name>timeBetweenEvictionRunsMillis</name>
   <value>10000</value>
 </parameter>

 <!-- Don't allow connections to hang out idle too long,
     never longer than what wait_timeout is set to on the
     server...A few minutes or even fraction of a minute
     is sometimes okay here, it depends on your application
     and how much spikey load it will see -->

<parameter>
  <name>minEvictableIdleTimeMillis</name>
  <value>60000</value>
</parameter>

<!-- Username and password used when connecting to MySQL -->

<parameter>
 <name>username</name>
 <value>someuser</value>
</parameter>

<parameter>
 <name>password</name>
 <value>somepass</value>
</parameter>

<!-- Class name for the Connector/J driver -->

<parameter>
   <name>driverClassName</name>
   <value>com.mysql.jdbc.Driver</value>
</parameter>

<!-- The JDBC connection url for connecting to MySQL, notice
     that if you want to pass any other MySQL-specific parameters
     you should pass them here in the URL, setting them using the
     parameter tags above will have no effect, you will also
     need to use &amp; to separate parameter values as the
     ampersand is a reserved character in XML -->

<parameter>
  <name>url</name>
  <value>jdbc:mysql://localhost:3306/test</value>
</parameter>

【讨论】:

    【解决方案3】:

    如果您使用 Eclipse Java EE 运行 Tomcat,
    1) 转到服务器选项卡
    2)双击Tomcat服务器
    3)在一般信息部分,单击打开启动配置。
    4) 转到 Classpath 选项卡并添加 jdbc jar 文件。
    希望这有效..为我工作。

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 2011-09-22
      相关资源
      最近更新 更多