KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jpa > DefaultJpaDialect


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.jpa;
18
19 import java.sql.SQLException JavaDoc;
20
21 import javax.persistence.EntityManager;
22 import javax.persistence.EntityManagerFactory;
23 import javax.persistence.PersistenceException;
24
25 import org.springframework.dao.DataAccessException;
26 import org.springframework.jdbc.datasource.ConnectionHandle;
27 import org.springframework.transaction.InvalidIsolationLevelException;
28 import org.springframework.transaction.TransactionDefinition;
29 import org.springframework.transaction.TransactionException;
30
31 /**
32  * Default implementation of the JpaDialect interface.
33  * Used as default dialect by JpaAccessor and JpaTransactionManager.
34  *
35  * <p>Simply begins a standard JPA transaction in <code>beginTransaction</code>
36  * and performs standard exception translation through EntityManagerFactoryUtils.
37  *
38  * @author Juergen Hoeller
39  * @since 2.0
40  * @see JpaAccessor#setJpaDialect
41  * @see JpaTransactionManager#setJpaDialect
42  */

43 public class DefaultJpaDialect implements JpaDialect {
44
45     //-------------------------------------------------------------------------
46
// Hooks for transaction management (used by JpaTransactionManager)
47
//-------------------------------------------------------------------------
48

49     /**
50      * This implementation invokes the standard JPA <code>Transaction.begin</code>
51      * method. Throws an InvalidIsolationLevelException if a non-default isolation
52      * level is set.
53      * <p>This implementation does not return any transaction data Object, since there
54      * is no state to be kept for a standard JPA transaction. Hence, subclasses do not
55      * have to care about the return value (<code>null</code>) of this implementation
56      * and are free to return their own transaction data Object.
57      * @see javax.persistence.EntityTransaction#begin
58      * @see org.springframework.transaction.InvalidIsolationLevelException
59      * @see #cleanupTransaction
60      */

61     public Object JavaDoc beginTransaction(EntityManager entityManager, TransactionDefinition definition)
62             throws PersistenceException, SQLException JavaDoc, TransactionException {
63
64         if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
65             throw new InvalidIsolationLevelException(
66                     "Standard JPA does not support custom isolation levels - " +
67                     "use a special JpaDialect for your JPA implementation");
68         }
69         entityManager.getTransaction().begin();
70         return null;
71     }
72
73     /**
74      * This implementation does nothing, since the default <code>beginTransaction</code>
75      * implementation does not require any cleanup.
76      * @see #beginTransaction
77      */

78     public void cleanupTransaction(Object JavaDoc transactionData) {
79     }
80
81     /**
82      * This implementation always returns <code>null</code>,
83      * indicating that no JDBC Connection can be provided.
84      */

85     public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly)
86             throws PersistenceException, SQLException JavaDoc {
87
88         return null;
89     }
90
91     /**
92      * This implementation does nothing, assuming that the Connection
93      * will implicitly be closed with the EntityManager.
94      * <p>If the JPA implementation returns a Connection handle that it expects
95      * the application to close after use, the dialect implementation needs to invoke
96      * <code>Connection.close()</code> (or some other method with similar effect) here.
97      * @see java.sql.Connection#close()
98      */

99     public void releaseJdbcConnection(ConnectionHandle conHandle, EntityManager em)
100             throws PersistenceException, SQLException JavaDoc {
101     }
102
103
104     //-----------------------------------------------------------------------------------
105
// Hook for exception translation (used by JpaTransactionManager and JpaTemplate)
106
//-----------------------------------------------------------------------------------
107

108     /**
109      * This implementation delegates to EntityManagerFactoryUtils.
110      * @see EntityManagerFactoryUtils#convertJpaAccessExceptionIfPossible
111      */

112     public DataAccessException translateExceptionIfPossible(RuntimeException JavaDoc ex) {
113         return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
114     }
115
116
117     public boolean supportsEntityManagerFactoryPlusOperations() {
118         return false;
119     }
120
121     public boolean supportsEntityManagerPlusOperations() {
122         return false;
123     }
124
125     public EntityManagerFactoryPlusOperations getEntityManagerFactoryPlusOperations(EntityManagerFactory rawEntityManager) {
126         throw new UnsupportedOperationException JavaDoc(getClass().getName() + " does not support EntityManagerFactoryPlusOperations");
127     }
128
129     public EntityManagerPlusOperations getEntityManagerPlusOperations(EntityManager rawEntityManager) {
130         throw new UnsupportedOperationException JavaDoc(getClass().getName() + " does not support EntityManagerPlusOperations");
131     }
132
133 }
134
Popular Tags