I try build a program with Network/Client DB Connection support. I have done everything in DB creation and GUI design which user contact with Database. Everything is working perfect in local machine (i mean which PC does DB create and start/stop the Derby Server)
Server Configuring;
//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();
Working fine local connection;
//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);
Proplematic Remote Connection;
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(Server one) create and store the DB files. Also it has ability Start and Stop Derby Network Server when program launched. When PC1 run the program i see below lines in derby.log and understand Server is succesfully up and running;
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(Client one) try connect to remote Server with its LAN ip.
I have attach some log writer to the program and when i run the program in Client machine i see below Exception;
java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server 10.90.232.2 on port 1.527 with message Connection timed out: connect.
Really i am stuck at this point. Am i missing something?
It's probably the Windows Firewall, which is usually on by default, and which will prevent inbound connections to the Derby Network Server from another machine, unless you explicitly configure the Windows Firewall to allow such connections.
The original poster clarified that:
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 ?
You can affect the Derby Network Server's listening address and port using the -h and and -p parameters, so to conform to these firewall rules you want to say:
-h 10.90.232.2 -p 1527
The original poster confirmed that specifying the network configuration in this way allowed connections to be accepted by the Firewall:
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()) +"\""+ ";";