diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java b/spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java index da80e70009..5101620a5e 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java @@ -172,8 +172,10 @@ public class LocalContainerEntityManagerFactoryBean extends AbstractEntityManage * JDBC configuration in persistence.xml, passing in a Spring-managed * DataSource instead. *

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 persistence.xml (if any). + * on the PersistenceUnitInfo passed to the PersistenceProvider, as well as + * overriding data source configuration in persistence.xml (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. *

NOTE: Only applied if no external PersistenceUnitManager specified. * @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 persistence.xml, passing in a Spring-managed + * DataSource instead. + *

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 persistence.xml (if any). + *

NOTE: Only applied if no external PersistenceUnitManager specified. + * @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. *

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 * persistence.xml. *

NOTE: Only applied if no external PersistenceUnitManager specified. * @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()); } } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java index e7832284bc..728e5f832b 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java @@ -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 - * persistence.xml. + * Specify the JDBC DataSource that the JPA persistence provider is supposed to use + * for accessing the database if none has been specified in persistence.xml. + * This variant indicates no special transaction setup, i.e. typical resource-local. *

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 - * persistence.xml. + * Return the JDBC DataSource that the JPA persistence provider is supposed to use + * for accessing the database if none has been specified in persistence.xml. */ 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 persistence.xml. + * This variant indicates that JTA is supposed to be used as transaction type. + *

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 persistence.xml. + */ + public DataSource getDefaultJtaDataSource() { + return this.defaultJtaDataSource; + } + /** * Set the PersistenceUnitPostProcessors to be applied to each * PersistenceUnitInfo that has been parsed by this manager. - *

Such post-processors can, for example, register further entity - * classes and jar files, in addition to the metadata read in from - * persistence.xml. + *

Such post-processors can, for example, register further entity classes and + * jar files, in addition to the metadata read from persistence.xml. */ 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); } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/SmartPersistenceUnitInfo.java b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/SmartPersistenceUnitInfo.java index 1d90c22251..a5dd4affad 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/SmartPersistenceUnitInfo.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/SmartPersistenceUnitInfo.java @@ -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 {