Which code segment should you insert line 11?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an app1ication. The
application connects to a Microsoft SQL Server database. You write the following code segment that
executes two commands against the database within a transaction. (Line numbers are included for
reference only)
01 Using connection As New SqlConnection(cnnStr)
02 connnectionOpen()
03 Dim sqlTran As SqlTransaction = connection.BeginTraraction()
04 Din command As Sqlcommand = connection Createcommand()
05 commandTransation = sqlTran
06 Try
07 command.CommandText = “INSERT INTO Production.ScrapReason(Name)
VALUES(‘Wrong size’)”
08 command.ExecuteNonQuery()
09 command.CommandText = “INSERT INTO Production ScrapReason(Name)
VALIES(’Wrong color’)”
10 command.ExecuteNonQuery()
11
12 End Using
You need to log error information if the transaction fails to commit or roll back. Which code segment
should you insert line 11?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an app1ication. The
application connects to a Microsoft SQL Server database. You write the following code segment that
executes two commands against the database within a transaction. (Line numbers are included for
reference only)
01 Using connection As New SqlConnection(cnnStr)
02 connnectionOpen()
03 Dim sqlTran As SqlTransaction = connection.BeginTraraction()
04 Din command As Sqlcommand = connection Createcommand()
05 commandTransation = sqlTran
06 Try
07 command.CommandText = “INSERT INTO Production.ScrapReason(Name)
VALUES(‘Wrong size’)”
08 command.ExecuteNonQuery()
09 command.CommandText = “INSERT INTO Production ScrapReason(Name)
VALIES(’Wrong color’)”
10 command.ExecuteNonQuery()
11
12 End Using
You need to log error information if the transaction fails to commit or roll back. Which code segment
should you insert line 11?

A.
sqlTran.Commit() Catch ex As Exception
sqTranRollback()
Trace.WriteLine(ex.Message)
End Try

B.
sqlTran.Commit() Catch ex As Exception
Trace.WriteLine(ex.Message)

Try
sqlTran.Rollback()
Catch exRollback As Exception
Trace.WriteLine(exRollback.Message)
End Try
End Try

C.
Catch ex As Exception Trace.Writeline(ex.Message)
Try sqlTran.Rollback()
Catch exRollback As Exception
Trace.WnteLine(exRoIback.Message)
End Try Finally sqlTran.Commit()
End Try

D.
Catch ex As Exception sqlTran.Rollback()
Trace.WriteLine(ex.Message)
Finally
try sqlTran.Commit() Catch exCommit As Exception
Trace.WriteLine(exCommit.Message)
End Try
End Try

Explanation:
A would work, but B is better since we are checking for possible errors during the rollback. C & D
would try to do a rollback before a commit?
The finally block is only executed when the application ends, which may not be the appropriate place
to put this logic.
Normally, a finally block would contain code to close a Database connection. The finally block
executes even if an exception arises.



Leave a Reply 0

Your email address will not be published. Required fields are marked *