17 |
17 |
import com.vaadin.ui.UI;
|
18 |
18 |
|
19 |
19 |
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
|
20 |
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
20 |
21 |
import eu.etaxonomy.cdm.api.service.DeleteResult;
|
21 |
22 |
import eu.etaxonomy.cdm.api.service.IService;
|
22 |
23 |
import eu.etaxonomy.cdm.model.common.CdmBase;
|
... | ... | |
41 |
42 |
|
42 |
43 |
TransactionStatus tx = null;
|
43 |
44 |
|
44 |
|
Session session = null;
|
|
45 |
ConversationHolder conversationHolder = null;
|
45 |
46 |
|
46 |
47 |
/**
|
47 |
48 |
*
|
... | ... | |
54 |
55 |
|
55 |
56 |
this.repo = repo;
|
56 |
57 |
this.service = service;
|
|
58 |
|
|
59 |
}
|
|
60 |
|
|
61 |
|
|
62 |
public TransactionStatus startConversationalTransaction() {
|
|
63 |
checkExistingTransaction();
|
|
64 |
getConversationHolder().bind();
|
|
65 |
tx = getConversationHolder().startTransaction();
|
|
66 |
return tx;
|
57 |
67 |
}
|
58 |
68 |
|
|
69 |
|
|
70 |
/**
|
|
71 |
* @return
|
|
72 |
*/
|
|
73 |
protected ConversationHolder getConversationHolder() {
|
|
74 |
if(conversationHolder == null){
|
|
75 |
conversationHolder = (ConversationHolder) repo.getBean("conversationHolder");
|
|
76 |
}
|
|
77 |
return conversationHolder;
|
|
78 |
}
|
59 |
79 |
/**
|
60 |
80 |
* @return
|
61 |
81 |
*
|
62 |
82 |
*/
|
63 |
|
public TransactionStatus startTransaction() {
|
|
83 |
private TransactionStatus startTransaction(boolean readOnly) {
|
|
84 |
checkExistingTransaction();
|
|
85 |
return repo.startTransaction(readOnly);
|
|
86 |
}
|
|
87 |
|
|
88 |
|
|
89 |
/**
|
|
90 |
*
|
|
91 |
*/
|
|
92 |
protected void checkExistingTransaction() {
|
64 |
93 |
if (tx != null) {
|
65 |
94 |
// @formatter:off
|
66 |
95 |
// holding the TransactionStatus as state is not good design. we
|
... | ... | |
76 |
105 |
// @formatter:on
|
77 |
106 |
throw new RuntimeException("Can't process a second save operation while another one is in progress.");
|
78 |
107 |
}
|
79 |
|
return repo.startTransaction(true);
|
80 |
108 |
}
|
81 |
109 |
|
82 |
110 |
/**
|
... | ... | |
89 |
117 |
* was not necessary.
|
90 |
118 |
*/
|
91 |
119 |
public T mergedBean(T bean) {
|
|
120 |
|
92 |
121 |
Session session = getSession();
|
93 |
122 |
|
94 |
123 |
// session.clear();
|
... | ... | |
113 |
142 |
/**
|
114 |
143 |
* @return
|
115 |
144 |
*/
|
116 |
|
protected Session getSession() {
|
|
145 |
public Session getSession() {
|
|
146 |
|
117 |
147 |
Session session = repo.getSession();
|
118 |
148 |
logger.trace(this._toString() + ".getSession() - session:" + session.hashCode() + ", persistenceContext: "
|
119 |
149 |
+ ((SessionImplementor) session).getPersistenceContext() + " - " + session.toString());
|
|
150 |
|
120 |
151 |
return session;
|
121 |
152 |
}
|
122 |
153 |
|
... | ... | |
127 |
158 |
/**
|
128 |
159 |
*
|
129 |
160 |
* @param bean
|
|
161 |
* @param tx2
|
|
162 |
* @param presaveSession
|
130 |
163 |
* @return the merged bean, this bean is <b>not reloaded</b> from the
|
131 |
164 |
* persistent storage.
|
132 |
165 |
*/
|
... | ... | |
138 |
171 |
} else {
|
139 |
172 |
changeEventType = Type.CREATED;
|
140 |
173 |
}
|
141 |
|
|
142 |
|
// Session session = getSession();
|
143 |
|
logger.trace(this._toString() + ".onEditorSaveEvent - session: " + session);
|
|
174 |
Session session = getSession();
|
|
175 |
if(tx == null){
|
|
176 |
startTransaction(false);
|
|
177 |
}
|
|
178 |
logger.trace(this._toString() + ".onEditorSaveEvent - session: " + session.hashCode());
|
144 |
179 |
logger.trace(this._toString() + ".onEditorSaveEvent - merging bean into session");
|
145 |
180 |
// merge the changes into the session, ...
|
146 |
181 |
T mergedBean = mergedBean(bean);
|
147 |
182 |
repo.getCommonService().saveOrUpdate(mergedBean);
|
148 |
|
session.flush();
|
149 |
|
logger.trace(this._toString() + ".onEditorSaveEvent - session flushed");
|
150 |
|
repo.commitTransaction(tx);
|
151 |
|
tx = null;
|
152 |
|
if (session.isOpen()) {
|
153 |
|
session.close();
|
154 |
|
}
|
155 |
|
logger.trace(this._toString() + ".onEditorSaveEvent - transaction comitted");
|
|
183 |
flushCommitAndCloseConversationTransaction();
|
|
184 |
|
156 |
185 |
return new EntityChangeEvent(mergedBean.getClass(), mergedBean.getId(), changeEventType);
|
157 |
186 |
}
|
158 |
187 |
|
... | ... | |
162 |
191 |
* @return a EntityChangeEvent in case the deletion was successful otherwise <code>null</code>.
|
163 |
192 |
*/
|
164 |
193 |
public final EntityChangeEvent deleteBean(T bean) {
|
|
194 |
|
165 |
195 |
logger.trace(this._toString() + ".onEditorPreSaveEvent - starting transaction");
|
166 |
|
tx = startTransaction();
|
|
196 |
|
|
197 |
startTransaction(false);
|
167 |
198 |
logger.trace(this._toString() + ".deleteBean - deleting" + bean.toString());
|
168 |
199 |
DeleteResult result = service.delete(bean);
|
169 |
200 |
if (result.isOk()) {
|
170 |
|
Session session = getSession();
|
171 |
|
session.flush();
|
172 |
|
logger.trace(this._toString() + ".deleteBean - session flushed");
|
173 |
|
repo.commitTransaction(tx);
|
174 |
|
tx = null;
|
175 |
|
session.close();
|
|
201 |
|
|
202 |
flushCommitAndClose();
|
176 |
203 |
logger.trace(this._toString() + ".deleteBean - transaction comitted");
|
177 |
204 |
return new EntityChangeEvent(bean.getClass(), bean.getId(), Type.REMOVED);
|
178 |
205 |
} else {
|
... | ... | |
210 |
237 |
return null;
|
211 |
238 |
}
|
212 |
239 |
|
|
240 |
/**
|
|
241 |
* @param session
|
|
242 |
*/
|
|
243 |
protected void flushCommitAndClose() {
|
|
244 |
Session session = getSession();
|
|
245 |
session.flush();
|
|
246 |
logger.trace(this._toString() + "session flushed");
|
|
247 |
repo.commitTransaction(tx);
|
|
248 |
tx = null;
|
|
249 |
if(session.isOpen()){
|
|
250 |
session.close();
|
|
251 |
}
|
|
252 |
session = null;
|
|
253 |
|
|
254 |
logger.trace(this._toString() + "transaction comitted and session closed");
|
|
255 |
}
|
|
256 |
|
|
257 |
protected void flushCommitAndCloseConversationTransaction() {
|
|
258 |
getConversationHolder().getSession().flush();
|
|
259 |
logger.trace(this._toString() + "conversational session flushed");
|
|
260 |
getConversationHolder().commit();
|
|
261 |
getConversationHolder().close();
|
|
262 |
tx = null;
|
|
263 |
logger.trace(this._toString() + "conversational transaction comitted and session closed");
|
|
264 |
}
|
|
265 |
|
213 |
266 |
}
|
ref #6687 using conversational transaction for presave and save operation