首页 > 数据库 > 数据库开发技术 > DB2 HADR环境下,应用的改变
|
### JCC Properties read through -Ddb2.jcc.propertiesFile=./conn.properties #db2.jcc.traceFile=./jcc.trace db2.jcc.outputDirectory=. ### Connection level properties used by ### DriverManager.getConnection(url, conProperties); user=db2psc password=password portNumber=50000 deferPrepares=false retrieveMessagesFromServerOnGetMessage=true clientApplicationInformation=hadrrosProg clientProgramName=mixedwl clientWorkstation=node03 defaultIsolationLevel=2 driverType=4 ### Properties for the Automatic Client Reroute #enableSysplexWLB=true #enableConnectionConcentrator=true enableSeamlessFailover=true clientRerouteAlternateServerName=192.168.142.100,192.168.142.100 clientRerouteAlternatePortNumber=50000,50000 enableClientAffinitiesList=1 maxRetriesForClientReroute=4 retryIntervalForClientReroute=120 ### properties read by the application such as server, port, ### dbName, taskprefix, autoCommit, fetchsize server=192.168.142.100 port=50000 dbName=hadb |
|
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Properties; public class Test1 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { String propFile = "C:\\Users\\IBM_ADMIN\\IBM\\rationalsdp\\ds1\\hadr\\src\\hadr.properties"; Properties connProp = new Properties(); FileInputStream dsPropFile = null; try { dsPropFile = new FileInputStream(propFile); } catch (FileNotFoundException fe) { System.out.println(fe.getMessage()); throw fe; } connProp.load(dsPropFile); String server = connProp.getProperty("server"); String port = connProp.getProperty("port"); String dbName = connProp.getProperty("dbName"); String url = "jdbc:db2://" + server + ":" + port + "/" + dbName; Connection bladeConn = null; PreparedStatement pstmt = null; try { bladeConn = DriverManager.getConnection(url, connProp); bladeConn.setAutoCommit(false); pstmt = bladeConn.prepareStatement("insert into t values(?,?)"); for (int i = 0; i < 100000000; i++) { pstmt.setInt(1, i); pstmt.setInt(2, i); pstmt.addBatch(); boolean retry=false; do { retry=false; try { if (i % 50 == 0) { pstmt.executeBatch(); bladeConn.commit(); System.out.println(i); } }catch(SQLException se) { //deal with client reroute exception int errorcode = se.getErrorCode(); System.out.println("SQLException: " + se); System.out.println("SQLCode: " + errorcode); if(bladeConn != null){ try{ bladeConn.rollback(); }catch(Exception e) { e.printStackTrace(); } } //endif if((errorcode == -30108) || (errorcode == -4498)){ System.out.println("connection is re-established, re-executing the failed transaction."); retry = true; } } } while(retry); } System.out.print("Batch execution completed."); } catch (Exception ex) { ex.printStackTrace(); System.exit(-1); } finally { try { pstmt.close(); bladeConn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/725820/viewspace-2064494/ ,如需转载,请注明出处,否则将追究法律责任。