1
1
/*
2
- * Copyright 2009-2012 the original author or authors.
2
+ * Copyright 2009-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
34
34
public class CachingExecutor implements Executor {
35
35
36
36
private Executor delegate ;
37
- private boolean autoCommit ; // issue #573. No need to call commit() on autoCommit sessions
38
37
private TransactionalCacheManager tcm = new TransactionalCacheManager ();
39
38
40
- private boolean dirty ;
41
-
42
39
public CachingExecutor (Executor delegate ) {
43
- this (delegate , false );
44
- }
45
-
46
- public CachingExecutor (Executor delegate , boolean autoCommit ) {
47
40
this .delegate = delegate ;
48
- this .autoCommit = autoCommit ;
49
41
}
50
42
51
43
public Transaction getTransaction () {
@@ -54,9 +46,8 @@ public Transaction getTransaction() {
54
46
55
47
public void close (boolean forceRollback ) {
56
48
try {
57
- //issue #499. Unresolved session handling
58
- //issue #573. Autocommit sessions should commit
59
- if (dirty && !autoCommit ) {
49
+ //issues #499, #524 and #573
50
+ if (forceRollback ) {
60
51
tcm .rollback ();
61
52
} else {
62
53
tcm .commit ();
@@ -81,28 +72,23 @@ public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds r
81
72
return query (ms , parameterObject , rowBounds , resultHandler , key , boundSql );
82
73
}
83
74
84
- public <E > List <E > query (MappedStatement ms , Object parameterObject , RowBounds rowBounds , ResultHandler resultHandler , CacheKey key , BoundSql boundSql ) throws SQLException {
75
+ public <E > List <E > query (MappedStatement ms , Object parameterObject , RowBounds rowBounds , ResultHandler resultHandler , CacheKey key , BoundSql boundSql )
76
+ throws SQLException {
85
77
Cache cache = ms .getCache ();
86
78
if (cache != null ) {
87
79
flushCacheIfRequired (ms );
88
- if (ms .isUseCache () && resultHandler == null ) {
80
+ if (ms .isUseCache () && resultHandler == null ) {
89
81
ensureNoOutParams (ms , parameterObject , boundSql );
90
- if (!dirty ) {
91
- cache .getReadWriteLock ().readLock ().lock ();
92
- try {
93
- @ SuppressWarnings ("unchecked" )
94
- List <E > cachedList = (List <E >) cache .getObject (key );
95
- if (cachedList != null ) return cachedList ;
96
- } finally {
97
- cache .getReadWriteLock ().readLock ().unlock ();
98
- }
82
+ @ SuppressWarnings ("unchecked" )
83
+ List <E > list = (List <E >) cache .getObject (key );
84
+ if (list == null ) {
85
+ list = delegate .<E > query (ms , parameterObject , rowBounds , resultHandler , key , boundSql );
86
+ tcm .putObject (cache , key , list ); // issue #578. Query must be not synchronized to prevent deadlocks
87
+ return list ;
99
88
}
100
- List <E > list = delegate .<E > query (ms , parameterObject , rowBounds , resultHandler , key , boundSql );
101
- tcm .putObject (cache , key , list ); // issue #578. Query must be not synchronized to prevent deadlocks
102
- return list ;
103
89
}
104
90
}
105
- return delegate .<E >query (ms , parameterObject , rowBounds , resultHandler , key , boundSql );
91
+ return delegate .<E > query (ms , parameterObject , rowBounds , resultHandler , key , boundSql );
106
92
}
107
93
108
94
public List <BatchResult > flushStatements () throws SQLException {
@@ -112,13 +98,11 @@ public List<BatchResult> flushStatements() throws SQLException {
112
98
public void commit (boolean required ) throws SQLException {
113
99
delegate .commit (required );
114
100
tcm .commit ();
115
- dirty = false ;
116
101
}
117
102
118
103
public void rollback (boolean required ) throws SQLException {
119
104
try {
120
105
delegate .rollback (required );
121
- dirty = false ;
122
106
} finally {
123
107
if (required ) {
124
108
tcm .rollback ();
@@ -154,8 +138,7 @@ public void clearLocalCache() {
154
138
155
139
private void flushCacheIfRequired (MappedStatement ms ) {
156
140
Cache cache = ms .getCache ();
157
- if (cache != null && ms .isFlushCacheRequired ()) {
158
- dirty = true ; // issue #524. Disable using cached data for this session
141
+ if (cache != null && ms .isFlushCacheRequired ()) {
159
142
tcm .clear (cache );
160
143
}
161
144
}
0 commit comments