Javaアプリケーションでは、getSQLStateメソッドでSQLSTATE値を取得し、エラー状態を認識します。
以降にJavaアプリケーションでの作成例を記載していますが、EJBアプリケーション、サーブレットおよびJakarta Server Pagesの場合にも、SQLSTATE値を判定することより、Connection Managerを利用することができます。
参照
その他のアプリケーション作成例については、“アプリケーション開発ガイド(JDBCドライバ編)”を参照してください。
ここではロードシェア運用の場合の例を示します。
import java.sql.*; import java.util.Hashtable; import java.net.URL; import javax.sql.DataSource; import javax.naming.*; /* * JdbcSample1.java * 内容:JDBC APIを使用したアプリケーションの作成例 */ class JdbcSample1 { /*** メイン ***/ public static void main(String args[]) { try { int result = -1; /* CONNECT */ DBAccess dbAccess = new DBAccess(); /* PREPARE */ dbAccess.createObj(); while (result == -1) { /* EXECUTE */ result = dbAccess.exec(); // SQLStateが40003,40703,71410の場合、再度exec()を実行。 if(result == -2 | result == -3) { result = -1; } // 正常終了か、その他のエラーの場合、アプリ終了 else { break; } } /* DISCONNECT */ dbAccess.close(); } catch(Exception e) { /* 異常系処理 */ e.printStackTrace(); } } } /* * データベースアクセス処理 */ class DBAccess { private Connection con = null; private PreparedStatement pstmt = null; private int iRollback = 0; /* データベースへ接続 */ DBAccess() { try { Hashtable env = null; DataSource ds = null; Context ctx = null; String sDataSourceName = "DS1"; String sProviderName = "com.fujitsu.symfoware.jdbc2.jndisp.SYMContextFactory"; String sProviderURL = "SYM://localhost:26600"; env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, sProviderName); env.put(Context.PROVIDER_URL, sProviderURL); ctx = new InitialContext(env); String key_ = "jdbc/" + sDataSourceName; ds = (DataSource)ctx.lookup(key_); /* データベースへ接続 */ con = ds.getConnection(); /*手動コミットモードの設定 */ con.setAutoCommit(false); } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println("SQLState = <" + e.getSQLState() + ">, Code = <"+ e.getErrorCode() + ">"); e.printStackTrace(); } catch (NamingException e) { e.printStackTrace(); } } /* * ステートメントを作成 */ public void createObj() { String sSQLInsert = "INSERT INTO SCM1.CATEGORY1 VALUES(?, 'NAME')"; try { /* PreparedStatementクラスのオブジェクトを作成 */ pstmt = con.prepareStatement(sSQLInsert); } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println("SQLState = <" + e.getSQLState() + ">, Code = <"+ e.getErrorCode() + ">"); e.printStackTrace(); } } /* * SQL文の実行 */ public int exec() { int result = -1; try { // SQLSTATEが71410の場合、rollbackする。 if(iRollback == 1) { con.rollback(); pstmt.close(); createObj(); iRollback = 0; } /* データを5件挿入 */ for (int i = 1; i <= 5; i++) { pstmt.setInt(1, i); result = pstmt.executeUpdate(); } /* PreparedStatementクラスのオブジェクトをクローズ */ pstmt.close(); /*トランザクションをコミット */ con.commit(); } catch (SQLException e) { String sErr1 = "40003"; String sErr2 = "40703"; String sErr3 = "71410"; String sErr4 = "71003"; String prefix = e.getSQLState(); // SQLSTATEが40003か40703の場合、再実行する。 if (prefix.equals(sErr1) || prefix.equals(sErr2)) { result = -2; } // SQLSTATEが71410か71003の場合、再実行する。 // rollbackするフラグも立てる。 else if (prefix.equals(sErr3) || prefix.equals(sErr4)) { result = -3; iRollback = 1; } // その他のエラーの場合、エラーを返却する。 else { System.out.println(e.getMessage()); System.out.println("SQLState = <" + e.getSQLState() + ">, Code = <"+ e.getErrorCode() + ">"); result = 1; } } return result; } /* * オブジェクトの回収処理 */ public void close() { try { // Connectionクラスのオブジェクトをクローズ con.close(); } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println("SQLState = <" + e.getSQLState() + ">, Code = <"+ e.getErrorCode() + ">"); e.printStackTrace(); } } }