Commit e RollBack da codice

lunedì 08 maggio 2006 - 13.49

ricky Profilo | Junior Member

Ciao, sto "pasticciando" con il commit e begintransaction

Da un metodo principale (all'interno di una begin trasaction) richiamo altre
classi che eseguono a loro volta altre transazioni che terminato tutte con
commit/rollback

nel commit del metodo principale ottengo però questo errore

La SqlTransaction è completata e non può più essere utilizzata.

Cosa può essere??

Inner.Commit();


in System.Data.SqlClient.SqlTransaction.ZombieCheck()
in System.Data.SqlClient.SqlTransaction.Commit()
in
System.Windows.Forms.ToolBar.OnButtonClick(ToolBarButtonClickEventArgs
e)
in System.Windows.Forms.ToolBar.WmReflectCommand(Message& m)
in System.Windows.Forms.ToolBar.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd,
Int32 msg, IntPtr wParam, IntPtr lParam)
in System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam,
IntPtr lparam)
in System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd,
Message& m)
in System.Windows.Forms.Control.WmCommand(Message& m)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.ScrollableControl.WndProc(Message& m)
in System.Windows.Forms.ContainerControl.WndProc(Message& m)
in System.Windows.Forms.Form.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
in System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
in System.Windows.Forms.Control.DefWndProc(Message& m)
in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.ToolBar.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
in
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
in
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
in System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
in System.Windows.Forms.Form.ShowDialog()
in MainClass..ctor(String StartApp) in MainClass.cs:riga 829
in MainClass.Main() in MainClass.cs:riga 661
in System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
in System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart()

alx_81 Profilo | Guru

Ciao!
prova a postare il codice e l'sql che lanci..
probabilmente nello statement c'è già la chiusura della transazione.
Alx81 =)

http://blogs.dotnethell.it/suxstellino

trinity Profilo | Guru

Anch'io utilizzo SqlConnection come provider di connessione e le transazioni per eseguire salvataggi e modifiche su database Sql Server.
prova questo codice:

Public Db As New SqlConnection Public SqlTrans As SqlTransaction Try SqlTrans = Db.BeginTransaction SqlCmd.Transaction = SqlTrans ---esegui il codice di salvataggio o altro SqlCmd.Connection = Db SqlCmd.CommandType = CommandType.text SqlCmd.ExecuteNonQuery() SqlTrans.Commit() SqlCmd.Dispose() Catch ex As Exception SqlTrans.Rollback() MessageBox.Show(ex.Message, "prova", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Return End Try

ciao
Cirillo Fabio
www.wondernet.biz
fabio@wondernet.biz
http://blogs.dotnethell.it/fabiocirillo/

ricky Profilo | Junior Member

Grazie per l'aiuto... cercherò di "estrapolare" il codice e postarlo sul forum, anche perchè non ho ancora capito dove sta l'errore (per ora ho tolto il begin/commit/rollback) ma vorrei capire la causa del problema!

A presto quindi!

ricky Profilo | Junior Member

Il codice è il seguente


myDataIns.BeginTransaction();

ID = myDoc.Save (myDataIns); ...che esegue un'operazione che termina con cmd.ExecuteNonQuery();

se il ristulato del salvataggio è avvenuto con successo...

if (ID!=-1)
{

myDoc.Elimina(...); ...che elimina delle righe...


...


myDataIns.Commit();

}

else//-1
{
myDataIns.Rollback();
}


Sembra tutto OK...???

tra l'altro ho un dubbio: se dal mio metodo principale richiamo una serie di operazioni secondarie presenti in altre classi che effettuano scrittura/update di dati, la mia transazione intercetterà eventuali problemi nei metodi secondari?? (indipentemente dagli oggetti utilizzati per accedere ai dati?)

ricky Profilo | Junior Member

Please help me!!!
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5