java - Restartability for access to the database -
i'm trying access oracle database, see code below.
the problem database cluster db if 1 down, can access other. has problem staying on 1 node possible me update java code , if fails check again until connection has been established?
i'm using properties-file insert credential database. when connection has been established query entered , result returned.
public static string connectdb(string configfile, string query) throws filenotfoundexception, ioexception, sqlexception, classnotfoundexception{ properties p = new properties(); p.load(new fileinputstream(configfile)); string servername = (p.getproperty("rms_dbservername")); string portnumber = (p.getproperty("rms_portnumber")); string sid = (p.getproperty("rms_sid")); string url = "jdbc:oracle:thin:@//" + servername + ":" + portnumber + "/" + sid; string username = (p.getproperty("rms_username")); string password = (p.getproperty("rms_password")); class.forname("oracle.jdbc.driver.oracledriver"); sleep(10); connection connection = drivermanager.getconnection(url,username,password); sleep(5); string setr = null; try { statement stmt = connection.createstatement(); try { resultset rset = stmt.executequery(query); try { while(rset.next()) setr = rset.getstring(1); return setr; } { try { rset.close(); } catch (exception ignore) {} } } { try { stmt.close(); } catch (exception ignore) {} } } { try { connection.close(); } catch (exception ignore) {} } }
i'm not sure understand you, java.sql.drivermanager
throws sqlexception
"if database access error occurs". can sth like:
public static string connectdb(string configfile, string query) try{ connection connection = drivermanager.getconnection(url,username,password); } catch (sqlexception ex) { sleep(10); connectdb(configfile,query); // try connect again }
or sth like:
//dbprefix put before name of every property associated given node in config file. public static string connectdb(string configfile, string dbprefix, string query) ... { ... string servername = (p.getproperty(dbprefix + "rms_dbservername")); string portnumber = (p.getproperty(dbprefix + "rms_portnumber")); string sid = (p.getproperty(dbprefix + "rms_sid")); string url = "jdbc:oracle:thin:@//" + servername + ":" + portnumber + "/" + sid; string username = (p.getproperty(dbprefix + "rms_username")); string password = (p.getproperty(dbprefix + "rms_password")); ... try{ connection connection = drivermanager.getconnection(url,username,password); } catch (sqlexception ex) { sleep(10); connectdb(configfile,dbprefix,query); // try connect again, use different node }
Comments
Post a Comment