1 /*
2 * Copyright 2002-2007 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.io.Serializable;
20 import java.sql.SQLException;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.EntityManagerFactory;
24 import javax.persistence.PersistenceException;
25
26 import org.springframework.dao.DataAccessException;
27 import org.springframework.jdbc.datasource.ConnectionHandle;
28 import org.springframework.transaction.InvalidIsolationLevelException;
29 import org.springframework.transaction.TransactionDefinition;
30 import org.springframework.transaction.TransactionException;
31
32 /**
33 * Default implementation of the {@link JpaDialect} interface.
34 * Used as default dialect by {@link JpaAccessor} and {@link JpaTransactionManager}.
35 *
36 * <p>Simply begins a standard JPA transaction in {@link #beginTransaction}
37 * and performs standard exception translation through {@link EntityManagerFactoryUtils}.
38 *
39 * @author Juergen Hoeller
40 * @since 2.0
41 * @see JpaAccessor#setJpaDialect
42 * @see JpaTransactionManager#setJpaDialect
43 */
44 public class DefaultJpaDialect implements JpaDialect, Serializable {
45
46 //-------------------------------------------------------------------------
47 // Hooks for transaction management (used by JpaTransactionManager)
48 //-------------------------------------------------------------------------
49
50 /**
51 * This implementation invokes the standard JPA <code>Transaction.begin</code>
52 * method. Throws an InvalidIsolationLevelException if a non-default isolation
53 * level is set.
54 * <p>This implementation does not return any transaction data Object, since there
55 * is no state to be kept for a standard JPA transaction. Hence, subclasses do not
56 * have to care about the return value (<code>null</code>) of this implementation
57 * and are free to return their own transaction data Object.
58 * @see javax.persistence.EntityTransaction#begin
59 * @see org.springframework.transaction.InvalidIsolationLevelException
60 * @see #cleanupTransaction
61 */
62 public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
63 throws PersistenceException, SQLException, TransactionException {
64
65 if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
66 throw new InvalidIsolationLevelException(
67 "Standard JPA does not support custom isolation levels - " +
68 "use a special JpaDialect for your JPA implementation");
69 }
70 entityManager.getTransaction().begin();
71 return null;
72 }
73
74 public Object prepareTransaction(EntityManager entityManager, boolean readOnly, String name)
75 throws PersistenceException {
76
77 return null;
78 }
79
80 /**
81 * This implementation does nothing, since the default <code>beginTransaction</code>
82 * implementation does not require any cleanup.
83 * @see #beginTransaction
84 */
85 public void cleanupTransaction(Object transactionData) {
86 }
87
88 /**
89 * This implementation always returns <code>null</code>,
90 * indicating that no JDBC Connection can be provided.
91 */
92 public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly)
93 throws PersistenceException, SQLException {
94
95 return null;
96 }
97
98 /**
99 * This implementation does nothing, assuming that the Connection
100 * will implicitly be closed with the EntityManager.
101 * <p>If the JPA implementation returns a Connection handle that it expects
102 * the application to close after use, the dialect implementation needs to invoke
103 * <code>Connection.close()</code> (or some other method with similar effect) here.
104 * @see java.sql.Connection#close()
105 */
106 public void releaseJdbcConnection(ConnectionHandle conHandle, EntityManager em)
107 throws PersistenceException, SQLException {
108 }
109
110
111 //-----------------------------------------------------------------------------------
112 // Hook for exception translation (used by JpaTransactionManager and JpaTemplate)
113 //-----------------------------------------------------------------------------------
114
115 /**
116 * This implementation delegates to EntityManagerFactoryUtils.
117 * @see EntityManagerFactoryUtils#convertJpaAccessExceptionIfPossible
118 */
119 public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
120 return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
121 }
122
123
124 public boolean supportsEntityManagerFactoryPlusOperations() {
125 return false;
126 }
127
128 public boolean supportsEntityManagerPlusOperations() {
129 return false;
130 }
131
132 public EntityManagerFactoryPlusOperations getEntityManagerFactoryPlusOperations(EntityManagerFactory rawEntityManager) {
133 throw new UnsupportedOperationException(getClass().getName() + " does not support EntityManagerFactoryPlusOperations");
134 }
135
136 public EntityManagerPlusOperations getEntityManagerPlusOperations(EntityManager rawEntityManager) {
137 throw new UnsupportedOperationException(getClass().getName() + " does not support EntityManagerPlusOperations");
138 }
139
140 }