| BatchSessionImpl.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.liferay.portal.service.persistence;
24
25 import com.liferay.portal.kernel.dao.orm.ORMException;
26 import com.liferay.portal.kernel.dao.orm.Session;
27 import com.liferay.portal.model.BaseModel;
28 import com.liferay.portal.util.PropsValues;
29
30 /**
31 * <a href="BatchSessionImpl.java.html"><b><i>View Source</i></b></a>
32 *
33 * @author Raymond Augé
34 * @author Brian Wing Shun Chan
35 *
36 */
37 public class BatchSessionImpl implements BatchSession {
38
39 public boolean isEnabled() {
40 return _enabled.get();
41 }
42
43 public void setEnabled(boolean enabled) {
44 _enabled.set(enabled);
45 }
46
47 public void update(Session session, BaseModel model, boolean merge)
48 throws ORMException {
49
50 if (merge) {
51 session.merge(model);
52 }
53 else {
54 boolean contains = false;
55
56 if (isEnabled()) {
57 Object obj = session.get(
58 model.getClass(), model.getPrimaryKeyObj());
59
60 if ((obj != null) && obj.equals(model)) {
61 contains = true;
62 }
63 }
64
65 if (model.isNew()) {
66 session.save(model);
67 }
68 else if (!contains && !session.contains(model)) {
69 session.saveOrUpdate(model);
70 }
71 }
72
73 if (!isEnabled()) {
74 session.flush();
75
76 return;
77 }
78
79 if (_counter.get() < PropsValues.HIBERNATE_JDBC_BATCH_SIZE) {
80 _counter.set(_counter.get() + 1);
81 }
82 else {
83 _counter.set(BatchSessionCounter.INITIAL_VALUE);
84
85 session.flush();
86 session.clear();
87 }
88 }
89
90 private BatchSessionCounter _counter = new BatchSessionCounter();
91 private BatchSessionEnabled _enabled = new BatchSessionEnabled();
92
93 }