Skip to content

Commit 4ff7654

Browse files
jhoellerunknown
authored andcommitted
Added "jtaDataSource" property to JPA LocalContainerEntityManagerFactoryBean (for default units)
Issue: SPR-9883
1 parent 88ef3ce commit 4ff7654

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

spring-orm/src/main/java/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,10 @@ public void setMappingResources(String... mappingResources) {
172172
* JDBC configuration in <code>persistence.xml</code>, passing in a Spring-managed
173173
* DataSource instead.
174174
* <p>In JPA speak, a DataSource passed in here will be used as "nonJtaDataSource"
175-
* on the PersistenceUnitInfo passed to the PersistenceProvider, overriding
176-
* data source configuration in <code>persistence.xml</code> (if any).
175+
* on the PersistenceUnitInfo passed to the PersistenceProvider, as well as
176+
* overriding data source configuration in <code>persistence.xml</code> (if any).
177+
* Note that this variant typically works for JTA transaction management as well;
178+
* if it does not, consider using the explicit {@link #setJtaDataSource} instead.
177179
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
178180
* @see javax.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource()
179181
* @see #setPersistenceUnitManager
@@ -183,11 +185,28 @@ public void setDataSource(DataSource dataSource) {
183185
this.internalPersistenceUnitManager.setDefaultDataSource(dataSource);
184186
}
185187

188+
/**
189+
* Specify the JDBC DataSource that the JPA persistence provider is supposed
190+
* to use for accessing the database. This is an alternative to keeping the
191+
* JDBC configuration in <code>persistence.xml</code>, passing in a Spring-managed
192+
* DataSource instead.
193+
* <p>In JPA speak, a DataSource passed in here will be used as "jtaDataSource"
194+
* on the PersistenceUnitInfo passed to the PersistenceProvider, as well as
195+
* overriding data source configuration in <code>persistence.xml</code> (if any).
196+
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
197+
* @see javax.persistence.spi.PersistenceUnitInfo#getJtaDataSource()
198+
* @see #setPersistenceUnitManager
199+
*/
200+
public void setJtaDataSource(DataSource jtaDataSource) {
201+
this.internalPersistenceUnitManager.setDataSourceLookup(new SingleDataSourceLookup(jtaDataSource));
202+
this.internalPersistenceUnitManager.setDefaultJtaDataSource(jtaDataSource);
203+
}
204+
186205
/**
187206
* Set the PersistenceUnitPostProcessors to be applied to the
188207
* PersistenceUnitInfo used for creating this EntityManagerFactory.
189208
* <p>Such post-processors can, for example, register further entity
190-
* classes and jar files, in addition to the metadata read in from
209+
* classes and jar files, in addition to the metadata read from
191210
* <code>persistence.xml</code>.
192211
* <p><b>NOTE: Only applied if no external PersistenceUnitManager specified.</b>
193212
* @see #setPersistenceUnitManager
@@ -319,9 +338,13 @@ public String getPersistenceUnitName() {
319338
@Override
320339
public DataSource getDataSource() {
321340
if (this.persistenceUnitInfo != null) {
322-
return this.persistenceUnitInfo.getNonJtaDataSource();
341+
return (this.persistenceUnitInfo.getJtaDataSource() != null ?
342+
this.persistenceUnitInfo.getJtaDataSource() :
343+
this.persistenceUnitInfo.getNonJtaDataSource());
323344
}
324-
return this.internalPersistenceUnitManager.getDefaultDataSource();
345+
return (this.internalPersistenceUnitManager.getDefaultJtaDataSource() != null ?
346+
this.internalPersistenceUnitManager.getDefaultJtaDataSource() :
347+
this.internalPersistenceUnitManager.getDefaultDataSource());
325348
}
326349

327350
}

spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/DefaultPersistenceUnitManager.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ public class DefaultPersistenceUnitManager
117117

118118
private DataSource defaultDataSource;
119119

120+
private DataSource defaultJtaDataSource;
121+
120122
private PersistenceUnitPostProcessor[] persistenceUnitPostProcessors;
121123

122124
private LoadTimeWeaver loadTimeWeaver;
@@ -241,9 +243,9 @@ public DataSourceLookup getDataSourceLookup() {
241243
}
242244

243245
/**
244-
* Specify the JDBC DataSource that the JPA persistence provider is supposed
245-
* to use for accessing the database if none has been specified in
246-
* <code>persistence.xml</code>.
246+
* Specify the JDBC DataSource that the JPA persistence provider is supposed to use
247+
* for accessing the database if none has been specified in <code>persistence.xml</code>.
248+
* This variant indicates no special transaction setup, i.e. typical resource-local.
247249
* <p>In JPA speak, a DataSource passed in here will be uses as "nonJtaDataSource"
248250
* on the PersistenceUnitInfo passed to the PersistenceProvider, provided that
249251
* none has been registered before.
@@ -254,20 +256,39 @@ public void setDefaultDataSource(DataSource defaultDataSource) {
254256
}
255257

256258
/**
257-
* Return the JDBC DataSource that the JPA persistence provider is supposed
258-
* to use for accessing the database if none has been specified in
259-
* <code>persistence.xml</code>.
259+
* Return the JDBC DataSource that the JPA persistence provider is supposed to use
260+
* for accessing the database if none has been specified in <code>persistence.xml</code>.
260261
*/
261262
public DataSource getDefaultDataSource() {
262263
return this.defaultDataSource;
263264
}
264265

266+
/**
267+
* Specify the JDBC DataSource that the JPA persistence provider is supposed to use
268+
* for accessing the database if none has been specified in <code>persistence.xml</code>.
269+
* This variant indicates that JTA is supposed to be used as transaction type.
270+
* <p>In JPA speak, a DataSource passed in here will be uses as "jtaDataSource"
271+
* on the PersistenceUnitInfo passed to the PersistenceProvider, provided that
272+
* none has been registered before.
273+
* @see javax.persistence.spi.PersistenceUnitInfo#getJtaDataSource()
274+
*/
275+
public void setDefaultJtaDataSource(DataSource defaultJtaDataSource) {
276+
this.defaultJtaDataSource = defaultJtaDataSource;
277+
}
278+
279+
/**
280+
* Return the JTA-aware DataSource that the JPA persistence provider is supposed to use
281+
* for accessing the database if none has been specified in <code>persistence.xml</code>.
282+
*/
283+
public DataSource getDefaultJtaDataSource() {
284+
return this.defaultJtaDataSource;
285+
}
286+
265287
/**
266288
* Set the PersistenceUnitPostProcessors to be applied to each
267289
* PersistenceUnitInfo that has been parsed by this manager.
268-
* <p>Such post-processors can, for example, register further entity
269-
* classes and jar files, in addition to the metadata read in from
270-
* <code>persistence.xml</code>.
290+
* <p>Such post-processors can, for example, register further entity classes and
291+
* jar files, in addition to the metadata read from <code>persistence.xml</code>.
271292
*/
272293
public void setPersistenceUnitPostProcessors(PersistenceUnitPostProcessor... postProcessors) {
273294
this.persistenceUnitPostProcessors = postProcessors;
@@ -342,6 +363,9 @@ public void preparePersistenceUnitInfos() {
342363
if (pui.getPersistenceUnitRootUrl() == null) {
343364
pui.setPersistenceUnitRootUrl(determineDefaultPersistenceUnitRootUrl());
344365
}
366+
if (pui.getJtaDataSource() == null) {
367+
pui.setJtaDataSource(this.defaultJtaDataSource);
368+
}
345369
if (pui.getNonJtaDataSource() == null) {
346370
pui.setNonJtaDataSource(this.defaultDataSource);
347371
}

spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/SmartPersistenceUnitInfo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,13 +20,13 @@
2020

2121
/**
2222
* Extension of the standard JPA PersistenceUnitInfo interface, for advanced collaboration
23-
* between Spring's {@link org.springframework.orm.jpa.LocalEntityManagerFactoryBean} and
24-
* {@link PersistenceUnitManager} implementations.
23+
* between Spring's {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean}
24+
* and {@link PersistenceUnitManager} implementations.
2525
*
2626
* @author Juergen Hoeller
2727
* @since 3.0.1
2828
* @see PersistenceUnitManager
29-
* @see org.springframework.orm.jpa.LocalEntityManagerFactoryBean
29+
* @see org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
3030
*/
3131
public interface SmartPersistenceUnitInfo extends PersistenceUnitInfo {
3232

0 commit comments

Comments
 (0)