-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEXTransaction.m
More file actions
97 lines (86 loc) · 2.69 KB
/
EXTransaction.m
File metadata and controls
97 lines (86 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// EXTransaction.m
// Entropy
// (C) 2007-2009 Codesign
// Licensed under LGPL (v3)
//
#import "EXTransaction.h"
#import "EXContainer.h"
#import "EXException.h"
@implementation EXTransaction
- (id)initWithContainer:(EXContainer*)_container timeout:(NSTimeInterval)timeout {
if (self = [super init]) {
if (timeout > 0) {
timer = [NSTimer scheduledTimerWithTimeInterval: timeout target: self selector: @selector(timedOut:) userInfo: nil repeats: NO];
//NSLog(@"Transaction timer set up: %f %d %@", timeout, [timer isValid], [timer fireDate]);
}
container = [_container retain];
[container lockForTransaction];
[container executeSQL: @"BEGIN EXCLUSIVE TRANSACTION"];
storedObjects = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)timedOut:(NSTimer*)_timer {
NSLog(@"Transaction timed out - rolling back");
timer = nil;
[self rollback];
invalidated = YES;
}
- (NSTimer*)timer {
return timer;
}
- (void)commit {
if (invalidated == YES) {
if (timeoutExceptionRaised == NO) {
timeoutExceptionRaised = YES;
[EXException raise: @"EXException" format: @"Transaction timeout"];
} else return;
}
[timer invalidate];
if ([container executeSQL: @"COMMIT"] == NO) [EXException raise: @"EXException" format: @"Commit failed"];
else [container unlockForTransaction];
}
- (void)rollback {
if (invalidated == YES) {
if (timeoutExceptionRaised == NO) {
timeoutExceptionRaised = YES;
[EXException raise: @"EXException" format: @"Transaction timeout"];
} else return;
}
[timer invalidate];
if ([container executeSQL: @"ROLLBACK"] == NO) [EXException raise: @"EXException" format: @"Rollback failed"];
else [container unlockForTransaction];
}
- (int)storeObject:(id)object {
if (invalidated == YES) {
if (timeoutExceptionRaised == NO) {
timeoutExceptionRaised = YES;
[EXException raise: @"EXException" format: @"Transaction timeout"];
} else return -1;
}
if ([EXContainer isCollection: object]) {
[EXException raise: @"EXException" format: @"A collection object cannot be stored directly"];
return -1;
}
NSMutableSet* processedObjects = [[NSMutableSet alloc] init];
int objectID = [container store: object atomically: NO storedObjects: storedObjects locally: NO processedObjects: processedObjects];
[processedObjects release];
return objectID;
}
- (BOOL)removeObject:(id)object {
if (invalidated == YES) {
if (timeoutExceptionRaised == NO) {
timeoutExceptionRaised = YES;
[EXException raise: @"EXException" format: @"Transaction timeout"];
} else return NO;
}
return [container removeObject: object atomically: NO locally: NO];
}
- (void)dealloc {
//NSLog(@"Transaction dealloced");
[container release];
[storedObjects release];
[super dealloc];
}
@end