ページの先頭行へ戻る
HA Database Ready 業務開発ガイド(Open SQL編)
FUJITSU Integrated System

4.2.2 ODBC driverを利用する場合

以下のエラー事象が発生した場合は、データベースの切り替えが発生した可能性があります。この場合、コネクションの再接続を行って、トランザクションを再実行してください。

アプリケーションの記述例
    Sub Main()

      Dim con As System.Data.Odbc.OdbcConnection
      Dim com As System.Data.Odbc.OdbcCommand
      Dim drd As System.Data.Odbc.OdbcDataReader

      Dim i As Integer

      con = New System.Data.Odbc.OdbcConnection(~)

      ConProc:
      Try
          con.Open()
      Catch ex As Exception
          Console.WriteLine(ex.ToString())
          Exit Sub
      End Try

      com = Nothing
      drd = Nothing

      Try
          // SQLの実行
          com = New System.Data.Odbc.OdbcCommand("~", con)
          drd = com.ExecuteReader()

      Catch ex As System.Data.Odbc.OdbcException
          If ex.Errors(0).SQLState = "08S01" Then
              con.Close()
              GoTo ConProc
          End If
          Console.WriteLine(ex.ToString())
      Catch ex As Exception
          Dim e As System.Data.Odbc.OdbcException

          If Not ex.GetBaseException() Is Nothing Then
              If TypeOf ex.GetBaseException() Is System.Data.Odbc.OdbcException Then
                  e = DirectCast(ex.GetBaseException(), System.Data.Odbc.OdbcException)
                  If e.Errors(0).SQLState = "08S01" Or e.Errors(0).SQLState = "57P01" Or e.Errors(0).SQLState = "" Then
                      con.Close()
                      GoTo ConProc
                  End If
              End If
          End If
          Console.WriteLine(ex.ToString())
      Finally
          If Not drd Is Nothing Then
              Try
                  drd.Close()
              Catch ex As Exception
              End Try
          End If
      End Try

      con.Close()
      con.Dispose()
      If Not com Is Nothing Then
          com.Dispose()
      End If

      Exit Sub

注意

データベースの切り替えが完了するまでは、コネクションの接続がエラー終了し、SQLSTATEに"08001"を返却します。

アプリケーションの記述例

5秒間隔で10回、再接続をリトライする場合。

	Dim count As Integer
	count = 0

	Do
		count += 1
		Try
			con.Open()
			Return
			//再接続完了

		Catch ex As System.Data.Odbc.OdbcException
			If ex.Errors(0).SQLState = "08001" Then
				If count > 10 Then
					Throw ex  //再接続失敗
				End If
				System.Windows.Forms.Application.DoEvents()
				System.Threading.Thread.Sleep(5000)
			Else
				Throw
			End If
		Catch ex As Exception
			Throw
		End Try
	Loop