1 |
package tools; |
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.DriverManager; |
5 |
import java.sql.SQLException; |
6 |
|
7 |
public class MySQLConnectionCheck extends Thread { |
8 |
|
9 |
/** |
10 |
* Class to check wheter a MySQL server is alive and contains a certain database, |
11 |
* to be run in different threads so that it can be used to check all nodes in cluster parallely |
12 |
* Once thread is executed we can call the connTestPassed method which returns a boolean that |
13 |
* tells wheter the check was passed or not. |
14 |
* |
15 |
* @author duarte |
16 |
*/ |
17 |
|
18 |
String dbServer; |
19 |
String dbUserName; |
20 |
String dbPassword; |
21 |
String dbName; |
22 |
boolean success; |
23 |
|
24 |
public MySQLConnectionCheck(String dbServer,String dbUserName, String dbPassword, String dbName){ |
25 |
this.dbServer=dbServer; |
26 |
this.dbUserName=dbUserName; |
27 |
this.dbPassword=dbPassword; |
28 |
this.dbName=dbName; |
29 |
this.success=true; |
30 |
} |
31 |
|
32 |
@Override |
33 |
public void run() { |
34 |
MySQLConnection.loadMySQLDriver(); |
35 |
String connStr="jdbc:mysql://"+dbServer+"/"+dbName; |
36 |
Connection tryConn = null; |
37 |
try { |
38 |
tryConn = DriverManager.getConnection(connStr, dbUserName, dbPassword); |
39 |
} |
40 |
catch (SQLException e){ |
41 |
success = false; |
42 |
} |
43 |
if (tryConn!=null){ |
44 |
try { |
45 |
tryConn.close(); |
46 |
} catch (SQLException e) { |
47 |
e.printStackTrace(); |
48 |
System.err.println("Couldn't close the test connection."); |
49 |
} |
50 |
} |
51 |
} |
52 |
|
53 |
public boolean connTestPassed(){ |
54 |
return success; |
55 |
} |
56 |
|
57 |
} |