'****************************************************************************
' (c) Copyright IBM Corp. 2007 All rights reserved.
'
' The following sample of source code ("Sample") is owned by International
' Business Machines Corporation or one of its subsidiaries ("IBM") and is
' copyrighted and licensed, not sold. You may use, copy, modify, and
' distribute the Sample in any form without payment to IBM, for the purpose of
' assisting you in the development of your applications.
'
' The Sample code is provided to you on an "AS IS" basis, without warranty of
' any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
' IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do
' not allow for the exclusion or limitation of implied warranties, so the above
' limitations or exclusions may not apply to you. IBM shall not be liable for
' any damages you suffer as a result of using, copying, modifying or
' distributing the Sample, even if IBM has been advised of the possibility of
' such damages.
'****************************************************************************
'
' SOURCE FILE NAME: LCTrans.vb
'
' SAMPLE: Demonstrates loosely coupled transactions
' with the DB2 .NET Data Provider
'
' SQL Statement USED:
' SELECT
'
' DB2 .NET Data Provider Classes USED:
' DB2Connection
' DB2Command
'
'
'****************************************************************************
'
' Building and Running the sample program
'
' If the makefile is not present:
'
' 1. Create a new key pair and store it in SubCOM.snk by entering the
' following at the command prompt:
'
' sn -k SubCOM.snk
'
' 2. Compile the SubCOM.vb file with bldapp.bat by entering the following
' at the command prompt:
'
' bldapp SubCOM
'
' 3. Create a new key pair and store it in RootCOM.snk by entering the
' following at the command prompt:
'
' sn -k RootCOM.snk
'
' 4. Compile the RootCOM.vb file with bldapp.bat by entering the following
' at the command prompt:
'
' bldapp RootCOM
'
' 5. Compile the LCTrans.vb file with bldapp.bat by entering the following
' at the command prompt:
'
' bldapp LCTrans
'
' 6. Register the COM+ objects with regCOM.bat by entering the following
' at the command prompt:
'
' regCOM
'
' 7. Run the LCTrans program by entering the program name at the command
' prompt followed by a user id and password for the SAMPLE database:
'
' LCTrans <userid> <password>
'
' If the makefile is present:
'
' 1 Build the assembly TransCOM.dll and compile LCTrans.vb with the
' makefile by entering the following at the command prompt:
'
' nmake LCTrans
'
' 2. Register the COM+ objects with regCOM.bat by entering the following
' at the command prompt:
'
' regCOM
'
' 3. Run the LCTrans program by entering the program name at the command
' prompt followed by a user id and password for the SAMPLE database:
'
' LCTrans <userid> <password>
'
'****************************************************************************
'
' For more information on the sample programs, see the README file.
'
' For information on developing applications, see the Application
' Development Guide.
'
' For information on using SQL statements, see the SQL Reference.
'
' For the latest information on programming, compiling, and running DB2
' applications, visit the DB2 Information Center at
' http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
'
'****************************************************************************
Imports System
Imports System.IO
Imports System.Data
Imports IBM.Data.DB2
Imports Microsoft.VisualBasic
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports RootCOM
' Set the assembly attributes
' Review the values of the assembly attributes
<Assembly: AssemblyTitle("")>
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyCompany("")>
<Assembly: AssemblyProduct("")>
<Assembly: AssemblyCopyright("")>
<Assembly: AssemblyTrademark("")>
<Assembly: CLSCompliant(True)>
<Assembly: AssemblyVersion("1.0.*")>
Module LCTrans
Dim strUID As String = Nothing
Dim strPWD As String = Nothing
Sub Main(ByVal CmdArgs() As String)
Dim decIncrease As Decimal = 5000.0
Dim strNewJob As String = "Mgr"
Dim strOldJob As String
Dim strEmpJob As String
Dim comRootCOM As UpdateEmpInfoCOM
Console.WriteLine()
Console.WriteLine( _
" THIS SAMPLE DEMONSTRATES LOOSELY COUPLED TRANSACTIONS IN DB2" _
& vbNewLine & " USING MICROSOFT .NET")
Console.WriteLine()
' Check if the user id and password have been entered
Select Case CmdArgs.Length
Case 0
strUID = Nothing
strPWD = Nothing
Case 2
' Obtain the user id and password
strUID = Convert.ToString(CmdArgs(0))
strPWD = Convert.ToString(CmdArgs(1))
Case Else
Console.WriteLine(" Invocation:")
Console.WriteLine()
Console.WriteLine(" LCTrans")
Console.WriteLine(" or")
Console.WriteLine(" LCTrans <USERID> <PASSWORD>")
Return
End Select
Console.WriteLine( _
vbNewLine & _
" DEMONSTRATE LOOSELY COUPLED TRANSACTION IN WHICH ONE" & _
" FAILS CAUSING" & _
vbNewLine & " BOTH TO ROLLBACK" & vbNewLine)
' Display the salary and job title of employee id = 320 before the transaction
Console.WriteLine( _
vbNewLine & _
" EMPLOYEE INFO BEFORE THE TRANSACTION" & vbNewLine)
PrintEmpInfo(320, strEmpJob)
' Create a RootCOM instance. The root component invokes a sub component to demonstrate loosely coupled transactions
comRootCOM = New UpdateEmpInfoCOM()
' Demonstrate a case where one operation of the transaction fails causing both
' operaions to be rolled back. Employee id = 320 has been an employee
' for less 5 years and so, should not have a salary > 20000.
' The sub component aborts the transaction because it finds the salary of empid 320 will be greater than 20000
comRootCOM.UpdateEmpInfo(320, decIncrease, strNewJob, strUID, strPWD)
' Display the salary and job title of employee id = 320 after the transaction
' Nothing should be changed because of the rollback in the sub component.
Console.WriteLine( _
vbNewLine & _
" EMPLOYEE INFO AFTER THE TRANSACTION" & vbNewLine)
PrintEmpInfo(320, strEmpJob)
comRootCOM = Nothing
' Demonstrate a case where all operations are successful causing
' them to be committed.
Console.WriteLine( _
vbNewLine & vbNewLine & _
" DEMONSTRATE LOOSELY COUPLED TRANSACTIONS THAT ARE COMPLETED" _
& vbNewLine & _
" SUCCESSFULLY AND ARE THEREFORE COMMITTED" & vbNewLine)
' Display the salary and job title of employee id = 320 before the transaction
Console.WriteLine( _
vbNewLine & _
" EMPLOYEE INFO BEFORE THE TRANSACTION" & vbNewLine)
PrintEmpInfo(20, strEmpJob)
'Save the old job so we can do the unchanges
strOldJob = strEmpJob
comRootCOM = New UpdateEmpInfoCOM()
comRootCOM.UpdateEmpInfo(20, decIncrease, strNewJob, strUID, strPWD)
' Display the salary and job title of employee id = 20 after the transaction
' They should be changed.
Console.WriteLine( _
vbNewLine & _
" EMPLOYEE INFO AFTER THE TRANSACTION" & vbNewLine)
PrintEmpInfo(20, strEmpJob)
comRootCOM = Nothing
' Undo the changes made to the SAMPLE database
Console.WriteLine( _
vbNewLine & " UNDO THE CHANGES MADE TO THE 'SAMPLE' DATABASE")
comRootCOM = New UpdateEmpInfoCOM()
comRootCOM.UpdateEmpInfo(20, -decIncrease, strOldJob, strUID, strPWD)
Console.WriteLine( _
vbNewLine & _
" EMPLOYEE INFO AFTER THE UNDO" & vbNewLine)
PrintEmpInfo(20, strEmpJob)
Return
End Sub
' This method displays the job title and salary of the employee
' with id = empID
Public Sub PrintEmpInfo(ByVal IntEmpID As Integer, ByRef StrJob As String)
Dim decEmpSalary As Decimal
Dim objConnection As DB2Connection
If strUID Is Nothing Then
objConnection = New DB2Connection("database=sample;enlist=false")
Else
objConnection = New DB2Connection("database=sample;UID=" & strUID & _
";PWD=" & strPWD & ";enlist=false")
End If
Dim objCommand As DB2Command = New DB2Command()
Dim objReader As DB2DataReader
' Connect to the SAMPLE database
Try
objConnection.Open()
Catch e As Exception
Console.WriteLine(e.ToString())
Console.WriteLine( _
"LCTrans: Failed to open connection to the SAMPLE database!")
Return
End Try
objCommand.CommandText = "select SALARY, JOB " & _
" from STAFF " & _
" where ID = " & IntEmpID
objCommand.Connection = objConnection
' Execute the query
Try
objReader = objCommand.ExecuteReader()
If objReader.Read() Then
decEmpSalary = objReader.Item("SALARY")
StrJob = objReader.Item("JOB")
' Display the details of the employee
Console.WriteLine( _
" Salary of employee id = " & IntEmpID & _
" in table STAFF: " & decEmpSalary)
Console.WriteLine( _
" Job of employee id = " & IntEmpID & _
" in table STAFF: " & StrJob)
End If
Catch e As Exception
Console.WriteLine("LCTrans: Failed to fetch from table STAFF!")
Console.WriteLine(e.ToString())
Finally
objReader.Close()
End Try
objConnection.Close()
End Sub
End Module