gpt4 book ai didi

MySQL 连接 : Communications link failure

转载 作者:行者123 更新时间:2023-11-30 23:10:23 27 4
gpt4 key购买 nike

我想在外部计算机上启动我的程序。与数据库的初始连接工作正常,但是一旦第一个查询发送到数据库,我就会收到此错误:

Nov 18, 2013 10:55:24 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 08S01
Nov 18, 2013 10:55:24 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
org.hibernate.exception.JDBCConnectionException: Could not open connection
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:132)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1432)
at de.util.HibernateUtil.getList(HibernateUtil.java:230)
at de.commands.login.LoginCommand.executeServer(LoginCommand.java:35)
at de.commands.login.LoginCommand.executeServer(LoginCommand.java:20)
at de.commands.SessionManager.execute(SessionManager.java:142)
at de.commands.SessionManager.execute(SessionManager.java:124)
at de.commands.CommandExecutionThread.run(CommandExecutionThread.java:27)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:357)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2482)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2519)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2304)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:204)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
... 10 more
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:241)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:307)
... 25 more

查看其他线程后,其他人遇到了超时问题。我的问题似乎有所不同,因为它说最后收到的包裹是 0 毫秒前。此外,使用 phpmyadmin 从未引起任何问题。尽管如此,我的超时值为:

connect_timeout: 10
interactive_timeout: 28800
wait_timeout: 28800

编辑:

这是我的 hibernate 配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/ws?autoReconnect=true&user=user&password=pw</property>
[...]
</session-factory>
</hibernate-configuration>

访问数据库的方法:

public static <T> List<T> getList(DetachedCriteria criteria)
{
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = null;
List<T> l = new LinkedList<>();

try
{
session = sf.openSession();
session.beginTransaction(); // This is the line where the exception occurs
List list = criteria.getExecutableCriteria(session).list();
for (Object o : list)
{
l.add((T) o);
}
session.getTransaction().commit();
} catch (HibernateException he)
{
he.printStackTrace();
} finally
{
session.close();
}

return l;
}

最佳答案

请确保您的 JDBC 链接是正确的,我举个例子:

它应该是这样的:

return conn = DriverManager.getConnection("jdbc:mysql://"+IP+"/"+DB, User, Pass);

制作您自己的数据库并在 JDBC 链接上使用您的信息:

//DB: exampledb
//Database username: exampleusername
//Pass: examplepassword


return conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/exampledb","exampleusername", "examplepassword");

需要这些端口来连接到 MySQL。 (你可以选择 127.0.0.1:3306 或 localhost:3306)它们都是一样的。

希望这对您有所帮助。

关于MySQL 连接 : Communications link failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20045049/

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