簡體   English   中英

Derby遠程連接超時:如何為Derby Network Server配置網絡設置?

[英]Derby remote connection timed-out: how do I configure the network settings for the Derby Network Server?

我嘗試構建具有網絡/客戶端數據庫連接支持的程序。 我已經完成了數據庫創建和GUI設計中與用戶聯系的所有工作。 一切都在本地計算機上運行完美(我的意思是DB可以創建PC並啟動/停止Derby Server)

服務器配置;

//I take the exact path of derby jar files (derbyrun, derbynet, derby client etc.)
File file = new File(FirstTimeMainFrame.class.getProtectionDomain()
                .getCodeSource()
                .getLocation()
                .getPath().replace(new File(FirstTimeMainFrame.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName(), "").replace("%20", " "));
String path = file+"\\DB";

//Execute CMD Command for derbyrun.jar set the DERBY INSTALL Classpath
ProcessBuilder builder = new ProcessBuilder();
Process process = null;
String[] commandStart = new String[3];
String[] commandStop = new String[3];

commandStart[0] = "cmd.exe";
commandStart[1] = "/c";
commandStart[2] = "cd "+path+" && java -jar derbyrun.jar server start";

builder = new ProcessBuilder(commandStart[0], commandStart[1], commandStart[2]);
builder.redirectErrorStream(true);
process = builder.start();

//Create the DB with using Derby Client Driver
String driver = "org.apache.derby.jdbc.ClientDriver";
String connectionURL = "jdbc:derby://localhost:1527//myDB"+";create=true";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(connectionURL);

//Connect to DB and Setting the DB Properties
connectionURL = "jdbc:derby://localhost:1527//myDB"+";create=false";
conn = DriverManager.getConnection(connectionURL);
//Turning on authentication, Create sample users, Create sample tables etc...

//Shutdown the Derby Server
commandStop[0] = "cmd.exe";
commandStop[1] = "/c";
commandStop[2] = "cd "+path+" && java -jar derbyrun.jar server shutdown";

builder = new ProcessBuilder(commandStop[0], commandStop[1], commandStop[2]);
builder.redirectErrorStream(true);
process = builder.start();

工作良好的本地連接;

//Start the Server First
builder = new ProcessBuilder(commandStart[0], commandStart[1], commandStart[2]);
builder.redirectErrorStream(true);
process = builder.start();

//Load the driver and connect to Local DB
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
String connectionUrl = "jdbc:derby://localhost:1527//myDB"+";create=false;" + "user=" +"\""+ unameTextField.getText() +"\""+ ";" + "password=" +"\""+ new String (passwordPasswordField.getPassword()) +"\""+ ";"; 
Connection con = DriverManager.getConnection(connectionUrl);

完善的遠程連接;

Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
String connectionUrl = "jdbc:derby://10.90.232.2:1527//myDB"+";create=false;" + "user=" +"\""+ unameTextField.getText() +"\""+ ";" + "password=" +"\""+ new String (passwordPasswordField.getPassword()) +"\""+ ";"; 
Connection con = DriverManager.getConnection(connectionUrl);

--PC1(服務器一)創建並存儲數據庫文件。 程序啟動時,它還具有啟動和停止Derby Network Server的功能。 當PC1運行程序時,我在derby.log中看到以下幾行,並了解Server已成功啟動並正在運行;

Sat Dec 26 04:06:49 EET 2015 : Apache Derby Network Server - 10.12.1.1 - (1704137) started and ready to accept connections on port 1527
Sat Dec 26 04:06:49 EET 2015: Booting Derby version The Apache Software Foundation - Apache Derby - 10.12.1.1 - (1704137): instance a816c00e-0151-dc09-cf3e-ffffaab85dad on database directory C:\Users\Server_PC\Desktop\Trying Project\DB\myDB with class loader sun.misc.Launcher$AppClassLoader@1909752 Loaded from file:/C:/Users/Server_PC/Desktop/Trying%20Project/DB/derby.jar
java.vendor=Oracle Corporation
java.runtime.version=1.8.0_66-b18
user.dir=C:\Users\Server_PC\Desktop\Trying Project\DB
os.name=Windows 7
os.arch=x86
os.version=6.1
derby.system.home=C:\Users\Server_PC\Desktop\Trying Project\DB
Database Class Loader started - derby.database.classpath=''

--PC2(客戶端一)嘗試使用其LAN ip連接到遠程服務器。

我在程序上附加了一些日志編寫器,當我在客戶端計算機上運行該程序時,出現以下異常;

java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server 10.90.232.2 on port 1.527 with message Connection timed out: connect.

真的,我被困在這一點上。 我想念什么嗎?

可能是Windows防火牆,該防火牆通常默認情況下處於啟用狀態,並且將阻止從另一台機器到Derby Network Server的入站連接,除非您明確配置Windows防火牆以允許此類連接。

原始海報闡明:

I forgot to say, sorry: I have already added Firewall Port rule 
for 1527 to Inbound and Outbound in PC1. Interesting thing is
when I check netroutes with netstat -an | find "1527" in PC1 I
see 127.0.0.1 1527 is in LISTENING state.

It should be 10.90.232.2 1527 ?

您可以使用-h和-p參數來影響Derby Network Server的偵聽地址和端口,因此要遵循以下防火牆規則,您需要說:

-h 10.90.232.2 -p 1527

原始發布者確認,以這種方式指定網絡配置允許防火牆接受連接:

Exactly right. I found solution about 6-7 hours ago. 
For server Start should be 

commandStart[2] = "cd "+path+" && java -jar derbyrun.jar server start -h 10.90.232.2"; 

and shutdown 

commandStop[2] = "cd "+path+" && java -jar derbyrun.jar server shutdown -h 10.90.232.2"; 

then server and client side connect with 

String connectionUrl = "jdbc:derby://10.90.232.2:1527/myDB"+
    ";create=false;" + "user=" +"\""+ unameTextField.getText() +
    "\""+ ";" + "password=" +"\""+ 
    new String (passwordPasswordField.getPassword()) +"\""+ ";";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM