【问题标题】:After Deployment of .war File, Unable to Connect to Database in Glassfish部署 .war 文件后,无法连接到 Glassfish 中的数据库
【发布时间】:2016-04-11 08:43:30
【问题描述】:

我在大学时对 Java-EE 非常陌生,并且在尝试完成我的项目后,该项目与 Netbeans IDE 中的数据库连接完美配合。我尝试在不打开 Netbeans IDE 的情况下将 *.war 文件部署到本地主机服务器(服务器以 admin start-domain domain1 和 *.war 的命令启动文件在应用程序列表中),当我在 localhost:8080 中启动项目后,数据库连接失败,我的 web 应用程序无法工作。

有人知道如何解决吗? Exception picture

服务器日志:

[2016-04-11T16:24:50.888+0800] [Payara 4.1] [SEVERE] [] [] [tid: _ThreadID=40 _ThreadName=admin-listener(3)] [timeMillis: 1460363090888] [levelValue: 1000] [[
  Mon Apr 11 16:24:50 SGT 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.]]

[2016-04-11T16:36:55.713+0800] [Payara 4.1] [SEVERE] [] [] [tid: _ThreadID=40 _ThreadName=admin-listener(3)] [timeMillis: 1460363815713] [levelValue: 1000] [[
  Mon Apr 11 16:36:55 SGT 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.]]

[2016-04-11T16:50:35.840+0800] [Payara 4.1] [INFO] [] [javax.enterprise.resource.resourceadapter.com.sun.enterprise.connectors] [tid: _ThreadID=27 _ThreadName=http-listener-1(5)] [timeMillis: 1460364635840] [levelValue: 800] [[
  RAR7099: The mcf being used to allocate the connection and the MCF in the pool are not equal.]]

[2016-04-11T16:50:35.844+0800] [Payara 4.1] [WARNING] [] [javax.enterprise.resource.resourceadapter.com.sun.enterprise.resource.allocator] [tid: _ThreadID=27 _ThreadName=http-listener-1(5)] [timeMillis: 1460364635844] [levelValue: 900] [[
  RAR5038:Unexpected exception while creating resource for pool foodPool. Exception : javax.resource.spi.SecurityException: No password credential found]]

[2016-04-11T16:50:35.845+0800] [Payara 4.1] [WARNING] [] [javax.enterprise.resource.resourceadapter.com.sun.enterprise.connectors] [tid: _ThreadID=27 _ThreadName=http-listener-1(5)] [timeMillis: 1460364635845] [levelValue: 900] [[
  RAR5117 : Failed to obtain/create connection from connection pool [ foodPool ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: No password credential found]]

[2016-04-11T16:50:35.846+0800] [Payara 4.1] [WARNING] [] [javax.enterprise.resource.resourceadapter.com.sun.gjc.spi] [tid: _ThreadID=27 _ThreadName=http-listener-1(5)] [timeMillis: 1460364635846] [levelValue: 900] [[
  RAR5114 : Error allocating connection : [No password credential found]]]

[2016-04-11T16:50:35.848+0800] [Payara 4.1] [INFO] [] [] [tid: _ThreadID=27 _ThreadName=http-listener-1(5)] [timeMillis: 1460364635848] [levelValue: 800] [[
  No password credential found]]

[2016-04-11T16:50:35.849+0800] [Payara 4.1] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=27 _ThreadName=http-listener-1(5)] [timeMillis: 1460364635849] [levelValue: 900] [[
  StandardWrapperValve[login]: Servlet.service() for servlet login threw exception
java.lang.NullPointerException
    at servlet.login.doPost(login.java:146)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Unknown Source)
]]

连接小服务程序:

package object;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Richard
 */
public class connection {

    private InitialContext ctx;

    public InitialContext getCtx() {
        return ctx;
    }

    public void setCtx(InitialContext ctx) {
        this.ctx = ctx;
    }

    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public Statement getStmt() {
        return stmt;
    }

    public void setStmt(Statement stmt) {
        this.stmt = stmt;
    }

    public ResultSet getRs() {
        return rs;
    }

    public void setRs(ResultSet rs) {
        this.rs = rs;
    }
    private DataSource ds;
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;

    public connection(){

        try{

            ctx = new InitialContext();
            ds = (DataSource)ctx.lookup("jdbc/fastfood");

        }catch(NamingException ex)
        {


        }
    }

public DataSource getDataSource(){
    return ds;
}
}

【问题讨论】:

  • 请在此处显示您的例外情况
  • @AhmedGamal 添加异常图片
  • 空指针异常,为什么确定是db连接错误,请上传服务器日志
  • @AhmedGamal hi,服务器日志已添加
  • 未提供密码,请显示您的连接代码......分配连接时出错:[未找到密码凭据]]]

标签: jakarta-ee glassfish netbeans-8


【解决方案1】:

在 Java EE 中,您不应手动查找连接,而应使用在应用程序服务器中配置的数据源 (https://docs.oracle.com/javase/8/docs/api/index.html?javax/sql/DataSource.html)。

然后您可以将数据源注入到您的组件中,如下所述:https://docs.oracle.com/javaee/6/tutorial/doc/bncjk.html。 作为替代方案,您可以执行 JNDI 查找来获取数据源。

如果您使用 JPA 进行数据库访问,您的连接将通过在 persistence.xml 中提供其 JNDI 名称来获取,并且您只需通过 @PersistenceContext 注释 (https://docs.oracle.com/javaee/7/api/javax/persistence/PersistenceContext.html) 注入对 EntityManager 的引用

【讨论】:

    【解决方案2】:

    尝试更改查找语法如下:

    ds = (DataSource)ctx.lookup("java:jdbc/fastfood");
    

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 2020-09-27
      • 1970-01-01
      • 2015-03-11
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      相关资源
      最近更新 更多