alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (DefaultJpaDialect.java)

This example Spring Framework source code file (DefaultJpaDialect.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

connectionhandle, entitymanagerfactoryplusoperations, entitymanagerplusoperations, invalidisolationlevelexception, io, jdbc, jpa, jpa, jpadialect, object, object, persistenceexception, persistenceexception, serializable, sql, sqlexception, sqlexception

The Spring Framework DefaultJpaDialect.java source code

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

package org.springframework.orm.jpa;

import java.io.Serializable;
import java.sql.SQLException;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.ConnectionHandle;
import org.springframework.transaction.InvalidIsolationLevelException;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;

/**
 * Default implementation of the {@link JpaDialect} interface.
 * Used as default dialect by {@link JpaAccessor} and {@link JpaTransactionManager}.
 *
 * <p>Simply begins a standard JPA transaction in {@link #beginTransaction}
 * and performs standard exception translation through {@link EntityManagerFactoryUtils}.
 *
 * @author Juergen Hoeller
 * @since 2.0
 * @see JpaAccessor#setJpaDialect
 * @see JpaTransactionManager#setJpaDialect
 */
public class DefaultJpaDialect implements JpaDialect, Serializable {

	//-------------------------------------------------------------------------
	// Hooks for transaction management (used by JpaTransactionManager)
	//-------------------------------------------------------------------------

	/**
	 * This implementation invokes the standard JPA <code>Transaction.begin
	 * method. Throws an InvalidIsolationLevelException if a non-default isolation
	 * level is set.
	 * <p>This implementation does not return any transaction data Object, since there
	 * is no state to be kept for a standard JPA transaction. Hence, subclasses do not
	 * have to care about the return value (<code>null) of this implementation
	 * and are free to return their own transaction data Object.
	 * @see javax.persistence.EntityTransaction#begin
	 * @see org.springframework.transaction.InvalidIsolationLevelException
	 * @see #cleanupTransaction
	 */
	public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
			throws PersistenceException, SQLException, TransactionException {

		if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
			throw new InvalidIsolationLevelException(
					"Standard JPA does not support custom isolation levels - " +
					"use a special JpaDialect for your JPA implementation");
		}
		entityManager.getTransaction().begin();
		return null;
	}

	public Object prepareTransaction(EntityManager entityManager, boolean readOnly, String name)
			throws PersistenceException {

		return null;
	}

	/**
	 * This implementation does nothing, since the default <code>beginTransaction
	 * implementation does not require any cleanup.
	 * @see #beginTransaction
	 */
	public void cleanupTransaction(Object transactionData) {
	}

	/**
	 * This implementation always returns <code>null,
	 * indicating that no JDBC Connection can be provided.
	 */
	public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly)
			throws PersistenceException, SQLException {

		return null;
	}

	/**
	 * This implementation does nothing, assuming that the Connection
	 * will implicitly be closed with the EntityManager.
	 * <p>If the JPA implementation returns a Connection handle that it expects
	 * the application to close after use, the dialect implementation needs to invoke
	 * <code>Connection.close() (or some other method with similar effect) here.
	 * @see java.sql.Connection#close()
	 */
	public void releaseJdbcConnection(ConnectionHandle conHandle, EntityManager em)
			throws PersistenceException, SQLException {
	}


	//-----------------------------------------------------------------------------------
	// Hook for exception translation (used by JpaTransactionManager and JpaTemplate)
	//-----------------------------------------------------------------------------------

	/**
	 * This implementation delegates to EntityManagerFactoryUtils.
	 * @see EntityManagerFactoryUtils#convertJpaAccessExceptionIfPossible
	 */
	public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
		return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
	}


	public boolean supportsEntityManagerFactoryPlusOperations() {
		return false;
	}

	public boolean supportsEntityManagerPlusOperations() {
		return false;
	}

	public EntityManagerFactoryPlusOperations getEntityManagerFactoryPlusOperations(EntityManagerFactory rawEntityManager) {
		throw new UnsupportedOperationException(getClass().getName() + " does not support EntityManagerFactoryPlusOperations");
	}

	public EntityManagerPlusOperations getEntityManagerPlusOperations(EntityManager rawEntityManager) {
		throw new UnsupportedOperationException(getClass().getName() + " does not support EntityManagerPlusOperations");
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework DefaultJpaDialect.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.