Sometimes we need to write some data into a database independently on the currently running DB transaction. For example, we want to log some state occurred in the application, however any exception could cause a rollback of the whole transaction, including the log entry. It could look as follows:
LogTable logTable;
;
ttsbegin;
logTable.Message = "Something happens";
logTable.insert();
//some following code in the same transaction throws an exception
throw Exception::Error;
ttscommit;
It can be resolved quite easily. We need to create a separate database connection and possibly our own transaction. The modification of the preceding code would look as follows:
LogTable logTable;
UserConnection connection = new UserConnection();
;
ttsbegin;
//use another connection
logTable.setConnection(connection);
//beginning of the independent transaction
logTable.ttsbegin();
logTable.Message = "Something happens";
logTable.insert();
logTable.ttscommit();
//this exception doesn't affect the logTable entry
throw Exception::Error;
ttscommit;
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.