Revision 47673

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/impl/DefaultTransaction.java
23 23
package org.gvsig.fmap.dal.impl;
24 24

  
25 25
import java.util.ArrayList;
26
import java.util.Collection;
26 27
import java.util.HashMap;
27 28
import java.util.HashSet;
28 29
import java.util.List;
......
93 94

  
94 95
    @Override
95 96
    public void commit() throws DataException {
96
        if( !this.isInProgress() ) {
97
        if (!this.isInProgress()) {
97 98
            throw new IllegalStateException("Can't commit transaction without begin.");
98 99
        }
99
        for (Pair<DataStore, Boolean> item : stores.values()) {
100
            DataStore store = getStore(item);
101
            if( store instanceof FeatureStore ) {
102
                FeatureStore fstore = (FeatureStore) store;
103
                if( fstore!=null && fstore.getMode() != MODE_QUERY) {
104
//                    LOGGER.info("Transaction commit: finishEditing "+store.getFullName());
105
                    fstore.finishEditing();
100
        try {
101
            LOGGER.debug("commit in "+this.getCode());
102
            int retries = 5;
103
            boolean needretry = false;
104
            while(retries > 0) {
105
                needretry = false;
106
                Collection<Pair<DataStore, Boolean>> theStores = new ArrayList(stores.values());
107
                for (Pair<DataStore, Boolean> item : theStores) {
108
                    DataStore store = getStore(item);
109
                    if (store instanceof FeatureStore) {
110
                        FeatureStore fstore = (FeatureStore) store;
111
                        if (fstore != null && fstore.getMode() != MODE_QUERY) {
112
                            LOGGER.debug("commit: finishEditing "+store.getFullName());
113
                            fstore.finishEditing();
114
                            needretry=true;
115
                        }
116
                    }
106 117
                }
118
                if( !needretry ) {
119
                    break;
120
                }
121
                LOGGER.debug("commit: retry store finish editing");
122
                retries--;
107 123
            }
124
            for (ConnectionService connection : this.connections.values()) {
125
                connection.finish();
126
            }
127
            this.observableHelper.notifyObservers(this, new BaseNotification("COMMIT", null));
128
            this.inProgress = false;
129
        } finally {
130
            LOGGER.debug("commit out "+this.getCode());
108 131
        }
109
        for (ConnectionService connection : this.connections.values()) {
110
            connection.finish();
111
        }
112
        this.observableHelper.notifyObservers(this, new BaseNotification("COMMIT", null));
113
        this.inProgress = false;
114 132
    }
115 133

  
116 134
    @Override
......
118 136
        if( !this.isInProgress() ) {
119 137
            throw new IllegalStateException("Can't rollback transaction without begin.");
120 138
        }
121
        
139
//        LOGGER.info("rollback in");
122 140
        for (Pair<DataStore, Boolean> item : stores.values()) {
123 141
            DataStore store = getStore(item);
124 142
            if( store instanceof FeatureStore ) {
......
133 151
        }
134 152
        this.observableHelper.notifyObservers(this, new BaseNotification("ROLLBACK", null));
135 153
        this.inProgress = false;
154
//        LOGGER.info("rollback out");
136 155
    }
137 156

  
138 157
    @Override
......
161 180
    
162 181
    @Override
163 182
    public void add(DataStore store, String id, boolean local) {
164
        if(store == null){
183
        if (store == null) {
165 184
            throw new IllegalArgumentException("The store is required.");
166 185
        }
167
        String theId = id;
168
        if( StringUtils.isBlank(id) ) {
169
            theId = store.hashCode() + "@"+ store.getFullName();
170
        } else {
171
            DataStore theStore = getStore(this.stores.get(theId));
172
            if(theStore!=null ){
173
                if( theStore==store ) {
174
                    return;
186
//        try {
187
//            LOGGER.info("add in "+this.getCode()+" "+store.getFullName());
188
            String theId = id;
189
            if (StringUtils.isBlank(id)) {
190
                theId = store.hashCode() + "@" + store.getFullName();
191
            } else {
192
                DataStore theStore = getStore(this.stores.get(theId));
193
                if (theStore != null) {
194
                    if (theStore == store) {
195
                        return;
196
                    }
197
                    throw new IllegalArgumentException("The id '" + id + "' is already used.");
175 198
                }
176
                throw new IllegalArgumentException("The id '"+id+"' is already used.");
177 199
            }
178
        }
179
        if( store instanceof SupportTransactions ) {
180
            ((SupportTransactions) store).setTransaction(this);
181
        }
182
        if(!local){
183
            DisposeUtils.bind(store);
184
        }
185
        this.stores.put(theId,new MutablePair<>(store,local));
200
            if (store instanceof SupportTransactions) {
201
                ((SupportTransactions) store).setTransaction(this);
202
            }
203
            if (!local) {
204
                DisposeUtils.bind(store);
205
            }
206
            this.stores.put(theId, new MutablePair<>(store, local));
207
//        } finally {
208
//            LOGGER.info("add out");
209
//        }
186 210
    }
187 211

  
188 212
    @Override
......
267 291
        if( this.inProgress && !DisposeUtils.isNullOrDisposed(store)){
268 292
            throw new IllegalStateException("Can't remove store from a in progress transaction.");
269 293
        }
270
        String id = null;
271
        for (Map.Entry<String, Pair<DataStore,Boolean>> entry : this.stores.entrySet()) {
272
            if( store == getStore(entry.getValue()) ) {
273
                id = entry.getKey();
274
                break;
294
//        try {
295
//            LOGGER.info("remove in");        
296
            String id = null;
297
            for (Map.Entry<String, Pair<DataStore,Boolean>> entry : this.stores.entrySet()) {
298
                if( store == getStore(entry.getValue()) ) {
299
                    id = entry.getKey();
300
                    break;
301
                }
275 302
            }
276
        }
277
        if( id==null ) {
278
            return;
279
        }
280
        if( store instanceof SupportTransactions ) {
281
            ((SupportTransactions) store).setTransaction(null);
282
        }
283
        this.stores.remove(id);
284
        DisposeUtils.dispose(store);
303
            if( id==null ) {
304
                return;
305
            }
306
            if( store instanceof SupportTransactions ) {
307
                ((SupportTransactions) store).setTransaction(null);
308
            }
309
            this.stores.remove(id);
310
            DisposeUtils.dispose(store);
311
//        } finally {
312
//            LOGGER.info("remove in");        
313
//        }
285 314
    }
286 315

  
287 316
    @Override
......
313 342

  
314 343
    @Override
315 344
    public void doDispose() {
345
//        LOGGER.info("doDispose in "+this.getCode());        
346
        
316 347
        if( this.inProgress ) {
317 348
            this.rollbackQuietly();
318 349
        }
......
347 378
        this.supportTransactions = null;
348 379
        this.disposables = null;
349 380
        this.stores = null;
350
    }
381
//        LOGGER.info("doDispose out");        
382
   }
351 383

  
352 384
    @Override
353 385
    public void close() throws Exception {

Also available in: Unified diff