我正在努力让我的数据库与我的Java程序对话.
有人可以使用JDBC给我一个快速而又脏的示例程序吗?
我收到了一个相当惊人的错误:
Exception in thread "main" 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:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2260)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:787)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at SqlTest.main(SqlTest.java:22)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The …Run Code Online (Sandbox Code Playgroud) 我从生产代码中得到了这个错误:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:从服务器成功收到的最后一个数据包是36940秒之前.成功发送到服务器的最后一个数据包是36940秒前,这比服务器配置的'wait_timeout'值长.您应该考虑在应用程序中使用之前过期和/或测试连接有效性,增加服务器配置的客户端超时值,或使用Connector/J连接属性"autoReconnect = true"来避免此问题.
而现在我正试图在本地重现问题并修复它.我设置spring上下文如下:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://localhost:3306/test?userUnicode=yes&characterEncoding=UTF-8&"
p:idleConnectionTestPeriod="120" p:initialPoolSize="1" p:maxIdleTime="1800"
p:maxPoolSize="1" p:minPoolSize="1" p:checkoutTimeout="1000"
/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>
hibernate.connection.provider_class = org.hibernate.connection.C3P0ConnectionProvider
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.default_schema=platform_server_original
hibernate.show_sql=false
</value>
</property>
<property name="mappingResources">
<list>
<value>sometables.hbm.xml</value>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
然后我将我的mysql wait_timeout设置为10秒,然后运行我的测试,这基本上是打开一个连接,执行查询,关闭它,所以它返回到池中,然后将线程休眠15秒,然后再次打开连接,并再次进行查询,因此它会中断.但是,我只得到了类似的错误:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败
发送到服务器的最后一个数据包是16毫秒前.
所以我想知道这两个错误是一样的,还是它们不同?我做了一些研究,似乎两个错误都归结为同一个解决方案:使用属性"testConnectionOnCheckout = true".但是,根据c3p0 doc,这是一个非常昂贵的检查.它建议使用"idleConnectionTestPeriod",但我已经设置为120秒.我应该使用什么值才能正确验证空闲连接?
所以我基本上要问两件事:1.如何重现生产代码中的错误?2.我该如何解决?
谢谢!
我有一个Java应用程序,它在开始时打开与数据库的连接,并在最后关闭它.但是,程序并不总是完成,因为抛出异常或我正在调试它并在中途停止它.
这是否会导致打开连接以堆积和减慢数据库速度,还是会自动清理?
我"Communications link failure"在这行代码中得到错误:
mySqlCon = DriverManager.getConnection("jdbc:mysql://**server ip address**:3306/db-name", "mu-user-name", "my-password");
Run Code Online (Sandbox Code Playgroud)
我查了这篇文章中的所有内容:
当我将查询字符串更改为:
mySqlCon = DriverManager.getConnection("jdbc:mysql://**server ip address**:22/127.0.0.1:3306/db-name", "mu-user-name", "my-password");
Run Code Online (Sandbox Code Playgroud)
我收到了错误 Packet for query is too large (4739923 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
虽然我已经改变了my.cnf上的数据包大小,然后重新启动了mysql服务.
有什么建议?
注意:
我可以通过ssh与这段代码连接,但这种方式似乎不合理!我可以在main中连接一次然后我应该将连接传递给所有类.
public my-class-constructor() {
try {
go();
} catch (Exception e) {
e.printStackTrace();
}
mySqlCon = null; …Run Code Online (Sandbox Code Playgroud) 我收到以下错误。检查许多解决方案,但没有得到结果。
未连接到数据库-网络编码 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 错误:java.lang.NullPointerException 成功发送到服务器的最后一个数据包是在0毫秒之前。 驱动程序尚未收到来自服务器的任何数据包。
以下是数据库连接代码。
public class DBConnect {
// System.out.println("MySQL Connect Example.");
public static Connection getConnection() {
Connection conn = null;
String url = "jdbc:mysql://127.0.0.1:3306/";
String dbName = "networkcoding";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "xampp123";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Connected to the database "+dbName);
//conn.close();
//System.out.println("Disconnected from database");
} catch (Exception e) {
System.out.println("Not Connected to the database - "+dbName);
e.printStackTrace();
}
return conn;
} …Run Code Online (Sandbox Code Playgroud)