示例:重新执行应用SQL

当主数据库节点故障且10s未恢复时,openGauss会将对应的备数据库节点升主,使openGauss正常运行。备升主期间正在运行的作业会失败;备升主后启动的作业不会再受影响。如果要做到数据库节点主备切换过程中,上层业务不感知,可参考此示例构建业务层SQL重试机制。

1. import java.sql.Connection;
2. import java.sql.DriverManager;
3. import java.sql.PreparedStatement;
4. import java.sql.ResultSet;
5. import java.sql.SQLException;
6. import java.sql.Statement;

8. class ExitHandler extends Thread {
9. private Statement cancel_stmt = null;

11. public ExitHandler(Statement stmt) {
12. super("Exit Handler");
13. this.cancel_stmt = stmt;
14. }
15. public void run() {
16. System.out.println("exit handle");
17. try {
18. this.cancel_stmt.cancel();
19. } catch (SQLException e) {
20. System.out.println("cancel query failed.");
21. e.printStackTrace();
22. }
23. }
24. }

26. public class SQLRetry {
27. //创建数据库连接。
28. public static Connection GetConnection(String username, String passwd) {
29. String driver = "org.opengauss.Driver";
30. String sourceURL = "jdbc:opengauss://10.131.72.136:8000/postgres";
31. Connection conn = null;
32. try {
33. //加载数据库驱动。
34. Class.forName(driver).newInstance();
35. } catch (Exception e) {
36. e.printStackTrace();
37. return null;
38. }

40. try {
41. //创建数据库连接。
42. conn = DriverManager.getConnection(sourceURL, username, passwd);
43. System.out.println("Connection succeed!");
44. } catch (Exception e) {
45. e.printStackTrace();
46. return null;
47. }

49. return conn;
50. }

52. //执行普通SQL语句,创建jdbc_test1表。
53. public static void CreateTable(Connection conn) {
54. Statement stmt = null;
55. try {
56. stmt = conn.createStatement();

59. Runtime.getRuntime().addShutdownHook(new ExitHandler(stmt));

61. //执行普通SQL语句。
62. int rc2 = stmt
63. .executeUpdate("DROP TABLE if exists jdbc_test1;");

65. int rc1 = stmt
66. .executeUpdate("CREATE TABLE jdbc_test1(col1 INTEGER, col2 VARCHAR(10));");

68. stmt.close();
69. } catch (SQLException e) {
70. if (stmt != null) {
71. try {
72. stmt.close();
73. } catch (SQLException e1) {
74. e1.printStackTrace();
75. }
76. }
77. e.printStackTrace();
78. }
79. }

81. //执行预处理语句,批量插入数据。
82. public static void BatchInsertData(Connection conn) {
83. PreparedStatement pst = null;

85. try {
86. //生成预处理语句。
87. pst = conn.prepareStatement("INSERT INTO jdbc_test1 VALUES (?,?)");
88. for (int i = 0; i < 100; i++) {
89. //添加参数。
90. pst.setInt(1, i);
91. pst.setString(2, "data " + i);
92. pst.addBatch();
93. }
94. //执行批处理。
95. pst.executeBatch();
96. pst.close();
97. } catch (SQLException e) {
98. if (pst != null) {
99. try {
100. pst.close();
101. } catch (SQLException e1) {
102. e1.printStackTrace();
103. }
104. }
105. e.printStackTrace();
106. }
107. }

109. //执行预编译语句,更新数据。
110. private static boolean QueryRedo(Connection conn){
111. PreparedStatement pstmt = null;
112. boolean retValue = false;
113. try {
114. pstmt = conn
115. .prepareStatement("SELECT col1 FROM jdbc_test1 WHERE col2 = ?");

117. pstmt.setString(1, "data 10");
118. ResultSet rs = pstmt.executeQuery();

120. while (rs.next()) {
121. System.out.println("col1 = " + rs.getString("col1"));
122. }
123. rs.close();

125. pstmt.close();
126. retValue = true;
127. } catch (SQLException e) {
128. System.out.println("catch...... retValue " + retValue);
129. if (pstmt != null) {
130. try {
131. pstmt.close();
132. } catch (SQLException e1) {
133. e1.printStackTrace();
134. }
135. }
136. e.printStackTrace();
137. }

139. System.out.println("finesh......");
140. return retValue;
141. }

143. //查询语句,执行失败重试,重试次数可配置。
144. public static void ExecPreparedSQL(Connection conn) throws InterruptedException {
145. int maxRetryTime = 50;
146. int time = 0;
147. String result = null;
148. do {
149. time++;
150. try {
151. System.out.println("time:" + time);
152. boolean ret = QueryRedo(conn);
153. if(ret == false){
154. System.out.println("retry, time:" + time);
155. Thread.sleep(10000);
156. QueryRedo(conn);
157. }
158. } catch (Exception e) {
159. e.printStackTrace();
160. }
161. } while (null == result && time < maxRetryTime);

163. }

165. /**
166. * 主程序,逐步调用各静态方法。
167. * @param args
168. * @throws InterruptedException
169. */
170. public static void main(String[] args) throws InterruptedException {
171. //创建数据库连接。
172. Connection conn = GetConnection("testuser", "test@123");

174. //创建表。
175. CreateTable(conn);

177. //批插数据。
178. BatchInsertData(conn);

180. //执行预编译语句,更新数据。
181. ExecPreparedSQL(conn);

183. //关闭数据库连接。
184. try {
185. conn.close();
186. } catch (SQLException e) {
187. e.printStackTrace();
188. }

190. }

192. }