Skip to content

Commit 01e9307

Browse files
committed
DataSourceTransactionManager triggers flush callbacks on registered transaction synchronizations
Issue: SPR-14847 (cherry picked from commit 2874066)
1 parent 5697cb6 commit 01e9307

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceTransactionManager.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -28,6 +28,7 @@
2828
import org.springframework.transaction.support.DefaultTransactionStatus;
2929
import org.springframework.transaction.support.ResourceTransactionManager;
3030
import org.springframework.transaction.support.TransactionSynchronizationManager;
31+
import org.springframework.transaction.support.TransactionSynchronizationUtils;
3132

3233
/**
3334
* {@link org.springframework.transaction.PlatformTransactionManager}
@@ -47,14 +48,14 @@
4748
*
4849
* <p>Application code is required to retrieve the JDBC Connection via
4950
* {@link DataSourceUtils#getConnection(DataSource)} instead of a standard
50-
* J2EE-style {@link DataSource#getConnection()} call. Spring classes such as
51+
* Java EE-style {@link DataSource#getConnection()} call. Spring classes such as
5152
* {@link org.springframework.jdbc.core.JdbcTemplate} use this strategy implicitly.
5253
* If not used in combination with this transaction manager, the
5354
* {@link DataSourceUtils} lookup strategy behaves exactly like the native
5455
* DataSource lookup; it can thus be used in a portable fashion.
5556
*
5657
* <p>Alternatively, you can allow application code to work with the standard
57-
* J2EE-style lookup pattern {@link DataSource#getConnection()}, for example for
58+
* Java EE-style lookup pattern {@link DataSource#getConnection()}, for example for
5859
* legacy code that is not aware of Spring at all. In that case, define a
5960
* {@link TransactionAwareDataSourceProxy} for your target DataSource, and pass
6061
* that proxy DataSource to your DAOs, which will automatically participate in
@@ -87,6 +88,12 @@
8788
* DBCP connection pool). Switching between this local strategy and a JTA
8889
* environment is just a matter of configuration!
8990
*
91+
* <p>As of 4.3.4, this transaction manager triggers flush callbacks on registered
92+
* transaction synchronizations (if synchronization is generally active), assuming
93+
* resources operating on the underlying JDBC {@code Connection}. This allows for
94+
* setup analogous to {@code JtaTransactionManager}, in particular with respect to
95+
* lazily registered ORM resources (e.g. a Hibernate {@code Session}).
96+
*
9097
* @author Juergen Hoeller
9198
* @since 02.05.2003
9299
* @see #setNestedTransactionAllowed
@@ -368,6 +375,13 @@ public void setRollbackOnly() {
368375
public boolean isRollbackOnly() {
369376
return getConnectionHolder().isRollbackOnly();
370377
}
378+
379+
@Override
380+
public void flush() {
381+
if (TransactionSynchronizationManager.isSynchronizationActive()) {
382+
TransactionSynchronizationUtils.triggerFlush();
383+
}
384+
}
371385
}
372386

373387
}

spring-tx/src/main/java/org/springframework/transaction/TransactionStatus.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -80,6 +80,10 @@ public interface TransactionStatus extends SavepointManager, Flushable {
8080
/**
8181
* Flush the underlying session to the datastore, if applicable:
8282
* for example, all affected Hibernate/JPA sessions.
83+
* <p>This is effectively just a hint and may be a no-op if the underlying
84+
* transaction manager does not have a flush concept. A flush signal may
85+
* get applied to the primary resource or to transaction synchronizations,
86+
* depending on the underlying resource.
8387
*/
8488
@Override
8589
void flush();

0 commit comments

Comments
 (0)