【问题标题】:Configure glassfish to deploy a .war and use jdbc配置 glassfish 以部署 .war 并使用 jdbc
【发布时间】:2014-04-27 15:08:46
【问题描述】:

这是我在这个论坛上的第一个问题,所以我希望你能理解我的问题!

我在 Glassfish 中部署应用时遇到了问题。

我一直在开发一个 Web 应用程序,我必须访问两个不同的数据库,一个 derby 数据库和一个 mysql 数据库。 从 NetBeans IDE 7.4 开发时,没有任何问题,我使用 SessionBean 来访问两个数据库,配置连接太容易了。 我在构造函数中写了这个

Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
    connectionSoria=DriverManager.getConnection("jdbc:derby://localhost:1527/WEBAPP-Soria","App","App");
    Class.forName("com.mysql.jdbc.Driver").newInstance();
  connectionTeruel=DriverManager.getConnection("jdbc:mysql://localhost:3306/webapp_teruel","root","root");

一切正常。

当我构建 .战争,我试图将它部署在另一台没有 NetBeans 的计算机上。 我成功部署了它,但是当我访问该应用程序时,它要求我输入用户名和密码,我将它们都写了,按登录,它似乎没有与任何数据库连接。 我进行了搜索,发现当我从 Glassfish 的 bin 目录中的 asadmin 部署应用程序时,我不得不更改连接到数据库的方式。

我在连接数据库的bean的构造函数中写的是这样的:

ctx1 = new InitialContext();
            ds1 = (DataSource)ctx1.lookup("jdbc/WEBAPP-Soria");
            ds2 = (DataSource)ctx1.lookup("jdbc/WEBAPP_Teruel");

            connectionSoria=ds1.getConnection();
            connectionTeruel=ds2.getConnection();

我用

向 glasssfish 添加了两个资源
C:\glassfishv3\asadmin
asadmin> start-domain
asadmin> deploy E:\AppV2.war
asadmin> add-resources E:\resources.xml
asadmin> restart-domain

resources.xml 的内容是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-resource enabled="true" jndi-name="jdbc/WEBAPP-Soria" object-type="user" pool-name="WEBAPP-Soria">
<description/>
</jdbc-resource>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.apache.derby.jdbc.ClientDriver" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="WEBAPP-Soria" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="port" value="1527"/>
<property name="databaseName" value="WEBAPP-Soria"/>
<property name="serverName" value="localhost"/>
<property name="url" value="jdbc:derby://localhost:1527/WEBAPP-Soria"/>
<property name="user" value="App"/>
<property name="password" value="App"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="jdbc/WEBAPP_Teruel" object-type="user" pool-name="WEBAPP_Teruel">
<description/>
</jdbc-resource>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.Driver" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="WEBAPP_Teruel" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="port" value="3306"/>
<property name="databaseName" value="WEBAPP_Teruel"/>
<property name="serverName" value="localhost"/>
<property name="url" value="jdbc:mysql://localhost:3306/WEBAPP_Teruel"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</jdbc-connection-pool>
</resources>

无论如何它都不起作用,我想我应该将这个 resources.xml 添加到 WEB-INF 目录中作为 glassfish-resources.xml 但我不知道这是否会起作用。

你们建议我应该怎么做? 提前非常感谢!

【问题讨论】:

    标签: mysql jdbc glassfish derby war


    【解决方案1】:

    由于您的应用程序指定

    jdbc:derby://localhost:1527/WEBAPP-Soria
    

    作为它的 JDBC 连接 URL,它期望连接到与您的应用程序位于同一台机器(“localhost”)上的 Derby Network Server 实例。

    因此,当您部署应用程序时,您还需要部署 Derby Network Server。

    或者,如果您的意图是在多台计算机上部署应用程序,然后让它们都连接到 Derby 网络服务器的同一个实例,则您需要指定该单个共享实例的主机名或 IP 地址“localhost”(并确保您的网络允许建立这些连接)。

    【讨论】:

      猜你喜欢
      • 2014-12-23
      • 2016-04-29
      • 2016-05-01
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      相关资源
      最近更新 更多