Skip to content

Commit

Permalink
Added support for LocalTransactionScope object (like TransactionScope…
Browse files Browse the repository at this point in the history
… except no MSDTC). Closes #58.
  • Loading branch information
hisystems committed May 5, 2012
1 parent 1168675 commit 0fdc7b9
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Database/Database.vb
Expand Up @@ -1818,27 +1818,27 @@ Public Class Database
''' If a transaction is not in progress an exception will occur.
''' </summary>
''' --------------------------------------------------------------------------------
Public Sub ExecuteNonQuery(ByVal objStatement As SQL.ISQLStatement)
Public Function ExecuteNonQuery(ByVal objStatement As SQL.ISQLStatement) As Integer

If Not pobjConnectionController.InTransactionMode Then
Throw New Exceptions.DatabaseObjectsException("Not in transaction mode")
End If

pobjConnectionController.ExecuteNonQuery(objStatement)
Return pobjConnectionController.ExecuteNonQuery(objStatement)

End Sub
End Function

''' --------------------------------------------------------------------------------
''' <summary>
''' Allows the SQL statements to be executed on the current transaction connection.
''' If a transaction is not in progress an exception will occur.
''' </summary>
''' --------------------------------------------------------------------------------
Public Sub ExecuteNonQuery(ByVal objStatements As SQL.ISQLStatement())
Public Function ExecuteNonQuery(ByVal objStatements As SQL.ISQLStatement()) As Integer

Me.ExecuteNonQuery(New SQL.SQLStatements(objStatements))
Return Me.ExecuteNonQuery(New SQL.SQLStatements(objStatements))

End Sub
End Function

''' --------------------------------------------------------------------------------
''' <summary>
Expand Down
154 changes: 154 additions & 0 deletions Database/LocalTransactionScope.vb
@@ -0,0 +1,154 @@

' ___________________________________________________
'
' © Hi-Integrity Systems 2012. All rights reserved.
' www.hisystems.com.au - Toby Wicks
' ___________________________________________________
'

Option Strict On
Option Explicit On

Imports DatabaseObjects
Imports System.Data

''' <summary>
''' Represents a local (not distributed) database transaction.
''' Should be used with a 'Using' statement to ensure that the transaction is rolled back if any exception occurs (database or code related).
''' Performs like the System.Transactions.TransactionScope object, except that it only targets local database transactions
''' (i.e. BEING/COMMIT/ROLLBACK TRANSACTION commands) and does not utilise the Microsoft Distributed Transaction Coordinator.
''' </summary>
''' <example>
''' Using localTransaction = New LocalTransactionScope(database)
'''
''' With collection.Add
''' .Field = "Value"
''' .Save()
''' End With
'''
''' localTransaction.Complete()
''' End Using
''' </example>
Public Class LocalTransactionScope
Implements IDisposable

Private _transaction As Database.TransactionsClass
Private _disposed As Boolean
Private _completed As Boolean = False

''' <summary>
''' Begins a local database transaction.
''' </summary>
Public Sub New(database As Database)

Me.New(database, IsolationLevel.Unspecified)

End Sub

''' <summary>
''' Begins a local database transaction.
''' </summary>
Public Sub New(database As Database, isolationLevel As Data.IsolationLevel)

If database Is Nothing Then
Throw New ArgumentNullException
End If

_transaction = database.Transactions

_transaction.Begin(isolationLevel)

End Sub

''' --------------------------------------------------------------------------------
''' <summary>
''' Executes the SQL statement.
''' Returns Nothing/null if no record was selected, otherwise the first field from the
''' returned result.
''' </summary>
''' --------------------------------------------------------------------------------
Public Function ExecuteScalar(ByVal sqlStatement As SQL.ISQLStatement) As Object

Return _transaction.Execute(sqlStatement)

End Function

''' --------------------------------------------------------------------------------
''' <summary>
''' Executes the SQL statement and returns the result.
''' The returned reader can only be used until the LocalTransactionScope is disposed
''' (typically at the end of a using construct).
''' </summary>
''' --------------------------------------------------------------------------------
Public Function Execute(ByVal sqlStatement As SQL.ISQLStatement) As IDataReader

Return _transaction.Execute(sqlStatement)

End Function

''' --------------------------------------------------------------------------------
''' <summary>
''' Executes the SQL statements and returns the first result.
''' The returned reader(s) can only be used until the LocalTransactionScope is disposed
''' (typically at the end of a using construct).
''' </summary>
''' --------------------------------------------------------------------------------
Public Function Execute(ByVal sqlStatements As SQL.ISQLStatement()) As IDataReader

Return _transaction.Execute(sqlStatements)

End Function

''' --------------------------------------------------------------------------------
''' <summary>
''' Executes the SQL statement and returns the number of rows affected.
''' </summary>
''' --------------------------------------------------------------------------------
Public Function ExecuteNonQuery(ByVal sqlStatement As SQL.ISQLStatement) As Integer

Return _transaction.ExecuteNonQuery(sqlStatement)

End Function

''' --------------------------------------------------------------------------------
''' <summary>
''' Executes the SQL statements and returns the number of rows affected for all of the statements.
''' </summary>
''' --------------------------------------------------------------------------------
Public Function ExecuteNonQuery(ByVal sqlStatements As SQL.ISQLStatement()) As Integer

Return _transaction.ExecuteNonQuery(sqlStatements)

End Function

''' <summary>
''' Indicates that the transaction should be commited.
''' </summary>
''' <remarks></remarks>
Public Sub Complete()

If _completed Then
Throw New InvalidOperationException("Transaction has already been completed")
End If

_transaction.Commit()
_completed = True

End Sub

''' <summary>
''' Closes the connection if it has not already been closed / disposed.
''' </summary>
''' <remarks></remarks>
Public Sub Dispose() Implements IDisposable.Dispose

If Not Me._disposed AndAlso Not _completed Then
_transaction.Rollback()
Me._disposed = True
End If

GC.SuppressFinalize(Me)

End Sub

End Class
1 change: 1 addition & 0 deletions DatabaseObjects.vbproj
Expand Up @@ -124,6 +124,7 @@
<Compile Include="Contraints\DateIsTodayOrFutureForNewObjectConstraint.vb" />
<Compile Include="Contraints\NumberIsMaximumOrLesserConstraint.vb" />
<Compile Include="Contraints\StringMaxLengthConstraint.vb" />
<Compile Include="Database\LocalTransactionScope.vb" />
<Compile Include="Database\ObjectReferenceEarlyBinding.vb" />
<Compile Include="Database\ConnectionScope.vb" />
<Compile Include="Database\DatabaseObjectsItemInstance.vb" />
Expand Down

0 comments on commit 0fdc7b9

Please sign in to comment.