KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jdo > JdoAccessor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.jdo;
18
19 import javax.jdo.JDOException;
20 import javax.jdo.PersistenceManager;
21 import javax.jdo.PersistenceManagerFactory;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.springframework.beans.factory.InitializingBean;
27 import org.springframework.dao.DataAccessException;
28
29 /**
30  * Base class for JdoTemplate and JdoInterceptor, defining common
31  * properties such as PersistenceManagerFactory and flushing behavior.
32  *
33  * <p>Note: With JDO, modifications to persistent objects are just possible
34  * within a transaction (in contrast to Hibernate). Therefore, eager flushing
35  * will just get applied when in a transaction. Furthermore, there is no
36  * explicit notion of flushing never, as this would not imply a performance
37  * gain due to JDO's field interception mechanism (which doesn't involve
38  * the overhead of snapshot comparisons).
39  *
40  * <p>Eager flushing is just available for specific JDO providers.
41  * You need to a corresponding JdoDialect to make eager flushing work.
42  *
43  * <p>Not intended to be used directly. See JdoTemplate and JdoInterceptor.
44  *
45  * @author Juergen Hoeller
46  * @since 02.11.2003
47  * @see JdoTemplate
48  * @see JdoInterceptor
49  * @see #setFlushEager
50  */

51 public abstract class JdoAccessor implements InitializingBean {
52
53     /** Logger available to subclasses */
54     protected final Log logger = LogFactory.getLog(getClass());
55
56     private PersistenceManagerFactory persistenceManagerFactory;
57
58     private JdoDialect jdoDialect;
59
60     private boolean flushEager = false;
61
62
63     /**
64      * Set the JDO PersistenceManagerFactory that should be used to create
65      * PersistenceManagers.
66      */

67     public void setPersistenceManagerFactory(PersistenceManagerFactory pmf) {
68         this.persistenceManagerFactory = pmf;
69     }
70
71     /**
72      * Return the JDO PersistenceManagerFactory that should be used to create
73      * PersistenceManagers.
74      */

75     public PersistenceManagerFactory getPersistenceManagerFactory() {
76         return persistenceManagerFactory;
77     }
78
79     /**
80      * Set the JDO dialect to use for this accessor.
81      * <p>The dialect object can be used to retrieve the underlying JDBC
82      * connection and to eagerly flush changes to the database.
83      * <p>Default is a DefaultJdoDialect based on the PersistenceManagerFactory's
84      * underlying DataSource, if any.
85      */

86     public void setJdoDialect(JdoDialect jdoDialect) {
87         this.jdoDialect = jdoDialect;
88     }
89
90     /**
91      * Return the JDO dialect to use for this accessor.
92      * <p>Creates a default one for the specified PersistenceManagerFactory if none set.
93      */

94     public JdoDialect getJdoDialect() {
95         if (this.jdoDialect == null) {
96             this.jdoDialect = new DefaultJdoDialect();
97         }
98         return this.jdoDialect;
99     }
100
101     /**
102      * Set if this accessor should flush changes to the database eagerly.
103      * <p>Eager flushing leads to immediate synchronization with the database,
104      * even if in a transaction. This causes inconsistencies to show up and throw
105      * a respective exception immediately, and JDBC access code that participates
106      * in the same transaction will see the changes as the database is already
107      * aware of them then. But the drawbacks are:
108      * <ul>
109      * <li>additional communication roundtrips with the database, instead of a
110      * single batch at transaction commit;
111      * <li>the fact that an actual database rollback is needed if the JDO
112      * transaction rolls back (due to already submitted SQL statements).
113      * </ul>
114      */

115     public void setFlushEager(boolean flushEager) {
116         this.flushEager = flushEager;
117     }
118
119     /**
120      * Return if this accessor should flush changes to the database eagerly.
121      */

122     public boolean isFlushEager() {
123         return flushEager;
124     }
125
126     /**
127      * Eagerly initialize the JDO dialect, creating a default one
128      * for the specified PersistenceManagerFactory if none set.
129      */

130     public void afterPropertiesSet() {
131         if (getPersistenceManagerFactory() == null) {
132             throw new IllegalArgumentException JavaDoc("persistenceManagerFactory is required");
133         }
134         // Build default JdoDialect if none explicitly specified.
135
if (this.jdoDialect == null) {
136             this.jdoDialect = new DefaultJdoDialect(getPersistenceManagerFactory().getConnectionFactory());
137         }
138     }
139
140
141     /**
142      * Flush the given JDO persistence manager if necessary.
143      * @param pm the current JDO PersistenceManager
144      * @param existingTransaction if executing within an existing transaction
145      * (within an existing JDO PersistenceManager that won't be closed immediately)
146      * @throws JDOException in case of JDO flushing errors
147      */

148     protected void flushIfNecessary(PersistenceManager pm, boolean existingTransaction) throws JDOException {
149         if (isFlushEager()) {
150             logger.debug("Eagerly flushing JDO persistence manager");
151             getJdoDialect().flush(pm);
152         }
153     }
154
155     /**
156      * Convert the given JDOException to an appropriate exception from the
157      * <code>org.springframework.dao</code> hierarchy.
158      * <p>Default implementation delegates to the JdoDialect.
159      * May be overridden in subclasses.
160      * @param ex JDOException that occured
161      * @return the corresponding DataAccessException instance
162      * @see JdoDialect#translateException
163      */

164     public DataAccessException convertJdoAccessException(JDOException ex) {
165         return getJdoDialect().translateException(ex);
166     }
167
168 }
169
Popular Tags