locked
Transaction in Three Layer Architecture Problem RRS feed

  • Question

  • User-448512826 posted

    Hello forum member,

    i am faceing a seriously problem regarding tranction.i using three layer architecture in my project.there are three step by which i store data in database.i use Transctionscope but it is not working as i need.some one please give me some idea how can i solve it.below is my three step to inserting record in database.

    1.Businessobject

    Imports System

    Imports System.ComponentModel

    Imports System.Diagnostics

    Namespace ABITGroup.BO

    <DebuggerDisplay("DUES_MST:{DUE_CODE, nq} {DUE_NAME, nq} ) ")> _

    Public Class DueMst

    Private _ROWID As Integer = 0

    Private _intDUECODE As Integer = 0

    Private _strDUENAME As String = String.Empty

    Private _strDUETYPE As String = String.Empty

    <DataObjectFieldAttribute(True, True, False)> _

    Public Property RowId() As Integer

    Get

    Return _ROWID

    End Get

    Set(ByVal value As Integer)

    _ROWID = value

    End Set

    End Property

    Public Property DueCode() As Integer

    Get

    Return _intDUECODE

    End Get

    Set(ByVal value As Integer)

    _intDUECODE = value

    End Set

    End Property

    Public Property DueName() As String

    Get

    Return _strDUENAME

    End Get

    Set(ByVal value As String)

    _strDUENAME = value

    End Set

    End Property

    Public Property DueType() As String

    Get

    Return _strDUETYPE

    End Get

    Set(ByVal value As String)

    _strDUETYPE = value

    End Set

    End Property

    End Class

    End Namespace

    Business Logic

    Imports System

    Imports System.Data

    Imports System.Data.SqlClient

    Imports System.ComponentModel

    Imports System.Transactions

    Imports ABITGroup.BO

    Imports DataAccessLayer

    Namespace ABITGroup.Bll

    <DataObjectAttribute()> _

    Public Class DueManager

    <DataObjectMethod(DataObjectMethodType.Update Or DataObjectMethodType.Insert, True)> _

    Public Shared Function Save(ByVal myDueMst As DueMst) As Integer

    Dim result As Integer = 0

    Dim dbManager As IDBManager = New DBManager(DataProvider.SqlServer)

    dbManager.ConnectionString = ConfigurationManager.AppSettings("database").ToString()

    Try

    dbManager.Open()

    dbManager.CreateParameters(4)

    dbManager.AddParameters(0, "@rowId", myDueMst.RowId)

    dbManager.AddParameters(1, "@DUECODE", myDueMst.DueCode)

    If String.IsNullOrEmpty(myDueMst.DueName) Then

    dbManager.AddParameters(2, "@DUENAME", DBNull.Value.ToString())

    Else

    dbManager.AddParameters(2, "@DUENAME", myDueMst.DueName)

    End If

    If String.IsNullOrEmpty(myDueMst.DueType) Then

    dbManager.AddParameters(3, "@DUETYPE", DBNull.Value.ToString())

    Else

    dbManager.AddParameters(3, "@DUETYPE", myDueMst.DueType)

    End If

    result = dbManager.ExecuteNonQuery(CommandType.StoredProcedure, "sprocDueMstInsertUpdate")

    Catch ex As Exception

    ErrHandler.WriteError(ex.Message)

    Throw New Exception("Record Saved Failure!")

    Finally

    dbManager.Dispose()

    End Try

    Return result

    End Function

    End Class

    End Namespace

     

    Presention Layer

    Sub SAVEDATA()

    Try

    msg = "Record Inserted Sucessfully!"

    Using myTran As New TransactionScope(TransactionScopeOption.RequiresNew)

    '````````DATA INSERTED IN DISCIPLIEN_MST TABLE`````````

    Dim DISCIPLINE As DataTable = CType(ViewState("DISCIPLINE"), DataTable)

    For Each row As DataRow In DISCIPLINE.Rows

    Dim DisciplineCode As String = row("DSPCODE")

    Dim CourseCode As String = ddlCourse.SelectedValue

    Dim DisciplineName As String = row("DSPNAME")

    Dim DisciplineShName As String = row("DSPSHNM")

    Dim myDiscplineMst As New DisciplineMst

    myDiscplineMst.DisciplineCode = DisciplineCode

    myDiscplineMst.CourseCode = CourseCode

    myDiscplineMst.DisciplineName = DisciplineName

    myDiscplineMst.DisciplineShName = DisciplineShName

    DisciplineManager.Save(myDiscplineMst)

    Next

    myTran.Complete()

    End Using

    Catch ex As Exception

    ErrHandler.WriteError(ex.Message)

    msg = ex.Message

    End Try

    ClientScript.RegisterStartupScript(GetType(Page), "alert", "<script language=JavaScript>alert('" & msg & "');</script>")

    End Sub

    i go through in above three step but if any error occure during inserting record in database,then no rollback working here.for example if i try to insert record in two table and error occure inserting record in second table then first table record is insering but not in second table.but i need both two table 's record inserting or rollback.i using here transactionScope but it is not working proparely.

    any solution

    Friday, April 16, 2010 7:19 AM

Answers

  • User-952121411 posted

    i go through in above three step but if any error occure during inserting record in database,then no rollback working here.for example if i try to insert record in two table and error occure inserting record in second table then first table record is insering but not in second table.
     

    I am not sure if you tried to use transactions and write the code and design from scratch, but I would begin by modeling the MSDN code examples on using Transactions.  It is not that it is incompatible with an n-layer design, but more of how you have implemented the code.  Take a look to the following links, and go through the code examples.  I even recommend starting from scratch with a test application using the code examples from the MSDN to get transactions working the way intended.  Then try an re-inject the working code into your application.

    Implementing an Implicit Transaction using Transaction Scope:

    http://msdn.microsoft.com/en-us/library/ms172152(v=VS.90).aspx

    Writing a Transactional Application:

    http://msdn.microsoft.com/en-us/library/ms229973(v=VS.90).aspx

    Transactionscope Problems:

    http://forums.asp.net/t/1062625.aspx

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 17, 2010 4:41 PM

All replies

  • User888441741 posted

    show me how does your sprocDueMstInsertUpdate looks like 

    Sunday, April 18, 2010 9:49 AM
  • User614805505 posted

    Dear salman behera,

    Is better to put the Transaction at ur BusinessLogic Layer.

    Sunday, April 18, 2010 10:42 AM
  • User-448512826 posted

    below is a sample of   my procedure

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go
    
    
    
    
    
    
    
    ALTER PROCEDURE [dbo].[sprocDueMstInsertUpdate]
    /*	'===============================================================
    	'   NAME:                	sprocDueMstInsertUpdate
    	'   DATE CREATED:       	12TH March 2010
    	'   CREATED BY:          	Salman Behera
    	'   CREATED FOR:			ABITGroup of Institutions
    	'   FUNCTION:            	Creates or updates an item in table: DUE_MST
    	'   IN:                  
    	'   OUT:                 	Nothing
    	'   VERSION: 1           
    	'   EXAMPLE:             
    	'   COMMENTS:            
    	'   MODIFIED ON:         
    	'   MODIFIED BY:         
    	'   REASON MODIFICATION: 
    	'===============================================================*/
    	@ROWID numeric(18, 0),
    	@DUECODE bigint,
    	@DUENAME VARCHAR(40),
    	@DUETYPE VARCHAR(1)
    	  
     	
     	AS
    	DECLARE @ReturnValue int
    	
    	IF EXISTS(SELECT * FROM DUES_MST  WHERE  DUE_CODE=@DUECODE)
    	BEGIN
    	UPDATE DUES_MST  SET
    			DUE_CODE=@DUECODE,
    			DUE_NAME=@DUENAME,
    			DUE_TYPE=@DUETYPE
    			WHERE  
    			DUE_CODE=@DUECODE
    	END
    	ELSE
    	BEGIN
     
    		INSERT INTO DUES_MST
    		(
    			DUE_CODE,
    			DUE_NAME,
    			DUE_TYPE
    		)
    		VALUES
    		(
    			@DUECODE,
    			@DUENAME,
    			@DUETYPE
    			
    			)
    
    		SELECT @ReturnValue = SCOPE_IDENTITY()
    		 
    	END
    	
    
    		SELECT @ReturnValue = @ROWID
    		 
    	
    
    	IF (@@ERROR != 0)
    	BEGIN
    		RETURN -1
    	END
    	ELSE
    	BEGIN
    		RETURN @ReturnValue
    		 
    	END
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    




    Monday, April 19, 2010 2:23 AM
  • User-448512826 posted

    Hi,

    someone help me please!!!!!!!!!!!!!!!! 

    Wednesday, April 21, 2010 5:05 AM
  • User-448512826 posted

    Hi,

    is it not possible "Transaction" in N-tire Architecture???????????

    Monday, April 26, 2010 5:33 AM
  • User-448512826 posted

    Hi CruzerB,

    could you give me example,still i have the same problem.

    please help me.

    Thanking you. 

    Saturday, May 8, 2010 2:59 AM
  • User-952121411 posted

    i go through in above three step but if any error occure during inserting record in database,then no rollback working here.for example if i try to insert record in two table and error occure inserting record in second table then first table record is insering but not in second table.
     

    I am not sure if you tried to use transactions and write the code and design from scratch, but I would begin by modeling the MSDN code examples on using Transactions.  It is not that it is incompatible with an n-layer design, but more of how you have implemented the code.  Take a look to the following links, and go through the code examples.  I even recommend starting from scratch with a test application using the code examples from the MSDN to get transactions working the way intended.  Then try an re-inject the working code into your application.

    Implementing an Implicit Transaction using Transaction Scope:

    http://msdn.microsoft.com/en-us/library/ms172152(v=VS.90).aspx

    Writing a Transactional Application:

    http://msdn.microsoft.com/en-us/library/ms229973(v=VS.90).aspx

    Transactionscope Problems:

    http://forums.asp.net/t/1062625.aspx

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 17, 2010 4:41 PM