Added "jtaDataSource" property to JPA LocalContainerEntityManagerFactoryBean (for default units)

Issue: SPR-9883
This commit is contained in:
Juergen Hoeller 2012-10-31 01:08:52 +01:00 committed by unknown
parent 88ef3ce51f
commit 4ff765446e
3 changed files with 65 additions and 18 deletions

View File

@ -172,8 +172,10 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
* JDBC configuration in <code>persistence.xml</code>, passing in a Spring-managed
* DataSource instead.
* <p>In JPA speak, a DataSource passed in here will be used as "nonJtaDataSource"
* on the PersistenceUnitInfo passed to the PersistenceProvider, overriding
* data source configuration in <code>persistence.xml</code> (if any).
* on the PersistenceUnitInfo passed to the PersistenceProvider, as well as
* overriding data source configuration in <code>persistence.xml</code> (if any).
* Note that this variant typically works for JTA transaction management as well;
* if it does not, consider using the explicit {@link #setJtaDataSource} instead.
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
* @see javax.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource()
* @see #setPersistenceUnitManager
@ -183,11 +185,28 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
this.internalPersistenceUnitManager.setDefaultDataSource(dataSource);
}
/**
* Specify the JDBC DataSource that the JPA persistence provider is supposed
* to use for accessing the database. This is an alternative to keeping the
* JDBC configuration in <code>persistence.xml</code>, passing in a Spring-managed
* DataSource instead.
* <p>In JPA speak, a DataSource passed in here will be used as "jtaDataSource"
* on the PersistenceUnitInfo passed to the PersistenceProvider, as well as
* overriding data source configuration in <code>persistence.xml</code> (if any).
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
* @see javax.persistence.spi.PersistenceUnitInfo#getJtaDataSource()
* @see #setPersistenceUnitManager
*/
public void setJtaDataSource(DataSource jtaDataSource) {
this.internalPersistenceUnitManager.setDataSourceLookup(new SingleDataSourceLookup(jtaDataSource));
this.internalPersistenceUnitManager.setDefaultJtaDataSource(jtaDataSource);
}
/**
* Set the PersistenceUnitPostProcessors to be applied to the
* PersistenceUnitInfo used for creating this EntityManagerFactory.
* <p>Such post-processors can, for example, register further entity
* classes and jar files, in addition to the metadata read in from
* classes and jar files, in addition to the metadata read from
* <code>persistence.xml</code>.
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
* @see #setPersistenceUnitManager
@ -319,9 +338,13 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
@Override
public DataSource getDataSource() {
if (this.persistenceUnitInfo != null) {
return this.persistenceUnitInfo.getNonJtaDataSource();
return (this.persistenceUnitInfo.getJtaDataSource() != null ?
this.persistenceUnitInfo.getJtaDataSource() :
this.persistenceUnitInfo.getNonJtaDataSource());
}
return this.internalPersistenceUnitManager.getDefaultDataSource();
return (this.internalPersistenceUnitManager.getDefaultJtaDataSource() != null ?
this.internalPersistenceUnitManager.getDefaultJtaDataSource() :
this.internalPersistenceUnitManager.getDefaultDataSource());
}
}

View File

@ -117,6 +117,8 @@ public class DefaultPersistenceUnitManager
private DataSource defaultDataSource;
private DataSource defaultJtaDataSource;
private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors;
private LoadTimeWeaver loadTimeWeaver;
@ -241,9 +243,9 @@ public class DefaultPersistenceUnitManager
}
/**
* Specify the JDBC DataSource that the JPA persistence provider is supposed
* to use for accessing the database if none has been specified in
* <code>persistence.xml</code>.
* Specify the JDBC DataSource that the JPA persistence provider is supposed to use
* for accessing the database if none has been specified in <code>persistence.xml</code>.
* This variant indicates no special transaction setup, i.e. typical resource-local.
* <p>In JPA speak, a DataSource passed in here will be uses as "nonJtaDataSource"
* on the PersistenceUnitInfo passed to the PersistenceProvider, provided that
* none has been registered before.
@ -254,20 +256,39 @@ public class DefaultPersistenceUnitManager
}
/**
* Return the JDBC DataSource that the JPA persistence provider is supposed
* to use for accessing the database if none has been specified in
* <code>persistence.xml</code>.
* Return the JDBC DataSource that the JPA persistence provider is supposed to use
* for accessing the database if none has been specified in <code>persistence.xml</code>.
*/
public DataSource getDefaultDataSource() {
return this.defaultDataSource;
}
/**
* Specify the JDBC DataSource that the JPA persistence provider is supposed to use
* for accessing the database if none has been specified in <code>persistence.xml</code>.
* This variant indicates that JTA is supposed to be used as transaction type.
* <p>In JPA speak, a DataSource passed in here will be uses as "jtaDataSource"
* on the PersistenceUnitInfo passed to the PersistenceProvider, provided that
* none has been registered before.
* @see javax.persistence.spi.PersistenceUnitInfo#getJtaDataSource()
*/
public void setDefaultJtaDataSource(DataSource defaultJtaDataSource) {
this.defaultJtaDataSource = defaultJtaDataSource;
}
/**
* Return the JTA-aware DataSource that the JPA persistence provider is supposed to use
* for accessing the database if none has been specified in <code>persistence.xml</code>.
*/
public DataSource getDefaultJtaDataSource() {
return this.defaultJtaDataSource;
}
/**
* Set the PersistenceUnitPostProcessors to be applied to each
* PersistenceUnitInfo that has been parsed by this manager.
* <p>Such post-processors can, for example, register further entity
* classes and jar files, in addition to the metadata read in from
* <code>persistence.xml</code>.
* <p>Such post-processors can, for example, register further entity classes and
* jar files, in addition to the metadata read from <code>persistence.xml</code>.
*/
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors) {
this.persistenceUnitPostProcessors = postProcessors;
@ -342,6 +363,9 @@ public class DefaultPersistenceUnitManager
if (pui.getPersistenceUnitRootUrl() == null) {
pui.setPersistenceUnitRootUrl(determineDefaultPersistenceUnitRootUrl());
}
if (pui.getJtaDataSource() == null) {
pui.setJtaDataSource(this.defaultJtaDataSource);
}
if (pui.getNonJtaDataSource() == null) {
pui.setNonJtaDataSource(this.defaultDataSource);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 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.
@ -20,13 +20,13 @@ import javax.persistence.spi.PersistenceUnitInfo;
/**
* Extension of the standard JPA PersistenceUnitInfo interface, for advanced collaboration
* between Spring's {@link org.springframework.orm.jpa.LocalEntityManagerFactoryBean} and
* {@link PersistenceUnitManager} implementations.
* between Spring's {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean}
* and {@link PersistenceUnitManager} implementations.
*
* @author Juergen Hoeller
* @since 3.0.1
* @see PersistenceUnitManager
* @see org.springframework.orm.jpa.LocalEntityManagerFactoryBean
* @see org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
*/
public interface SmartPersistenceUnitInfo extends PersistenceUnitInfo {