由于驱动程序管理器未正确初始化,因此无法获取连接

Cannot get a connection as the driver manager is not properly initialized

提问人:Massdey 提问时间:11/6/2023 最后编辑:BalusCMassdey 更新时间:11/6/2023 访问量:42

问:

Glassfish 错误日志:

[2023-11-05T19:42:14.756361+01:00] [GF 7.0.9] [WARN] [] [org.hibernate.orm.deprecation] [tid: _ThreadID=71 _ThreadName=http-listener-1(1)] [levelValue: 900] [[
  HHH90000028: Support for `<hibernate-mappings/>` is deprecated [RESOURCE : UsersEntity.hbm.xml]; migrate to orm.xml or mapping.xml, or enable `hibernate.transform_hbm_xml.enabled` for on the fly transformation]]

[2023-11-05T19:42:14.799953+01:00] [GF 7.0.9] [WARN] [] [org.hibernate.orm.connections.pooling] [tid: _ThreadID=71 _ThreadName=http-listener-1(1)] [levelValue: 900] [[
  HHH10001002: Using built-in connection pool (not intended for production use)]]

[2023-11-05T19:42:15.015709+01:00] [GF 7.0.9] [WARN] [] [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] [tid: _ThreadID=71 _ThreadName=http-listener-1(1)] [levelValue: 900] [[
  HHH000342: Could not obtain connection to query metadata
java.lang.IllegalStateException: Cannot get a connection as the driver manager is not properly initialized
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:262)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:316)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:152)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:34)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:119)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:264)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:239)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:216)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:45)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:125)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:248)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:216)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:121)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:43)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:61)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections.addConnections(DriverManagerConnectionProviderImpl.java:501)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections.<init>(DriverManagerConnectionProviderImpl.java:367)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections$Builder.build(DriverManagerConnectionProviderImpl.java:552)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:105)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:81)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:125)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:248)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:216)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:283)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:150)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:34)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:119)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:264)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:239)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:216)
    at org.hibernate.boot.model.relational.Database.<init>(Database.java:45)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.getDatabase(InFlightMetadataCollectorImpl.java:230)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:198)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:166)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:125)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:451)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:102)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:863)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:913)
    at com.example.jeeapplication.HibernateUtil.<clinit>(HibernateUtil.java:16)
    at com.example.jeeapplication.BusinessLayer.RegisterServlet.doPost(RegisterServlet.java:37)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:547)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:614)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1368)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:120)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:562)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:503)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:71)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:121)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:295)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:188)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:425)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:144)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:174)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:153)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:196)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:88)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:246)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:178)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:118)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:96)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:51)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:510)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:82)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:83)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:101)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:535)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:515)
    at java.base/java.lang.Thread.run(Thread.java:1623)
]]

[2023-11-05T19:42:15.024216+01:00] [GF 7.0.9] [SEVERE] [] [jakarta.enterprise.logging.stderr] [tid: _ThreadID=71 _ThreadName=http-listener-1(1)] [levelValue: 1000] [[
  Initial SessionFactorycreation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Error calling Driver#connect [null]]]

我的冬眠.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/database1</property>
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">x</property>
        <mapping class="com.example.jeeapplication.Beans.UsersEntity"/>
        <mapping resource="UsersEntity.hbm.xml"/>

        <!-- <property name="connection.password"/> -->

        <!-- DB schema will be updated if needed -->
        <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>

我的application.properties:

hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
javax.persistence.jdbc.driver=com.mysql.cj.jdbc.Driver
javax.persistence.jdbc.url=jdbc:mysql://localhost:3306/database1?createDatabaseIfNotExist=true
javax.persistence.jdbc.user=root
javax.persistence.jdbc.password=root
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.use_sql_comments=true
hibernate.hbm2ddl.auto=update

我尝试在其中调用会话工厂的 servlet

package com.example.jeeapplication.BusinessLayer;

import com.example.jeeapplication.Beans.UsersEntity;
import com.example.jeeapplication.DAO.UserDAOImpl;
import com.example.jeeapplication.HibernateUtil;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.time.LocalDate;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/html");
        String name = request.getParameter("name");
        String surname = request.getParameter("surname");
        String username = request.getParameter("username");
        String email = request.getParameter("email");
        LocalDate birthDate = LocalDate.parse(request.getParameter("birthDate"));
        String gender = request.getParameter("gender");
        String password = request.getParameter("password");

        boolean basicUser = false;

        UsersEntity newUser = new UsersEntity(name,surname,username,email,birthDate,gender,password,basicUser,basicUser);

        /*System.out.println(newUser.toString());
        UserDAOImpl userDao = new UserDAOImpl();
        userDao.addUser(newUser);*/

        UserDAOImpl dao = new UserDAOImpl(HibernateUtil.getSessionFactory());
        dao.addUser(newUser);

        // Exemple de réponse renvoyée au client
        response.getWriter().println("<html><body>");
        response.getWriter().println("<h2>Informations reçues :</h2>");
        response.getWriter().println("<p>Nom : " + name + "</p>");
        response.getWriter().println("<p>Prénom : " + surname + "</p>");
        response.getWriter().println("</body></html>");

    }

    public void destroy() {
    }
}

提前感谢您的帮助,我不知道如何解决这个问题。我在这个网站或其他论坛上找到了类似的主题,但没有找到解决我的问题的有效方法。

java mysql 休眠

评论

0赞 BalusC 11/6/2023
警告:您标记了 [jpa] 和 [jakarta-ee],但实际上您并没有使用它们。你基本上是在尝试以Java SE(或者Spring)的方式使用Hibernate。虽然这也可以,但我强烈建议退后一步,阅读一本权威的 JPA 书籍或教程,学习如何真正通过仅导入而不进行任何导入来真正使用 JPA。此外,无需手动安装 Hibernate 库,GlassFish 作为普通的 JEE 服务器,已经将其作为 JPA 实现提供开箱即用。jakarta.persistence.*org.hibernate.*
0赞 Massdey 11/6/2023
是的,我知道这一点。我最终使用了 JPA 而不是 hibernate(我使用了 entitymanagers 而不是 sessionfactory),我从 glassfish 切换到了 tomcat,我的查询最终起作用了。如果这一切都能帮助任何人摆脱我所处的境地。

答: 暂无答案