해외사이트에서 퍼온 트랜잭션 처리예제입니다.   ASP에서 트랜잭션 처리는 좀 의문이네요?

잘 작동하는지는 테스트해보지 않았습니다만 관심있는 분은 소스 참고해 보세요.


<!-- #include file="adovbs.inc" -->
<%
' ADO constants included above.  Questions about adovbs.inc?
' See "What is Adovbs.inc and Why Do I Need It?"

Dim strConnString      ' Connection string
Dim myConnection       ' ADO Connection object
Dim strSqlQuery        ' SQL query
Dim lngRecordsAffected ' Number of records affected by command

' Set our connection string
strConnString = "Provider=SQLOLEDB;Data Source=10.2.1.214;" _
 & "Initial Catalog=samples;User Id=samples;Password=password;" _
 & "Connect Timeout=15;Network Library=dbmssocn;"

' Build our SQL query
strSqlQuery = "INSERT INTO [scratch] (text_field, integer_field, date_time_field) " _
                & "VALUES ('" _
                & CStr(WeekdayName(WeekDay(Date()))) & "', '" _
                & CInt(Day(Now())) & "', '" _
                & Now() & "');"

' Open connection
Set myConnection = Server.CreateObject("ADODB.Connection")
myConnection.Open strConnString

' Start transaction
Response.Write "<p><strong>Starting Transaction.</strong></p>" & vbCrLf
myConnection.BeginTrans

' Execute the SQL command
Response.Write "<p><strong>Executing SQL Command:</strong><br /><code>" & strSqlQuery & "</code></p>" & vbCrLf
myConnection.Execute strSqlQuery, lngRecordsAffected, adCmdText + adExecuteNoRecords

' Echo back the number of records affected
Response.Write "<p><strong>Records Affected:</strong> <code>" & lngRecordsAffected & "</code></p>" & vbCrLf

' Either commit or rollback the transaction based on QueryString
If CBool(Request.QueryString("rollback")) = True Then
 Response.Write "<p><strong>Rolling Back Transaction.</strong></p>" & vbCrLf
 myConnection.RollbackTrans
Else
 Response.Write "<p><strong>Commiting Transaction.</strong></p>" & vbCrLf
 myConnection.CommitTrans
End If

' Close data access objects and free variables
myConnection.Close
Set myConnection = Nothing
%>

<br />

<p>
<strong>Run the script again:</strong>
<a href="<%= Request.ServerVariables("URL") %>?rollback=false">Commit Transaction</a>
or
<a href="<%= Request.ServerVariables("URL") %>?rollback=true">Rollback Transaction</a>
</p>



2번째 이야기

저는 이방법을 쓰지는 않지만  ASP에서 많이들 쓰시는  트랜잭션 처리 방법하나 올립니다.

참고하세요


<!--#include file="dbconnect/dbconnect.asp"-->
<OBJECT RUNAT=server PROGID=ADODB.Connection id=db></object>
<OBJECT RUNAT=server PROGID=ADODB.Recordset id=rs></object>
<%

tblname = request("tblname")

memuid = request("memuid")


dim dberr
dberr = 0


db.begintrans()


sql = "insert into  " & tblname & " ( memuid, idate ) values ('"&memuid&"',getdate())"
db.execute(sql)
dberr = dberr + db.errors.count


if dberr > 0 then
 db.rollbacktrans()
 db.close
 response.write "<center><br><br>회원가입 처리중 에러가 발생 했습니다."
 response.write "에러:<br><br><font color='red'>"&Err.Description&"</font>"
 response.write "<br><br>위의 에러 내용을 사이트관리자에게 문의하여 주시기 바랍니다.</center>"
 err.close()
 response.end
else
 db.committrans()
end if


db.Close
%>


 

2008/02/21 15:12 2008/02/21 15:12

Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다