2011-08-04 72 views
5

我試圖恢復我們的tomcat服務器,但是這個應用程序沒有正確連接到MySQL。部署在Tomcat上的Java應用程序無法連接到jdbc-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 
+0

什麼是堆棧跟蹤?考慮使用JNDI和DataSource,而不是創建自己的連接管理。 –

+0

@ Michael-O對不起,我沒有訪問源代碼。我所知道的是,當我運行應用程序時,有一條用戶消息「連接失敗,請聯繫管理員」。如果你能幫我找到堆棧跟蹤或任何幫助的日誌文件... –

+0

檢查'logs'中的catalina.out和其他文件。如果什麼都沒有,那麼你運氣不好:-(這好像是一個黑盒子。 –

回答

4

嘗試把JDBC jar文件到tomcat-dir/common/lib和重新啓動Tomcat。
對比問題Managing libraries in Tomcat

如果這樣做還是現在的工作,從Tomcat日誌文件發佈的提取物,尋找連接JDBC作爲關鍵字。

+0

Andreas,複製lib文件到tomcat-dir/lib目錄不起作用我檢查了tomcat的日誌以及我得到的所有策略錯誤,我修復了爲所有策略文件設置'permission java.security.AllPermission;'現在,我完全失明瞭,日誌文件沒有顯示任何內容,也沒有任何錯誤 –

+0

OP正在使用Tomcat 6.您建議的文件夾特定於Tomcat 5.5或更舊版本。 – BalusC

3

我以前有完全相同的問題,並設法解決它。 Apache需要jdbc-msql的額外配置步驟。 首先,確保您擁有爲您的SQL數據庫設置的正確GRANT權限。

下一步,按照他的解決方案,它是取自:http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-j2ee.html#connector-j-usagenotes-tomcat

首先,安裝附帶連接器/ J在$ CATALINA_HOME/common/lib目錄中的.jar文件,使其可用於安裝在所有應用程序容器。

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

0

如果你正在運行:

接下來,在定義Web應用程序上下文中添加聲明,資源,$ CATALINA_HOME/conf目錄/ server.xml中配置JNDI的DataSource Tomcat with Eclipse Java EE,
1)轉到服務器選項卡
2)雙擊Tomcat服務器
3)在常規信息部分,單擊打開啓動配置。
4)轉到Classpath選項卡並添加jdbc jar文件。
希望這工作..爲我工作。

相關問題