Added "jtaDataSource" property to JPA LocalContainerEntityManagerFactoryBean (for default units)
Issue: SPR-9883
This commit is contained in:
@@ -172,8 +172,10 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
|
|||||||
* JDBC configuration in <code>persistence.xml</code>, passing in a Spring-managed
|
* JDBC configuration in <code>persistence.xml</code>, passing in a Spring-managed
|
||||||
* DataSource instead.
|
* DataSource instead.
|
||||||
* <p>In JPA speak, a DataSource passed in here will be used as "nonJtaDataSource"
|
* <p>In JPA speak, a DataSource passed in here will be used as "nonJtaDataSource"
|
||||||
* on the PersistenceUnitInfo passed to the PersistenceProvider, overriding
|
* on the PersistenceUnitInfo passed to the PersistenceProvider, as well as
|
||||||
* data source configuration in <code>persistence.xml</code> (if any).
|
* 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>
|
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
|
||||||
* @see javax.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource()
|
* @see javax.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource()
|
||||||
* @see #setPersistenceUnitManager
|
* @see #setPersistenceUnitManager
|
||||||
@@ -183,11 +185,28 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
|
|||||||
this.internalPersistenceUnitManager.setDefaultDataSource(dataSource);
|
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
|
* Set the PersistenceUnitPostProcessors to be applied to the
|
||||||
* PersistenceUnitInfo used for creating this EntityManagerFactory.
|
* PersistenceUnitInfo used for creating this EntityManagerFactory.
|
||||||
* <p>Such post-processors can, for example, register further entity
|
* <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>.
|
* <code>persistence.xml</code>.
|
||||||
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
|
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
|
||||||
* @see #setPersistenceUnitManager
|
* @see #setPersistenceUnitManager
|
||||||
@@ -319,9 +338,13 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage
|
|||||||
@Override
|
@Override
|
||||||
public DataSource getDataSource() {
|
public DataSource getDataSource() {
|
||||||
if (this.persistenceUnitInfo != null) {
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -117,6 +117,8 @@ public class DefaultPersistenceUnitManager
|
|||||||
|
|
||||||
private DataSource defaultDataSource;
|
private DataSource defaultDataSource;
|
||||||
|
|
||||||
|
private DataSource defaultJtaDataSource;
|
||||||
|
|
||||||
private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors;
|
private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors;
|
||||||
|
|
||||||
private LoadTimeWeaver loadTimeWeaver;
|
private LoadTimeWeaver loadTimeWeaver;
|
||||||
@@ -241,9 +243,9 @@ public class DefaultPersistenceUnitManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify the JDBC DataSource that the JPA persistence provider is supposed
|
* Specify the JDBC DataSource that the JPA persistence provider is supposed to use
|
||||||
* to use for accessing the database if none has been specified in
|
* for accessing the database if none has been specified in <code>persistence.xml</code>.
|
||||||
* <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"
|
* <p>In JPA speak, a DataSource passed in here will be uses as "nonJtaDataSource"
|
||||||
* on the PersistenceUnitInfo passed to the PersistenceProvider, provided that
|
* on the PersistenceUnitInfo passed to the PersistenceProvider, provided that
|
||||||
* none has been registered before.
|
* none has been registered before.
|
||||||
@@ -254,20 +256,39 @@ public class DefaultPersistenceUnitManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the JDBC DataSource that the JPA persistence provider is supposed
|
* Return the JDBC DataSource that the JPA persistence provider is supposed to use
|
||||||
* to use for accessing the database if none has been specified in
|
* for accessing the database if none has been specified in <code>persistence.xml</code>.
|
||||||
* <code>persistence.xml</code>.
|
|
||||||
*/
|
*/
|
||||||
public DataSource getDefaultDataSource() {
|
public DataSource getDefaultDataSource() {
|
||||||
return this.defaultDataSource;
|
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
|
* Set the PersistenceUnitPostProcessors to be applied to each
|
||||||
* PersistenceUnitInfo that has been parsed by this manager.
|
* PersistenceUnitInfo that has been parsed by this manager.
|
||||||
* <p>Such post-processors can, for example, register further entity
|
* <p>Such post-processors can, for example, register further entity classes and
|
||||||
* classes and jar files, in addition to the metadata read in from
|
* jar files, in addition to the metadata read from <code>persistence.xml</code>.
|
||||||
* <code>persistence.xml</code>.
|
|
||||||
*/
|
*/
|
||||||
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors) {
|
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors) {
|
||||||
this.persistenceUnitPostProcessors = postProcessors;
|
this.persistenceUnitPostProcessors = postProcessors;
|
||||||
@@ -342,6 +363,9 @@ public class DefaultPersistenceUnitManager
|
|||||||
if (pui.getPersistenceUnitRootUrl() == null) {
|
if (pui.getPersistenceUnitRootUrl() == null) {
|
||||||
pui.setPersistenceUnitRootUrl(determineDefaultPersistenceUnitRootUrl());
|
pui.setPersistenceUnitRootUrl(determineDefaultPersistenceUnitRootUrl());
|
||||||
}
|
}
|
||||||
|
if (pui.getJtaDataSource() == null) {
|
||||||
|
pui.setJtaDataSource(this.defaultJtaDataSource);
|
||||||
|
}
|
||||||
if (pui.getNonJtaDataSource() == null) {
|
if (pui.getNonJtaDataSource() == null) {
|
||||||
pui.setNonJtaDataSource(this.defaultDataSource);
|
pui.setNonJtaDataSource(this.defaultDataSource);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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
|
* Extension of the standard JPA PersistenceUnitInfo interface, for advanced collaboration
|
||||||
* between Spring's {@link org.springframework.orm.jpa.LocalEntityManagerFactoryBean} and
|
* between Spring's {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean}
|
||||||
* {@link PersistenceUnitManager} implementations.
|
* and {@link PersistenceUnitManager} implementations.
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @since 3.0.1
|
* @since 3.0.1
|
||||||
* @see PersistenceUnitManager
|
* @see PersistenceUnitManager
|
||||||
* @see org.springframework.orm.jpa.LocalEntityManagerFactoryBean
|
* @see org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
|
||||||
*/
|
*/
|
||||||
public interface SmartPersistenceUnitInfo extends PersistenceUnitInfo {
|
public interface SmartPersistenceUnitInfo extends PersistenceUnitInfo {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user