home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (EclipseLinkJpaDialect.java)

This example Spring Framework source code file (EclipseLinkJpaDialect.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

abstractsession, connectionhandle, jdbc, jpaentitymanager, jpaentitymanager, object, override, persistenceexception, persistenceexception, sql, sqlexception, sqlexception, transactiondefinition, transactionexception, unitofwork, unitofwork

The Spring Framework EclipseLinkJpaDialect.java source code

 * Copyright 2002-2008 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package org.springframework.orm.jpa.vendor;

import java.sql.Connection;
import java.sql.SQLException;

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

import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.jpa.JpaEntityManager;
import org.eclipse.persistence.sessions.Session;
import org.eclipse.persistence.sessions.UnitOfWork;

import org.springframework.jdbc.datasource.ConnectionHandle;
import org.springframework.jdbc.datasource.SimpleConnectionHandle;
import org.springframework.orm.jpa.DefaultJpaDialect;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;

 * {@link org.springframework.orm.jpa.JpaDialect} implementation for
 * Eclipse Persistence Services (EclipseLink). Developed and tested
 * against EclipseLink 1.0 M4.
 * <p>By default, this class acquires a EclipseLink transaction to get the JDBC Connection
 * early. This allows mixing JDBC and JPA/EclipseLink operations in the same transaction.
 * In some cases, this eager acquisition of a transaction/connection may impact
 * scalability. In that case, set the "lazyDatabaseTransaction" flag to true if you
 * do not require mixing JDBC and JPA operations in the same transaction. Otherwise,
 * use a {@link org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy}
 * to ensure that the cost of connection acquisition is near zero until code actually
 * needs a JDBC Connection.
 * <p>This class is very analogous to {@link TopLinkJpaDialect}, since
 * EclipseLink is effectively the next generation of the TopLink product.
 * Thanks to Mike Keith for the original EclipseLink support prototype!
 * @author Juergen Hoeller
 * @since 2.5.2
 * @see #setLazyDatabaseTransaction
 * @see org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
public class EclipseLinkJpaDialect extends DefaultJpaDialect {

	private boolean lazyDatabaseTransaction = false;

	 * Set whether to lazily start a database transaction within an
	 * EclipseLink transaction.
	 * <p>By default, database transactions are started early. This allows
	 * for reusing the same JDBC Connection throughout an entire transaction,
	 * including read operations, and also for exposing EclipseLink transactions
	 * to JDBC access code (working on the same DataSource).
	 * <p>It is only recommended to switch this flag to "true" when no JDBC access
	 * code is involved in any of the transactions, and when it is acceptable to
	 * perform read operations outside of the transactional JDBC Connection.
	 * @see oracle.toplink.sessions.UnitOfWork#beginEarlyTransaction()
	public void setLazyDatabaseTransaction(boolean lazyDatabaseTransaction) {
		this.lazyDatabaseTransaction = lazyDatabaseTransaction;

	public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
			throws PersistenceException, SQLException, TransactionException {

		super.beginTransaction(entityManager, definition);
		if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) {
			// This is the magic bit. As with the existing Spring TopLink integration,
			// begin an early transaction to force EclipseLink to get a JDBC Connection
			// so that Spring can manage transactions with JDBC as well as EclipseLink.
			UnitOfWork uow = (UnitOfWork) getSession(entityManager);
		// Could return the UOW, if there were any advantage in having it later.
		return null;

	public ConnectionHandle getJdbcConnection(EntityManager em, boolean readOnly)
			throws PersistenceException, SQLException {

		AbstractSession session = (AbstractSession) getSession(em);
		// The connection was already acquired eagerly in beginTransaction,
		// unless lazyDatabaseTransaction was set to true.
		Connection con = session.getAccessor().getConnection();
		return (con != null ? new SimpleConnectionHandle(con) : null);

	 * Get a traditional EclipseLink Session from the given EntityManager.
	protected Session getSession(EntityManager em) {
		JpaEntityManager emi = (JpaEntityManager) em;
		return emi.getActiveSession();


Other Spring Framework examples (source code examples)

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

my book on functional programming


new blog posts


Copyright 1998-2019 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.