Skip to content

Commit 7ea5da8

Browse files
committed
Fix for http://code.google.com/p/mybatis/issues/detail?id=573 . Un resolved session handling in cachingexecutor when autocommit is on
1 parent 8327840 commit 7ea5da8

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

src/main/java/org/apache/ibatis/cache/TransactionalCacheManager.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2011 The MyBatis Team
2+
* Copyright 2009-2012 The MyBatis Team
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,13 +23,24 @@
2323
public class TransactionalCacheManager {
2424

2525
private Map<Cache, TransactionalCache> transactionalCaches = new HashMap<Cache, TransactionalCache>();
26-
26+
private boolean autoCommit; // issue #573. No need to call commit() on autoCommit sessions
27+
28+
public TransactionalCacheManager(){
29+
this(false);
30+
}
31+
32+
public TransactionalCacheManager(boolean autoCommit) {
33+
this.autoCommit = autoCommit;
34+
}
35+
2736
public void clear(Cache cache) {
2837
getTransactionalCache(cache).clear();
38+
if (autoCommit) commit();
2939
}
3040

3141
public void putObject(Cache cache, CacheKey key, Object value) {
3242
getTransactionalCache(cache).putObject(key, value);
43+
if (autoCommit) commit();
3344
}
3445

3546
public void commit() {

src/main/java/org/apache/ibatis/executor/CachingExecutor.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,19 @@
3434
public class CachingExecutor implements Executor {
3535

3636
private Executor delegate;
37-
private TransactionalCacheManager tcm = new TransactionalCacheManager();
37+
private boolean autoCommit; // issue #573. No need to call commit() on autoCommit sessions
38+
private TransactionalCacheManager tcm;
3839

3940
private boolean dirty;
4041

4142
public CachingExecutor(Executor delegate) {
43+
this(delegate, false);
44+
}
45+
46+
public CachingExecutor(Executor delegate, boolean autoCommit) {
4247
this.delegate = delegate;
48+
this.autoCommit = autoCommit;
49+
this.tcm = new TransactionalCacheManager(autoCommit);
4350
}
4451

4552
public Transaction getTransaction() {
@@ -48,7 +55,7 @@ public Transaction getTransaction() {
4855

4956
public void close(boolean forceRollback) {
5057
try {
51-
if (dirty) { //issue #499. Unresolved session handling (no commit/rollback)
58+
if (dirty) { //issue #499. Unresolved session handling
5259
tcm.rollback();
5360
} else {
5461
tcm.commit();
@@ -147,11 +154,9 @@ public void clearLocalCache() {
147154

148155
private void flushCacheIfRequired(MappedStatement ms) {
149156
Cache cache = ms.getCache();
150-
if (cache != null) {
151-
if (ms.isFlushCacheRequired()) {
152-
dirty = true; // issue #524. Disable using cached data for this session
153-
tcm.clear(cache);
154-
}
157+
if (cache != null && ms.isFlushCacheRequired()) {
158+
tcm.clear(cache);
159+
if (!autoCommit) dirty = true; // issue #524. Disable using cached data for this session because data may have changed
155160
}
156161
}
157162

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ public Configuration() {
141141
typeAliasRegistry.registerAlias("WEAK", WeakCache.class.getName());
142142

143143
typeAliasRegistry.registerAlias("VENDOR", VendorDatabaseIdProvider.class.getName());
144-
145144
}
146145

147146
public String getDatabaseId() {
@@ -356,6 +355,10 @@ public Executor newExecutor(Transaction transaction) {
356355
}
357356

358357
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
358+
return newExecutor(transaction, defaultExecutorType, false);
359+
}
360+
361+
public Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) {
359362
executorType = executorType == null ? defaultExecutorType : executorType;
360363
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
361364
Executor executor;
@@ -367,7 +370,7 @@ public Executor newExecutor(Transaction transaction, ExecutorType executorType)
367370
executor = new SimpleExecutor(this, transaction);
368371
}
369372
if (cacheEnabled) {
370-
executor = new CachingExecutor(executor);
373+
executor = new CachingExecutor(executor, autoCommit);
371374
}
372375
executor = (Executor) interceptorChain.pluginAll(executor);
373376
return executor;

src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSessionFactory.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2011 The MyBatis Team
2+
* Copyright 2009-2012 The MyBatis Team
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,11 +34,9 @@
3434
public class DefaultSqlSessionFactory implements SqlSessionFactory {
3535

3636
private final Configuration configuration;
37-
private final TransactionFactory managedTransactionFactory;
3837

3938
public DefaultSqlSessionFactory(Configuration configuration) {
4039
this.configuration = configuration;
41-
this.managedTransactionFactory = new ManagedTransactionFactory();
4240
}
4341

4442
public SqlSession openSession() {
@@ -83,7 +81,7 @@ private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionI
8381
final Environment environment = configuration.getEnvironment();
8482
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
8583
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
86-
final Executor executor = configuration.newExecutor(tx, execType);
84+
final Executor executor = configuration.newExecutor(tx, execType, autoCommit);
8785
return new DefaultSqlSession(configuration, executor);
8886
} catch (Exception e) {
8987
closeTransaction(tx); // may have fetched a connection so lets call close()
@@ -98,7 +96,7 @@ private SqlSession openSessionFromConnection(ExecutorType execType, Connection c
9896
final Environment environment = configuration.getEnvironment();
9997
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
10098
final Transaction tx = transactionFactory.newTransaction(connection);
101-
final Executor executor = configuration.newExecutor(tx, execType);
99+
final Executor executor = configuration.newExecutor(tx, execType, connection.getAutoCommit());
102100
return new DefaultSqlSession(configuration, executor);
103101
} catch (Exception e) {
104102
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
@@ -109,7 +107,7 @@ private SqlSession openSessionFromConnection(ExecutorType execType, Connection c
109107

110108
private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
111109
if (environment == null || environment.getTransactionFactory() == null) {
112-
return managedTransactionFactory;
110+
return new ManagedTransactionFactory();
113111
}
114112
return environment.getTransactionFactory();
115113
}
@@ -125,4 +123,3 @@ private void closeTransaction(Transaction tx) {
125123
}
126124

127125
}
128-

0 commit comments

Comments
 (0)