为了连接到数据库,我在尝试使用资源行时使用了此命令,然后输入了PreparedStatement:
try(conn = C3P0DataSource.getInstance().getConnection()){
.
.//Prepared Statement
.
}catch(SQLException e){
}
我的C3P0DataSource类是:
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0DataSource {
private static C3P0DataSource dataSource;
private ComboPooledDataSource comboPooledDataSource;
private C3P0DataSource() {
try {
comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass("org.postgresql.Driver");
comboPooledDataSource.setJdbcUrl("Server Address");
comboPooledDataSource.setUser("My username");
comboPooledDataSource.setPassword("My password");
comboPooledDataSource.setMaxStatements( 180 );
comboPooledDataSource.setMaxIdleTime(100);
}catch (PropertyVetoException ex1) {
JOptionPane.showMessageDialog(Main.splash_screen,
"Missing PostgreSQL JDBC Driver",
"Inane error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
public static C3P0DataSource getInstance() {
if (dataSource == null)
dataSource = new C3P0DataSource();
return dataSource;
}
public Connection getConnection() {
Connection con = null;
try {
con = comboPooledDataSource.getConnection();
if(con==null) {
JOptionPane.showMessageDialog(Main.splash_screen,
"Connection couldn`t established...",
"Inane error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(Main.splash_screen,
"Connection couldn`t established...",
"Inane error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
return con;
}
}
我正在使用这种方法通过VPN连接到服务器中的PostgreSQL数据库。当我有互联网连接并且VPN开启时,它可以正常工作。但是,当我有意关闭Internet连接或VPN观察正在发生的事情时,它进入无限循环,有很多例外:
(2018年8月13日,下午11:28:49 org.postgresql.Driver connect 严重:连接错误: org.postgresql.util.PSQLException:连接尝试失败。 在org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:257) 在org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) 在org.postgresql.jdbc.PgConnection。(PgConnection.java:195) 在org.postgresql.Driver.makeConnection(Driver.java:452) 在org.postgresql.Driver.connect(Driver.java:254) 在com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175) 在com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220) 在com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206) 在com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool $ 1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203) 在com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138) 在com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)中 在com.mchange.v2.resourcepool.BasicResourcePool.access $ 700(BasicResourcePool.java:44) 在com.mchange.v2.resourcepool.BasicResourcePool $ ScatteredAcquireTask.run(BasicResourcePool.java:1870) 在com.mchange.v2.async.ThreadPoolAsynchronousRunner $ PoolThread.run(ThreadPoolAsynchronousRunner.java:696) 造成原因:java.net.SocketTimeoutException:连接超时 在java.base / java.net.PlainSocketImpl.socketConnect(本机方法) 在java.base / java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400) 在java.base / java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243) 在java.base / java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225) 在java.base / java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402) 在java.base / java.net.Socket.connect(Socket.java:591) 在org.postgresql.core.PGStream。(PGStream.java:69) 在org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:156) ...还有13个)
直到我重新打开互联网连接。我的预期结果是在此过程结束时放置一个超时时间,并弹出上面显示的JOptionPane,然后退出。我该如何实现?