1 /* 2 * Copyright 2002-2014 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 import javax.persistence.EntityManager; 22 import javax.persistence.PersistenceException; 23 24 import org.springframework.dao.DataAccessException; 25 import org.springframework.jdbc.datasource.ConnectionHandle; 26 import org.springframework.transaction.InvalidIsolationLevelException; 27 import org.springframework.transaction.TransactionDefinition; 28 import org.springframework.transaction.TransactionException; 29 30 /** 31 * Default implementation of the {@link JpaDialect} interface. 32 * Used as default dialect by {@link JpaTransactionManager}. 33 * 34 * <p>Simply begins a standard JPA transaction in {@link #beginTransaction} and 35 * performs standard exception translation through {@link EntityManagerFactoryUtils}. 36 * 37 * <p><b>NOTE: Spring's JPA support requires JPA 2.0 or higher, as of Spring 4.0.</b> 38 * 39 * @author Juergen Hoeller 40 * @since 2.0 41 * @see JpaTransactionManager#setJpaDialect 42 */ 43 @SuppressWarnings("serial") 44 public class DefaultJpaDialect implements JpaDialect, Serializable { 45 46 /** 47 * This implementation invokes the standard JPA {@code Transaction.begin} 48 * method. Throws an InvalidIsolationLevelException if a non-default isolation 49 * level is set. 50 * <p>This implementation does not return any transaction data Object, since there 51 * is no state to be kept for a standard JPA transaction. Hence, subclasses do not 52 * have to care about the return value ({@code null}) of this implementation 53 * and are free to return their own transaction data Object. 54 * @see javax.persistence.EntityTransaction#begin 55 * @see org.springframework.transaction.InvalidIsolationLevelException 56 * @see #cleanupTransaction 57 */ 58 @Override 59 public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) 60 throws PersistenceException, SQLException, TransactionException { 61 62 if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { 63 throw new InvalidIsolationLevelException(getClass().getSimpleName() + 64 " does not support custom isolation levels due to limitations in standard JPA. " + 65 "Specific arrangements may be implemented in custom JpaDialect variants."); 66 } 67 entityManager.getTransaction().begin(); 68 return null; 69 } 70 71 @Override 72 public Object prepareTransaction(EntityManager entityManager, boolean readOnly, String name) 73 throws PersistenceException { 74 75 return null; 76 } 77 78 /** 79 * This implementation does nothing, since the default {@code beginTransaction} 80 * implementation does not require any cleanup. 81 * @see #beginTransaction 82 */ 83 @Override 84 public void cleanupTransaction(Object transactionData) { 85 } 86 87 /** 88 * This implementation always returns {@code null}, 89 * indicating that no JDBC Connection can be provided. 90 */ 91 @Override 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()} (or some other method with similar effect) here. 104 * @see java.sql.Connection#close() 105 */ 106 @Override 107 public void releaseJdbcConnection(ConnectionHandle conHandle, EntityManager em) 108 throws PersistenceException, SQLException { 109 } 110 111 112 //----------------------------------------------------------------------------------- 113 // Hook for exception translation (used by JpaTransactionManager) 114 //----------------------------------------------------------------------------------- 115 116 /** 117 * This implementation delegates to EntityManagerFactoryUtils. 118 * @see EntityManagerFactoryUtils#convertJpaAccessExceptionIfPossible 119 */ 120 @Override 121 public DataAccessException translateExceptionIfPossible(RuntimeException ex) { 122 return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex); 123 } 124 125 }