1
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.kernel.util.InitialThreadLocal;
28 import com.liferay.portal.model.BaseModel;
29 import com.liferay.portal.util.PropsValues;
30
31
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 || model.isCachedModel()) {
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) == 0) {
80 session.flush();
81 session.clear();
82 }
83
84 _counter.set(_counter.get() + 1);
85 }
86
87 private static final Long _INITIAL_COUNTER = new Long(1);
88
89 private static ThreadLocal<Long> _counter =
90 new InitialThreadLocal<Long>(_INITIAL_COUNTER);
91 private static ThreadLocal<Boolean> _enabled =
92 new InitialThreadLocal<Boolean>(Boolean.FALSE);
93
94 }