Source Code Cross Referenced for DefaultJdoDialect.java in  » J2EE » spring-framework-2.0.6 » org » springframework » orm » jdo » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » spring framework 2.0.6 » org.springframework.orm.jdo 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2006 the original author or authors.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.springframework.orm.jdo;
018:
019:        import java.sql.Connection;
020:        import java.sql.SQLException;
021:        import java.util.Collection;
022:
023:        import javax.jdo.JDOException;
024:        import javax.jdo.PersistenceManager;
025:        import javax.jdo.Query;
026:        import javax.jdo.Transaction;
027:
028:        import org.apache.commons.logging.Log;
029:        import org.apache.commons.logging.LogFactory;
030:
031:        import org.springframework.dao.DataAccessException;
032:        import org.springframework.dao.support.PersistenceExceptionTranslator;
033:        import org.springframework.jdbc.datasource.ConnectionHandle;
034:        import org.springframework.jdbc.support.JdbcUtils;
035:        import org.springframework.jdbc.support.SQLExceptionTranslator;
036:        import org.springframework.transaction.InvalidIsolationLevelException;
037:        import org.springframework.transaction.TransactionDefinition;
038:        import org.springframework.transaction.TransactionException;
039:
040:        /**
041:         * Default implementation of the JdoDialect interface.
042:         * Updated to leverage the JDO 2.0 API when available, as of Spring 1.2.
043:         * Used as default dialect by JdoAccessor and JdoTransactionManager.
044:         *
045:         * <p>Simply begins a standard JDO transaction in <code>beginTransaction</code>.
046:         * Returns a handle for a JDO2 DataStoreConnection on <code>getJdbcConnection</code>.
047:         * Calls the corresponding JDO2 operations on <code>detachCopy(All)</code>,
048:         * <code>attachCopy(All)</code>, <code>flush</code> and <code>newNamedQuery</code>.
049:         * Ignores a given query timeout in <code>applyQueryTimeout</code>.
050:         * Uses a Spring SQLExceptionTranslator for exception translation, if applicable.
051:         *
052:         * <p>Note that, even with JDO2, vendor-specific subclasses are still necessary
053:         * for special transaction semantics and more sophisticated exception translation.
054:         * Furthermore, vendor-specific subclasses are encouraged to expose the native JDBC
055:         * Connection on <code>getJdbcConnection</code>, rather than JDO2's wrapper handle.
056:         *
057:         * <p>This class also implements the PersistenceExceptionTranslator interface,
058:         * as autodetected by Spring's PersistenceExceptionTranslationPostProcessor,
059:         * for AOP-based translation of native exceptions to Spring DataAccessExceptions.
060:         * Hence, the presence of a standard DefaultJdoDialect bean automatically enables
061:         * a PersistenceExceptionTranslationPostProcessor to translate JDO exceptions.
062:         *
063:         * @author Juergen Hoeller
064:         * @since 1.1
065:         * @see #setJdbcExceptionTranslator
066:         * @see JdoAccessor#setJdoDialect
067:         * @see JdoTransactionManager#setJdoDialect
068:         * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
069:         */
070:        public class DefaultJdoDialect implements  JdoDialect,
071:                PersistenceExceptionTranslator {
072:
073:            protected final Log logger = LogFactory.getLog(getClass());
074:
075:            private SQLExceptionTranslator jdbcExceptionTranslator;
076:
077:            /**
078:             * Create a new DefaultJdoDialect.
079:             */
080:            public DefaultJdoDialect() {
081:            }
082:
083:            /**
084:             * Create a new DefaultJdoDialect.
085:             * @param connectionFactory the connection factory of the JDO PersistenceManagerFactory,
086:             * which is used to initialize the default JDBC exception translator
087:             * @see javax.jdo.PersistenceManagerFactory#getConnectionFactory()
088:             * @see PersistenceManagerFactoryUtils#newJdbcExceptionTranslator(Object)
089:             */
090:            DefaultJdoDialect(Object connectionFactory) {
091:                this .jdbcExceptionTranslator = PersistenceManagerFactoryUtils
092:                        .newJdbcExceptionTranslator(connectionFactory);
093:            }
094:
095:            /**
096:             * Set the JDBC exception translator for this dialect.
097:             * <p>Applied to any SQLException root cause of a JDOException, if specified.
098:             * The default is to rely on the JDO provider's native exception translation.
099:             * @param jdbcExceptionTranslator exception translator
100:             * @see java.sql.SQLException
101:             * @see javax.jdo.JDOException#getCause()
102:             * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
103:             * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
104:             */
105:            public void setJdbcExceptionTranslator(
106:                    SQLExceptionTranslator jdbcExceptionTranslator) {
107:                this .jdbcExceptionTranslator = jdbcExceptionTranslator;
108:            }
109:
110:            /**
111:             * Return the JDBC exception translator for this dialect, if any.
112:             */
113:            public SQLExceptionTranslator getJdbcExceptionTranslator() {
114:                return this .jdbcExceptionTranslator;
115:            }
116:
117:            //-------------------------------------------------------------------------
118:            // Hooks for transaction management (used by JdoTransactionManager)
119:            //-------------------------------------------------------------------------
120:
121:            /**
122:             * This implementation invokes the standard JDO <code>Transaction.begin</code>
123:             * method. Throws an InvalidIsolationLevelException if a non-default isolation
124:             * level is set.
125:             * @see javax.jdo.Transaction#begin
126:             * @see org.springframework.transaction.InvalidIsolationLevelException
127:             */
128:            public Object beginTransaction(Transaction transaction,
129:                    TransactionDefinition definition) throws JDOException,
130:                    SQLException, TransactionException {
131:
132:                if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
133:                    throw new InvalidIsolationLevelException(
134:                            "Standard JDO does not support custom isolation levels: "
135:                                    + "use a special JdoDialect implementation for your JDO provider");
136:                }
137:                transaction.begin();
138:                return null;
139:            }
140:
141:            /**
142:             * This implementation does nothing, as the default beginTransaction implementation
143:             * does not require any cleanup.
144:             * @see #beginTransaction
145:             */
146:            public void cleanupTransaction(Object transactionData) {
147:            }
148:
149:            /**
150:             * This implementation returns a DataStoreConnectionHandle for JDO2,
151:             * which will also work on JDO1 until actually accessing the JDBC Connection.
152:             * <p>For pre-JDO2 implementations, override this method to return the
153:             * Connection through the corresponding vendor-specific mechanism, or <code>null</code>
154:             * if the Connection is not retrievable.
155:             * <p><b>NOTE:</b> A JDO2 DataStoreConnection is always a wrapper,
156:             * never the native JDBC Connection. If you need access to the native JDBC
157:             * Connection (or the connection pool handle, to be unwrapped via a Spring
158:             * NativeJdbcExtractor), override this method to return the native
159:             * Connection through the corresponding vendor-specific mechanism.
160:             * <p>A JDO2 DataStoreConnection is only "borrowed" from the PersistenceManager:
161:             * it needs to be returned as early as possible. Effectively, JDO2 requires the
162:             * fetched Connection to be closed before continuing PersistenceManager work.
163:             * For this reason, the exposed ConnectionHandle eagerly releases its JDBC
164:             * Connection at the end of each JDBC data access operation (that is, on
165:             * <code>DataSourceUtils.releaseConnection</code>).
166:             * @see javax.jdo.PersistenceManager#getDataStoreConnection()
167:             * @see org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor
168:             * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
169:             */
170:            public ConnectionHandle getJdbcConnection(PersistenceManager pm,
171:                    boolean readOnly) throws JDOException, SQLException {
172:
173:                return new DataStoreConnectionHandle(pm);
174:            }
175:
176:            /**
177:             * This implementation does nothing, assuming that the Connection
178:             * will implicitly be closed with the PersistenceManager.
179:             * <p>If the JDO provider returns a Connection handle that it
180:             * expects the application to close, the dialect needs to invoke
181:             * <code>Connection.close</code> here.
182:             * @see java.sql.Connection#close()
183:             */
184:            public void releaseJdbcConnection(ConnectionHandle conHandle,
185:                    PersistenceManager pm) throws JDOException, SQLException {
186:            }
187:
188:            //-------------------------------------------------------------------------
189:            // Hooks for special data access operations (used by JdoTemplate)
190:            //-------------------------------------------------------------------------
191:
192:            /**
193:             * This implementation delegates to JDO 2.0's <code>detachCopy</code> method.
194:             * <p>To be overridden for pre-JDO2 implementations, using the corresponding
195:             * vendor-specific mechanism there.
196:             * @see javax.jdo.PersistenceManager#detachCopy(Object)
197:             */
198:            public Object detachCopy(PersistenceManager pm, Object entity)
199:                    throws JDOException {
200:                return pm.detachCopy(entity);
201:            }
202:
203:            /**
204:             * This implementation delegates to JDO 2.0's <code>detachCopyAll</code> method.
205:             * <p>To be overridden for pre-JDO2 implementations, using the corresponding
206:             * vendor-specific mechanism there.
207:             * @see javax.jdo.PersistenceManager#detachCopyAll(java.util.Collection)
208:             */
209:            public Collection detachCopyAll(PersistenceManager pm,
210:                    Collection entities) throws JDOException {
211:                return pm.detachCopyAll(entities);
212:            }
213:
214:            /**
215:             * This implementation delegates to JDO 2.0's <code>makePersistent</code> method,
216:             * which also serves as facility for reattaching objects as of JDO 2.0 final draft.
217:             * <p>To be overridden for pre-JDO2 implementations, using the corresponding
218:             * vendor-specific mechanism there.
219:             * <p>Note that previous JDO 2.0 drafts specified a dedicated <code>attachCopy</code>
220:             * method for this purpose, which Spring's DefaultJdoDialect used to delegate to.
221:             * As of Spring 2.0, this has been adapted to the final JDO 2.0 API. You can still
222:             * create a custom JdoDialect that uses the pre-final API methods, of course.
223:             * @see javax.jdo.PersistenceManager#makePersistent(Object)
224:             */
225:            public Object attachCopy(PersistenceManager pm,
226:                    Object detachedEntity) throws JDOException {
227:                return pm.makePersistent(detachedEntity);
228:            }
229:
230:            /**
231:             * This implementation delegates to JDO 2.0's <code>makePersistentAll</code> method,
232:             * which also serves as facility for reattaching objects as of JDO 2.0 final draft.
233:             * <p>To be overridden for pre-JDO2 implementations, using the corresponding
234:             * vendor-specific mechanism there.
235:             * <p>Note that previous JDO 2.0 drafts specified a dedicated <code>attachCopy</code>
236:             * method for this purpose, which Spring's DefaultJdoDialect used to delegate to.
237:             * As of Spring 2.0, this has been adapted to the final JDO 2.0 API. You can still
238:             * create a custom JdoDialect that uses the pre-final API methods, of course.
239:             * @see javax.jdo.PersistenceManager#makePersistentAll(java.util.Collection)
240:             */
241:            public Collection attachCopyAll(PersistenceManager pm,
242:                    Collection detachedEntities) throws JDOException {
243:                return pm.makePersistentAll(detachedEntities);
244:            }
245:
246:            /**
247:             * This implementation delegates to JDO 2.0's <code>flush</code> method.
248:             * <p>To be overridden for pre-JDO2 implementations, using the corresponding
249:             * vendor-specific mechanism there.
250:             * @see javax.jdo.PersistenceManager#flush()
251:             */
252:            public void flush(PersistenceManager pm) throws JDOException {
253:                pm.flush();
254:            }
255:
256:            /**
257:             * This implementation delegates to JDO 2.0's <code>newNamedQuery</code> method.
258:             * <p>To be overridden for pre-JDO2 implementations, using the corresponding
259:             * vendor-specific mechanism there.
260:             * @see javax.jdo.PersistenceManager#newNamedQuery(Class, String)
261:             */
262:            public Query newNamedQuery(PersistenceManager pm,
263:                    Class entityClass, String queryName) throws JDOException {
264:                return pm.newNamedQuery(entityClass, queryName);
265:            }
266:
267:            /**
268:             * This implementation logs a warning that it cannot apply a query timeout.
269:             */
270:            public void applyQueryTimeout(Query query,
271:                    int remainingTimeInSeconds) throws JDOException {
272:                logger
273:                        .info("DefaultJdoDialect does not support query timeouts: ignoring remaining transaction time");
274:            }
275:
276:            //-----------------------------------------------------------------------------------
277:            // Hook for exception translation (used by JdoTransactionManager and JdoTemplate)
278:            //-----------------------------------------------------------------------------------
279:
280:            /**
281:             * Implementation of the PersistenceExceptionTranslator interface,
282:             * as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.
283:             * <p>Converts the exception if it is a JDOException, using this JdoDialect.
284:             * Else returns <code>null</code> to indicate an unknown exception.
285:             * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
286:             * @see #translateException
287:             */
288:            public DataAccessException translateExceptionIfPossible(
289:                    RuntimeException ex) {
290:                if (ex instanceof  JDOException) {
291:                    return translateException((JDOException) ex);
292:                }
293:                return null;
294:            }
295:
296:            /**
297:             * This implementation delegates to PersistenceManagerFactoryUtils.
298:             * @see PersistenceManagerFactoryUtils#convertJdoAccessException
299:             */
300:            public DataAccessException translateException(JDOException ex) {
301:                if (getJdbcExceptionTranslator() != null
302:                        && ex.getCause() instanceof  SQLException) {
303:                    return getJdbcExceptionTranslator().translate(
304:                            "JDO operation: " + ex.getMessage(),
305:                            extractSqlStringFromException(ex),
306:                            (SQLException) ex.getCause());
307:                }
308:                return PersistenceManagerFactoryUtils
309:                        .convertJdoAccessException(ex);
310:            }
311:
312:            /**
313:             * Template method for extracting a SQL String from the given exception.
314:             * <p>Default implementation always returns <code>null</code>. Can be overridden in
315:             * subclasses to extract SQL Strings for vendor-specific exception classes.
316:             * @param ex the JDOException, containing a SQLException
317:             * @return the SQL String, or <code>null</code> if none found
318:             */
319:            protected String extractSqlStringFromException(JDOException ex) {
320:                return null;
321:            }
322:
323:            /**
324:             * ConnectionHandle implementation that fetches a new JDO2 DataStoreConnection
325:             * for every <code>getConnection</code> call and closes the Connection on
326:             * <code>releaseConnection</code>. This is necessary because JDO2 requires the
327:             * fetched Connection to be closed before continuing PersistenceManager work.
328:             * @see javax.jdo.PersistenceManager#getDataStoreConnection()
329:             */
330:            private static class DataStoreConnectionHandle implements 
331:                    ConnectionHandle {
332:
333:                private final PersistenceManager persistenceManager;
334:
335:                public DataStoreConnectionHandle(
336:                        PersistenceManager persistenceManager) {
337:                    this .persistenceManager = persistenceManager;
338:                }
339:
340:                public Connection getConnection() {
341:                    return (Connection) this .persistenceManager
342:                            .getDataStoreConnection();
343:                }
344:
345:                public void releaseConnection(Connection con) {
346:                    JdbcUtils.closeConnection(con);
347:                }
348:            }
349:
350:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.