|
-
Sep 25th, 2010, 03:58 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] transaction and rollback
hi!
i want to ask about the transaction
i want to rollback the changes if something goes wrong either in the clients site(internet connection failed in the time the sub is running) or in the servers side(if the server falls)
is the sqltrasnsaction the properly solution??
i found this example for the sqltransaction
Code:
Dim thisConnection As New SqlConnection("server=(local)\SQLEXPRESS;" & _
"integrated security=sspi;database=MyDatabase")
' SQL Delete Commands
Dim sql As String = "DELETE FROM Employee " & _
"WHERE ID = 10"
' Create command
Dim thisCommand As New SqlCommand(sql, thisConnection)
' Create Transaction
Dim thisTransaction As SqlTransaction
Try
' Open Connection
thisConnection.Open()
' Begin transaction and attach it to command
thisTransaction = thisConnection.BeginTransaction()
thisCommand.Transaction = thisTransaction
' Run delete command
thisCommand.ExecuteNonQuery()
' Commit transaction
thisTransaction.Commit()
' Display success
Console.WriteLine("Transaction Committed. Data Deleted")
Catch ex As Exception
' Roll back transaction
thisTransaction.Rollback()
Console.WriteLine("Transaction rolled back : " & ex.Message)
Finally
' Close Connection
thisConnection.Close()
End Try
-
Sep 25th, 2010, 04:04 AM
#2
Frenzied Member
Re: transaction and rollback
this means, if the Delete statement worked as expected then commit the changes,
and if there is any exception, or any something prevent the command from being executed, then rollback,
if this what you are looking for ??
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 04:07 AM
#3
Re: transaction and rollback
Assuming that it is Database operations that you are wanting to allow rollback for, then yes, the SqlTransaction class would be the correct choice.
Within this class, all operations have to complete successfully, otherwise, none of the operations will be committed to the database.
Gary
-
Sep 25th, 2010, 04:16 AM
#4
Thread Starter
Hyperactive Member
Re: transaction and rollback
and if i want to rollback something else like to give value to a session and at the same time make sql rollback?
-
Sep 25th, 2010, 04:24 AM
#5
Re: transaction and rollback
Take a look at this example here:
http://msdn.microsoft.com/en-us/libr...ansaction.aspx
In the appropriate places, you could do some additional work to set Session variables.
Gary
-
Sep 25th, 2010, 04:58 AM
#6
Thread Starter
Hyperactive Member
Re: transaction and rollback
in the example that i posted before if i put the
thisTransaction = thisConnection.BeginTransaction()
inside the try i have an error "variable is used before assigned a value in the catch"
isit right to have it outside the catch??
-
Sep 25th, 2010, 05:02 AM
#7
Frenzied Member
Re: transaction and rollback
can you post the code to take a look ,
and exactly where the error ?
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 05:03 AM
#8
Thread Starter
Hyperactive Member
Re: transaction and rollback
i see in the example that you posted Gary that it can be outside
another question is must the connection be openned before use the
"thisTransaction = thisConnection.BeginTransaction()"?? or it doesnt matter?
in my case i use many times con.open con.close??
is that a problem?
-
Sep 25th, 2010, 05:06 AM
#9
Thread Starter
Hyperactive Member
Re: transaction and rollback
for example
Code:
Dim thisTransaction As SqlTransaction
Try
thisTransaction = con.BeginTransaction()
Dim paramar(3) As SqlParameter
paramar(0) = New SqlParameter("@id", SqlDbType.Int, 4)
paramar(0).Value = last_id + 1
paramar(1) = New SqlParameter("@caption", SqlDbType.NVarChar)
paramar(1).Value = tbox_text_caption2.Text
paramar(2) = New SqlParameter("@image", SqlDbType.NVarChar)
paramar(2).Value = "~/photos/allesomades" & fname
paramar(3) = New SqlParameter("@imerominia", SqlDbType.DateTime, 512)
paramar(3).Value = Date.UtcNow.AddHours(3)
' paramar(2) = New sqlParameter("@data", sqlType.VarChar, Session("maxtextsize"))
' paramar(2).Value = tbox_text_data.Text
Dim sqlda As New SqlDataAdapter()
sqlda.SelectCommand = New SqlCommand()
sqlda.SelectCommand.Transaction = thisTransaction
sqlda.SelectCommand.Connection = con
sqlda.SelectCommand.Parameters.Add(paramar(0))
sqlda.SelectCommand.Parameters.Add(paramar(1))
sqlda.SelectCommand.Parameters.Add(paramar(2))
sqlda.SelectCommand.Parameters.Add(paramar(3))
sqlda.SelectCommand.CommandText = "insert into eidiseis_sxolia_allon_omadon (text_id,text_caption,image,imerominia) values (@id,@caption,@image,@imerominia)"
con.Open()
Dim ds As New DataSet()
sqlda.Fill(ds)
ds.Dispose()
'tbox_text_data.Text = ""
con.Close()
Dim mycommandtab As New SqlCommand("create table alles_omades" & last_id + 1 & " (text_id int null,text_caption nvarchar(max) null,melos nvarchar(max) null,site nvarchar(max),titlos nvarchar(max) null,imerominia datetime null,quoted int null)", con)
mycommandtab.Transaction = thisTransaction
con.Open()
dr = mycommandtab.ExecuteReader()
dr.Close()
con.close()
....
.....
....
thisTransaction.Commit()
Catch ex As Exception
thisTransaction.Rollback()
End Try
-
Sep 25th, 2010, 05:06 AM
#10
Frenzied Member
Re: transaction and rollback
it is important when you open a connection with the database,
that you use try catch finally and in the finally check if the connection state is opened then close it.
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 05:14 AM
#11
Frenzied Member
Re: transaction and rollback
the connection must be opened before you begin the transaction
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 05:35 AM
#12
Thread Starter
Hyperactive Member
Re: transaction and rollback
ok thanks for that it is an important information..
-
Sep 25th, 2010, 05:37 AM
#13
Frenzied Member
Re: transaction and rollback
if this answered your Questions,
don't forget to mark the thread as resolved
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 05:59 AM
#14
Thread Starter
Hyperactive Member
Re: transaction and rollback
so a code like that
Code:
con.Open()
Dim thisTransaction As SqlTransaction
thisTransaction = con.BeginTransaction()
Try
Dim paramar(3) As SqlParameter
paramar(0) = New SqlParameter("@id", SqlDbType.Int, 4)
paramar(0).Value = last_id + 1
paramar(1) = New SqlParameter("@caption", SqlDbType.NVarChar)
paramar(1).Value = tbox_text_caption2.Text
paramar(2) = New SqlParameter("@image", SqlDbType.NVarChar)
paramar(2).Value = "~/photos/allesomades" & fname
paramar(3) = New SqlParameter("@imerominia", SqlDbType.DateTime, 512)
paramar(3).Value = Date.UtcNow.AddHours(3)
' paramar(2) = New sqlParameter("@data", sqlType.VarChar, Session("maxtextsize"))
' paramar(2).Value = tbox_text_data.Text
Dim sqlda As New SqlDataAdapter()
sqlda.SelectCommand = New SqlCommand()
sqlda.SelectCommand.Transaction = thisTransaction
sqlda.SelectCommand.Connection = con
sqlda.SelectCommand.Parameters.Add(paramar(0))
sqlda.SelectCommand.Parameters.Add(paramar(1))
sqlda.SelectCommand.Parameters.Add(paramar(2))
sqlda.SelectCommand.Parameters.Add(paramar(3))
sqlda.SelectCommand.CommandText = "insert into eidiseis_sxolia_allon_omadon (text_id,text_caption,image,imerominia) values (@id,@caption,@image,@imerominia)"
Dim ds As New DataSet()
sqlda.Fill(ds)
ds.Dispose()
'tbox_text_data.Text = ""
Dim mycommandtab As New SqlCommand("create table alles_omades" & last_id + 1 & " (text_id int null,text_caption nvarchar(max) null,melos nvarchar(max) null,site nvarchar(max),titlos nvarchar(max) null,imerominia datetime null,quoted int null)", con)
mycommandtab.Transaction = thisTransaction
dr = mycommandtab.ExecuteReader()
dr.Close()
'Catch ex As SqlException
'Response.Write("problima kai anairesi allagon:" & ex.Message.ToString)
' End Try
Dim paramard(2) As SqlParameter
paramard(0) = New SqlParameter("@id", SqlDbType.Int, 4)
paramard(0).Value = 1
paramard(1) = New SqlParameter("@caption", SqlDbType.NVarChar)
paramard(1).Value = tbox_text_caption3.Text
paramard(2) = New SqlParameter("@imerominia", SqlDbType.DateTime, 512)
paramard(2).Value = Date.UtcNow.AddHours(3)
' paramar(2) = New sqlParameter("@data", sqlType.VarChar, Session("maxtextsize"))
' paramar(2).Value = tbox_text_data.Text
Dim sqldad As New SqlDataAdapter()
sqldad.SelectCommand = New SqlCommand()
sqldad.SelectCommand.Transaction = thisTransaction
sqldad.SelectCommand.Connection = con
sqldad.SelectCommand.Parameters.Add(paramard(0))
sqldad.SelectCommand.Parameters.Add(paramard(1))
sqldad.SelectCommand.Parameters.Add(paramard(2))
sqldad.SelectCommand.CommandText = "insert into alles_omades" & last_id + 1 & " (text_id,text_caption,imerominia,quoted) values (@id,@caption,@imerominia,0)"
Dim dsd As New DataSet()
sqldad.Fill(dsd)
tbox_text_caption3.Text = ""
dsd.Dispose()
thisTransaction.Commit()
Catch ex As Exception
Session("lathos") = True
Session("messagelathous") = "tis pire piso"
thisTransaction.Rollback()
Finally
con.Close()
End Try
do you think it will work??
another question is when will rollback be need in general??
i saw that if i close the browser in the time the sub is running it occurs no problem and all the sub runs until the end
i thought that if a browser will be closed i would have problem with some values that are token from the page like
tbox_text_caption2.Text
that is the text from a textbox of a site
i thought that if the connection would close before the sub takes this value i would have a problem and then i need rollback..
is that right as i am thinking?
-
Sep 25th, 2010, 06:04 AM
#15
Frenzied Member
Re: transaction and rollback
i guess you working on a website not a web application project ?
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 06:19 AM
#16
Thread Starter
Hyperactive Member
Re: transaction and rollback
-
Sep 25th, 2010, 06:28 AM
#17
Frenzied Member
Re: transaction and rollback
if you it was a web application it will stop executing.
but the website not,
http://connect.microsoft.com/VisualS...stop-debugging
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 06:34 AM
#18
Frenzied Member
Re: transaction and rollback
 Originally Posted by vagelis
another question is when will rollback be need in general??
i saw that if i close the browser in the time the sub is running it occurs no problem and all the sub runs until the end
now let me understand to clear things out?
for Q1 rollback if there is something goes wrong, so there is an exception
Q2 tell me how did you do this?
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 06:43 AM
#19
Thread Starter
Hyperactive Member
Re: transaction and rollback
the sub has many code and ii the middle i have putted a
"for" that counts a big number to have time to close it
so do you think that the code that i posted before is right??
and if you think that fall the conection in the time that the sub is running before take the value of textbox.text will create a problem if i dont have a transaction
or the browser will keep the value with some way before the conection falls?
Last edited by vagelis; Sep 25th, 2010 at 06:50 AM.
-
Sep 25th, 2010, 06:50 AM
#20
Frenzied Member
Re: transaction and rollback
as i told you before this code will work fine, but i don't know what you mean by the last Question
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 07:46 AM
#21
Thread Starter
Hyperactive Member
Re: transaction and rollback
i mean that if in the time sub is running and not have yet the value of a textbox.text if the connection of a client falls then the sub will create an error of dont know this value??
or the browser keeps the values the first time it displays a page??
-
Sep 25th, 2010, 07:56 AM
#22
Frenzied Member
Re: transaction and rollback
how can you do such a thing?
your text value is a parameter in the insert statement, if you didn't pass it in the first place, there will be exception raised about the missing parameter
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 08:05 AM
#23
Thread Starter
Hyperactive Member
Re: transaction and rollback
yes my question is about that
if the connection of the client falls before the sub reaches the parameter give value of the textbox what will happen?
-
Sep 25th, 2010, 08:10 AM
#24
Frenzied Member
Re: transaction and rollback
how this connection of your client will be last ?
you mean the client close the browser?
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 12:26 PM
#25
Thread Starter
Hyperactive Member
Re: transaction and rollback
close the browser or have bad internet from wifi or something like that..
-
Sep 25th, 2010, 12:29 PM
#26
Frenzied Member
Re: transaction and rollback
don't worry about this, if the request didn't reach to the server, then there is nothing will happen
You Don't Have to Rate Me.
I'm Not a Civilized Man I'm the Civilization it self
White or Black, Living or Dieing and 0 or 1 that's MY life
iam an Object in Object Oriented Life
my blog : http://refateid.blogspot.com/
twitter : @avrail
010011000111010101110110001000000100110101111001001000000101000001100011 
-
Sep 25th, 2010, 01:16 PM
#27
Thread Starter
Hyperactive Member
Re: transaction and rollback
and when will something happen??
only if i have wrong in my code??
-
Sep 25th, 2010, 01:20 PM
#28
Thread Starter
Hyperactive Member
Re: transaction and rollback
sory i writed here something not important and i didnt know how to delete this post
Last edited by vagelis; Sep 25th, 2010 at 01:23 PM.
-
Sep 27th, 2010, 01:20 AM
#29
Re: transaction and rollback
 Originally Posted by vagelis
sory i writed here something not important and i didnt know how to delete this post
Unless you are a PowerPoster, you can't delete your own threads. The only thing that you could do would be to make a request to a Moderator to delete your thread.
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|