Project

General

Profile

Revision 9399c5ec

ID9399c5ecf43e66a65c104cf976f4d676dc0b0c4e
Parent 1fef7214
Child a903034c

Added by Andreas Kohlbecker over 3 years ago

ref #7558 clearing the hibernate session after exceptions and closing it after any use to avoid dirty sessions dangling around

View differences:

src/main/java/eu/etaxonomy/cdm/service/CdmStore.java
9 9
package eu.etaxonomy.cdm.service;
10 10

  
11 11
import org.apache.log4j.Logger;
12
import org.hibernate.HibernateException;
12 13
import org.hibernate.Session;
13 14
import org.hibernate.engine.spi.SessionImplementor;
14 15
import org.springframework.transaction.TransactionStatus;
......
130 131
     * @return The bean merged to the session or original bean in case a merge
131 132
     *         was not necessary.
132 133
     */
133
    public T mergedBean(T bean) {
134
    public T mergedBean(T bean) throws IllegalStateException {
134 135

  
135 136
        Session session = getSession();
136 137

  
......
191 192
        }
192 193

  
193 194
        Session session = getSession();
194
        logger.trace(this._toString() + ".onEditorSaveEvent - session: " + session.hashCode());
195
        try {
196
            logger.trace(this._toString() + ".onEditorSaveEvent - session: " + session.hashCode());
195 197

  
196
        if(txStatus == null
197
//                || (conversationHolder != null && !conversationHolder.isTransactionActive())
198
                ){
199
            // no running transaction, start one ...
200
            startTransaction();
201
        }
198
            if(txStatus == null
199
    //                || (conversationHolder != null && !conversationHolder.isTransactionActive())
200
                    ){
201
                // no running transaction, start one ...
202
                startTransaction();
203
            }
202 204

  
203
        logger.trace(this._toString() + ".onEditorSaveEvent - merging bean into session");
204
        // merge the changes into the session, ...
205
            logger.trace(this._toString() + ".onEditorSaveEvent - merging bean into session");
206
            // merge the changes into the session, ...
205 207

  
206
        T mergedBean = mergedBean(bean);
208
            T mergedBean = mergedBean(bean);
207 209

  
208
        // NOTE: saveOrUpdate is really needed here even if we to a merge before
209
        // repo.getCommonService().saveOrUpdate(mergedBean);
210
        session.flush();
211
        commitTransaction();
210
            // NOTE: saveOrUpdate is really needed here even if we to a merge before
211
            // repo.getCommonService().saveOrUpdate(mergedBean);
212
            session.flush();
213
            commitTransaction();
214
            return new EntityChangeEvent(mergedBean, changeEventType, view);
215
        } catch (HibernateException | IllegalStateException e){
216
            session.clear();
217
            throw e;
218
        } finally {
219
            try {
220
                session.close();
221
            } catch (HibernateException e2) {
222
                /* IGNORE HERE */
223
            }
224
        }
212 225

  
213
        return new EntityChangeEvent(mergedBean, changeEventType, view);
214 226
    }
215 227

  
216 228
    /**
......
221 233
    public final EntityChangeEvent deleteBean(T bean, AbstractView view) {
222 234

  
223 235
        logger.trace(this._toString() + ".onEditorPreSaveEvent - starting transaction");
224

  
225
        startTransaction();
226
        logger.trace(this._toString() + ".deleteBean - deleting" + bean.toString());
227
        DeleteResult result = service.delete(bean);
228
        if (result.isOk()) {
229
            getSession().flush();
230
            commitTransaction();
231
            logger.trace(this._toString() + ".deleteBean - transaction comitted");
232
            return new EntityChangeEvent(bean, Type.REMOVED, view);
233
        } else {
234
            handleDeleteresultInError(result);
235
            txStatus = null;
236
        Session session = getSession();
237
        try {
238
            startTransaction();
239
            logger.trace(this._toString() + ".deleteBean - deleting" + bean.toString());
240
            DeleteResult result = service.delete(bean);
241
            if (result.isOk()) {
242
                session.flush();
243
                commitTransaction();
244
                logger.trace(this._toString() + ".deleteBean - transaction comitted");
245
                return new EntityChangeEvent(bean, Type.REMOVED, view);
246
            } else {
247
                handleDeleteresultInError(result, session);
248
                txStatus = null;
249
            }
250
        } catch (HibernateException e){
251
            session.clear();
252
            throw e;
253
        } finally {
254
            try {
255
                session.close();
256
            } catch (HibernateException e2) {
257
                /* IGNORE HERE */
258
            }
236 259
        }
237 260
        return null;
238 261
    }
......
240 263
    /**
241 264
     * @param result
242 265
     */
243
    public static void handleDeleteresultInError(DeleteResult result) {
266
    public static void handleDeleteresultInError(DeleteResult result, Session session) {
244 267
        String notificationTitle;
245 268
        StringBuffer messageBody = new StringBuffer();
246 269
        if (result.isAbort()) {
......
252 275
            messageBody.append("<h3>").append("Exceptions:").append("</h3>").append("<ul>");
253 276
            result.getExceptions().forEach(e -> messageBody.append("<li>").append(e.getMessage()).append("</li>"));
254 277
            messageBody.append("</ul>");
278
            if(result.getExceptions().stream().anyMatch(e -> HibernateException.class.isAssignableFrom(e.getClass()))){
279
                session.clear();
280
            }
255 281
        }
256 282
        if (!result.getRelatedObjects().isEmpty()) {
257 283
            messageBody.append("<h3>").append("Related objects exist:").append("</h3>").append("<ul>");
src/main/java/eu/etaxonomy/cdm/vaadin/view/name/NameTypeDesignationPresenter.java
189 189
            EntityChangeEvent changeEvent = new EntityChangeEvent(bean, Type.REMOVED, (AbstractView) getView());
190 190
            viewEventBus.publish(this, changeEvent);
191 191
        } else {
192
            CdmStore.handleDeleteresultInError(deletResult);
192
            CdmStore.handleDeleteresultInError(deletResult, getRepo().getSession());
193 193
        }
194 194
    }
195 195

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)