gpt4 book ai didi

java - 使用 Glassfish 打开与 Microsoft SQL 的连接时崩溃

转载 作者:行者123 更新时间:2023-11-30 06:01:26 25 4
gpt4 key购买 nike

我正在使用 GlassFish 5.0.0 开发 JavaEE 8 应用程序,我想访问驻留在 Microsoft SQL Server 中的数据库。我在 Maven 中附加了 Microsoft SQL 驱动程序 7.0 jre8。

当我部署应用程序并尝试打开访问数据库的页面时,我遇到了崩溃。下面是 StackTrace。

java.lang.NoSuchMethodError: sun.security.internal.spec.TlsMasterSecretParameterSpec.getExtendedMasterSecretSessionHash()[B
at com.sun.crypto.provider.TlsMasterSecretGenerator.engineGenerateKey(TlsMasterSecretGenerator.java:107)
at javax.crypto.KeyGenerator.generateKey(KeyGenerator.java:546)
at sun.security.ssl.Handshaker.calculateMasterSecret(Handshaker.java:1174)
at sun.security.ssl.Handshaker.calculateKeys(Handshaker.java:1120)
at sun.security.ssl.ClientHandshaker.serverHelloDone(ClientHandshaker.java:1126)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:351)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:984)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:919)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1756)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2391)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2042)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1889)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1120)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:700)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at datas.DatabaseDatas.getLelang(DatabaseDatas.java:19)
at org.apache.jsp.company.halaman_005futama_005fcompany_jsp._jspService(halaman_005futama_005fcompany_jsp.java:595)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:258)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
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:242)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
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:593)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
at java.lang.Thread.run(Thread.java:748)

触发崩溃的代码在这里:

public class DatabaseDatas {
private final static String sqlconn = "jdbc:sqlserver://<redacted>;" +
"databaseName=dbName";

/**
* Gets Lelang data in LelangList
* @return Data in LelangList table
*/
public static ResultSet getLelang(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection sqlconnection = DriverManager.getConnection(sqlconn,"sa","password"); //this is the one which trigger the crash
sqlconnection.isValid(0); //ignore this
Statement sta = sqlconnection.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
String sql = "SELECT * FROM ListLelang";
return sta.executeQuery(sql);
} catch (ClassNotFoundException|SQLException e) {
e.printStackTrace();
}
return null;
}

我使用 Jetbrains Intellij IDEA 2018.2.3 作为 IDE,并将 JDK 1.8.0_151 设置为 Project SDK。我还在 GlassFish 5 管理面板的 JDBC 连接池中设置了 javax.sql.DataSource 和 java.sql.driver。

我不知道为什么会发生这种情况。我在互联网上的任何地方都找不到我的错误。请帮我解决这个问题。如果需要,我可以提供更多信息(只要不是个人信息)。谢谢。

编辑:实际上我忘记测试连接池。当我尝试通过 Glassfish 5 管理面板“ping”后,我得到了与上面堆栈跟踪中的错误相同的错误。 Glassfish admin panel error when pinging to MSSQL connection

最佳答案

使用 Java EE,您不应使用 DriverManager 手动打开与 SQL-Server 的数据库连接。您的情况中的应用程序服务器(Glassfish)负责打开连接并将它们汇集起来。

正如您所说,您已经为 DataSourceJDBC 连接池 配置了 Glassfish,您无需在代码中再次执行此操作。现在,您可以使用 DataSource 通过 @Resource(lookup="JNDI_NAME_OF_YOUR_DATASOURCE") DataSource dataSource; 将其注入(inject)代码(EJB 或 CDI)中并使用它。您可以通过 dataSource.getConnection(); 获取 Connection

如果您计划使用 EntityManager 并使用 JPA,则您的应用程序中的以下目录中需要有一个 persistence.xml 文件:src/main/resources/META-INF,它可能如下所示:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
version="2.2">
<persistence-unit name="prod" transaction-type="JTA">
      <jta-data-source>JNDI_NAME_OF_YOUR_CONFIGURED_DATASOURCE</jta-data-source>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>

通过此设置,您现在可以使用 @Resource EntityManagerEntityManager; 在 CDI 或 EJB bean 中注入(inject) EntityManager 并使用它。

通过这种方法,您可以确保应用程序服务器正在管理所有与基础设施相关的内容(处理数据库),并且您可以专注于业务逻辑。

以下教程可能会为您提供进一步帮助:https://javaee.github.io/tutorial/resource-creation002.html

关于java - 使用 Glassfish 打开与 Microsoft SQL 的连接时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52221412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com