Reputation: 139
I have been given a task to write a script in java, to connect oracle database from an app server. Scheduling tool will be used to schedule the job every 10 minutes.
If connection exists, the script will do nothing and just disconnect it. (and will be run again after 10 minutes). If cannot connect after 10 secs, the program will send an email notification.
The whole thing is to ensure connection can be established between the app server and the oracle db.
Really have no clue on this. Could you please advise what are the steps to do this and what Java APIs I'll need??
Many many thanks in advance!
Upvotes: 0
Views: 229
Reputation: 159096
You will need classes out of the java.sql
package. Assuming Java 7+.
import java.sql.*;
public class Test {
public static void main(String[] args) {
String url = "..."; // Specify according to JDBC driver in use
String sql = "SELECT 1 FROM DUAL"; // Test statement. Change depending on SQL vendor
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {/*Nothing to do*/}
} catch (SQLException e) {
// Send email here
}
}
}
Upvotes: 1
Reputation: 2755
If you are using J2EE App Server, create a database connection pool there which your application is also going to use.
One of the parameters of the database pool can be to verify the connection at regular intervals by sending simple SQL. Then your task boils down just to monitor the logs to see if database connection pool throws any errors when polling.
Upvotes: 1