Java derby database server won't start anymore - java

I inherited some source code that uses a derby database but starting the server doesn't work anymore.
public void startDatabase(){
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
System.setProperty("derby.system.home", "D:\\runtime-my.product\\log");
NetworkServerControl nsc = new NetworkServerControl(InetAddress.getByName("localhost"), 1527)
nsc.start(null);
nsc.ping();
catch (Exception e){
e.printStackTrace();
}
}
When nsc.ping() is exectued, the following exception is thrown:
Exception: java.lang.Exception: DRDA_NoIO.S:Could not connect to Derby Network Server on host 127.0.0.1, port 1527: Connection refused: connect
Is there anything obvious missing or wrong with those lines of codes?

Check whether the server is started. You need to start the server explicitly. or via
setting the system property derby.drda.startNetworkServer=true.

It takes a while from the NetworkServerControl constructor returns, before the socket is ready for connections. On my machine about 50 ms. I changed your example like so:
NetworkServerControl nsc = new NetworkServerControl(InetAddress.getByName("localhost"), 1527);
nsc.start(null);
for (int i = 0; i < 10; ++i) {
try {
System.out.println("Attempting to ping...");
nsc.ping();
break;
} catch (Exception e) {
System.out.println(e.getMessage());
}
Thread.sleep(10);
}
It succeeded on the 5. attempt...

Related

Unable to connect to Oracle database using the JAR file

I'm using eclipse 2020 edition and I've added all libraries I need to connect to Oracle server like ojdbc7.jar and my code is like this:
public Connection SetDatabaseConnection() {
writeInLog("Connecting to IRB", 0);
if(openConnection()){
try {
productionPool.setDriverType("thin");
productionPool.setUser(username);
productionPool.setPassword(password);
productionPool.setPortNumber(Integer.parseInt(port));
productionPool.setServerName(IP);
productionPool.setServiceName(serviceName);
productionPool.setURL("jdbc:oracle:thin:#"+ _connStr.substring(_connStr.indexOf(":")+1));
productionPooledConnection = productionPool.getPooledConnection();
if (productionPooledConnection != null) {
//return true;
currentConnection = productionPooledConnection.getConnection();
logger.info("Connected to IRB server");
return currentConnection;
}
} catch (SQLException ex) {
logger.info("Unable to connect to IRB server, SQLException: "+ex.getMessage());
System.out.println(" (IRB-Exception) DB Exception: \n"+ ex);
}
}
}
my problem is: i can connect to the server while debugging or running the application in the eclipse but when I exported a JAR file the application stopped in this step.
in addition:
my code to open a connection:
private boolean openConnection(){
try {
productionPool = new OracleConnectionPoolDataSource();
productionPooledConnection = new OraclePooledConnection();
logger.info("openConnection(): Connected to IRB server \n");
return true;
} catch (SQLException e) {
e.printStackTrace();
logger.info("Unable to connect to IRB server , SQLException: "+e.getMessage());
}
logger.info("openConnection(): Unable to connect to IRB server \n");
return false;
}
The application never throws any excption it only write in the log file this statment: writeInLog("Connecting to IRB", 0);
I couldn't find the exact reason why this happened but I removed the JARs that cause the error FAT Jar Export: couldn't find the class-path for ...etc and import them again. It worked successfully.

Java Socket connect gives success when connecting to an unknown IP address... why?

I would like to connect by TCP to a machine given its IP address and its port.
I decided to use the JAVA java.net.Socket class and its connect method as it seems to fit my need.
For testing purpose, as the machines I need to test are not yet available, I tried to connect a local machine on its standard port 80.
But I was very surprised to see that it succeeded with any IP address, even an unknown one: I cannot "ping" it, but the connect method gives me success...
Did I missed something in JAVA Socket understanding?
How is this behaviour possible?
Here is my code :
private void testSocketConnection() {
try (Socket socketToMachine = new Socket()) {
InetSocketAddress address = new InetSocketAddress(InetAddress.getByName("1.2.3.4"), 80);
socketToMachine.connect(address, 1000);
System.out.println("SUCCESS");
} catch (UnknownHostException uhe) {
System.out.println("UnknownHostException");
} catch (IOException ioe) {
System.out.println("IOException");
}
}

Slow connection after connection to database with web-hosting

Heading ##I have problem with my java application with database in mySQL and swing GUI.
When I've used localhost everything worked properly. But now I would like to share project with my friend and we decided to use server hosting.
And here is a problem:
Now application works very slow, after pressing a button I have to wait a few seconds for the program to respond. Also the connection is lost from time to time. I have no idea where can I find reason for the problem... Do somebody now what is the reason of this problem?
private static Connection connection = null;
Boolean error = false;
private static String jdbcURL = "jdbc:mysql://host_name:3306/db_name";
private static String user = "user";
private static String password = "password";
MakeConnection(Connection connection) throws SQLException{
this.connection = connection;
try {
getConnection();
System.out.print("Connected to the data base in MakeConnection");
}
catch(Exception e) {
error = true;
System.out.print("Error in connection in MakeConnection consturctor" + e.getMessage());
}
finally{
if(connection!=null) connection.close();
if(error) System.out.print("Problem with connection");
else System.out.print("Program finished");
}
}
public Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(jdbcURL,user,password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
Also sometimes application shows this error:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
I don't see any problem in your code. The problem is probably with your server hosting. You should check the country of the host provider and measure the time required to send a request to the server. Also you should use logger instead of System.out.println so you can examine required time for actions like db access, application logic and find a bottleneck.

"Invalid connection string format" error when trying to connect to Oracle using TNS alias that contains dot character

I am trying to connect to Oracle database using TNS.
The problem is that TNS alias contains dot, so when I am specifying url like this:
jdbc:oracle:thin:#TNS.ALIAS
I've got...
oracle.net.ns.NetException: Invalid connection string format, a valid format is: "host:port:sid"
...during creation of connection.
I know that dot character is a problem, because after removing it from tnsnames.ora file connection to database works.
My question is - is it possible to escape dot character somehow? Maybe there is some connection parameter that can be setup to allow dot character in alias? I would like to avoid removing dot from tnsnames.ora since i am getting the file from outside source.
Here are the options that I've already tried that gave me the same error:
jdbc:oracle:thin:#"TNS.ALIAS"
jdbc:oracle:thin:#\"TNS.ALIAS\"
jdbc:oracle:thin:#`TNS.ALIAS`
jdbc:oracle:thin:#TNS\.ALIAS - this one is not compiling
jdbc:oracle:thin:#TNS\\.ALIAS
jdbc:oracle:thin:#TNS.ALIAS
jdbc:oracle:thin:#TNS\".\"ALIAS
jdbc:oracle:thin:#TNS%2eALIAS
Here are the options that resulted with oracle.net.ns.NetException: could not resolve the connect identifier:
jdbc:oracle:thin:#TNSALIAS
jdbc:oracle:thin:#TNS-ALIAS
jdbc:oracle:thin:#TNS_ALIAS
Additional context:
I am trying to create Java's DataSource (OracleDataSource to be
strict) in Scala (it is Play Framework - but I am not using Play's
way of creation of DB connections - I am doing it manually)
I have SQL Developer that is using exactly the same tnsnames.ora file and it is working there
We are having C# applications that are using exactly the same tnsnames.ora file and it is working there (data source is defined like this: <add name="connectionName" connectionString="Data Source=TNS.ALIAS;"/>
You need set
System.setProperty("oracle.net.tns_admin","C:\\app\\product\\12.2.0\\client_1\\network\\admin");
set to the location of the tnsnames.ora
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnect {
public Connection connection;
public DBConnect() {
}
public void connect() throws Exception {
String connectString;
System.setProperty("oracle.net.tns_admin", "C:\\app\\product\\12.2.0\\client_1\\network\\admin");
Class.forName("oracle.jdbc.driver.OracleDriver");
connectString = "jdbc:oracle:thin:#esmdj.test";
System.out.println("Before DriverManager.getConnection");
try {
connection = DriverManager.getConnection(connectString, "scott", "tiger");
System.out.println("Connection established");
connection.setAutoCommit(false);
} catch (Exception e) {
System.out.println("Exception inside connect(): " + e);
e.printStackTrace();
}
}
public static void main(String[] args) {
DBConnect client = new DBConnect();
System.out.println("beginning");
try {
client.connect();
System.out.println("after Connected");
client.connection.close();
System.out.println("after close");
} catch (Exception e) {
try {
System.out.println("Exception : " + e);
client.connection.close();
e.printStackTrace();
} catch (Exception ex) {
System.out.println("Close Connection Exception : " + ex);
ex.printStackTrace();
}
}
}
}
C:\Program Files\Java\jdk1.8.0_73\bin>java -classpath . DBConnect
beginning
Before DriverManager.getConnection
Connection established
after Connected
after close
tnsnames.ora
esmdj.test =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = xxx.xxx.yyy.zzz)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = ESMD)
)
)
I've found the problem - I was using older Oracle driver (ojdbc7.jar for version 12.1.0.1) after change to newer one (ojdbc8.jar for version 12.2.0.1) lookup by TNS alias started to work - there was no need to escape anything

how can i handle multiple server MongoDB

Hi all i am new to spring maven project, and i am using MongoDB. I want to use two tomcats/ MongoDB both of theri IP address are different. when first DB is down i need to connect with second one how it is possible
I am using following code
public boolean mongoRunningAt(String uri) {
try {
Mongo mongo = new Mongo(new MongoURI(uri));
try {
Socket socket = mongo.getMongoOptions().socketFactory.createSocket();
socket.connect(mongo.getAddress().getSocketAddress());
socket.close();
} catch (IOException ex) {
mongo = new Mongo(new MongoURI(uri_second));
Socket socket = mongo.getMongoOptions().socketFactory.createSocket();
socket.connect(mongo.getAddress().getSocketAddress());
socket.close();
//return false;
}
mongo.close();
return true;
} catch (UnknownHostException e) {
return false;
}
}
Using this code i tried with first one successfully connected, now stoped first DB now restarted server it is connected with second db.
But if i didn't restart server it is always pointing to First only... how should i work on this
Thanks in advance
You deployed 2 servers, are they in a replica set. If not you can follow the link.
When they are already in a replica set you can use a connectionstring containing the 2 servers.
Like this:
mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test

Categories