1
14
15 package com.liferay.portal.service.persistence;
16
17 import com.liferay.portal.kernel.dao.orm.ORMException;
18 import com.liferay.portal.kernel.dao.orm.Session;
19 import com.liferay.portal.kernel.util.InitialThreadLocal;
20 import com.liferay.portal.model.BaseModel;
21 import com.liferay.portal.util.PropsValues;
22
23
29 public class BatchSessionImpl implements BatchSession {
30
31 public void delete(Session session, BaseModel<?> model)
32 throws ORMException {
33
34 if (model.isCachedModel() || isEnabled()) {
35 Object staleObject = session.get(
36 model.getClass(), model.getPrimaryKeyObj());
37
38 if (staleObject != null) {
39 session.evict(staleObject);
40 }
41 }
42
43 session.delete(model);
44
45 if (!isEnabled()) {
46 session.flush();
47
48 return;
49 }
50
51 if ((PropsValues.HIBERNATE_JDBC_BATCH_SIZE == 0) ||
52 ((_counter.get() % PropsValues.HIBERNATE_JDBC_BATCH_SIZE) == 0)) {
53
54 session.flush();
55 }
56
57 _counter.set(_counter.get() + 1);
58 }
59
60 public boolean isEnabled() {
61 return _enabled.get();
62 }
63
64 public void setEnabled(boolean enabled) {
65 _enabled.set(enabled);
66 }
67
68 public void update(Session session, BaseModel<?> model, boolean merge)
69 throws ORMException {
70
71 if (merge || model.isCachedModel()) {
72 session.merge(model);
73 }
74 else {
75 boolean contains = false;
76
77 if (isEnabled()) {
78 Object obj = session.get(
79 model.getClass(), model.getPrimaryKeyObj());
80
81 if ((obj != null) && obj.equals(model)) {
82 contains = true;
83 }
84 }
85
86 if (model.isNew()) {
87 session.save(model);
88 }
89 else if (!contains && !session.contains(model)) {
90 session.saveOrUpdate(model);
91 }
92 }
93
94 if (!isEnabled()) {
95 session.flush();
96
97 return;
98 }
99
100 if ((PropsValues.HIBERNATE_JDBC_BATCH_SIZE == 0) ||
101 ((_counter.get() % PropsValues.HIBERNATE_JDBC_BATCH_SIZE) == 0)) {
102
103 session.flush();
104 }
105
106 _counter.set(_counter.get() + 1);
107 }
108
109 private static final Long _INITIAL_COUNTER = new Long(1);
110
111 private static ThreadLocal<Long> _counter =
112 new InitialThreadLocal<Long>(_INITIAL_COUNTER);
113 private static ThreadLocal<Boolean> _enabled =
114 new InitialThreadLocal<Boolean>(Boolean.FALSE);
115
116 }