@@ -379,11 +379,11 @@ moving from local to global transactions or vice versa.
379
379
[[tx-resource-synchronization]]
380
380
=== Synchronizing Resources with Transactions
381
381
382
- How to create different transaction managers and how they are
383
- linked to related resources that need to be synchronized to transactions (for example
384
- `DataSourceTransactionManager` to a JDBC `DataSource`, `HibernateTransactionManager` to
385
- a Hibernate `SessionFactory`, and so forth) should now be clear. This section describes how the application
386
- code (directly or indirectly, by using a persistence API such as JDBC, Hibernate, or JPA)
382
+ How to create different transaction managers and how they are linked to related resources
383
+ that need to be synchronized to transactions (for example `DataSourceTransactionManager`
384
+ to a JDBC `DataSource`, `HibernateTransactionManager` to a Hibernate `SessionFactory`,
385
+ and so forth) should now be clear. This section describes how the application code
386
+ (directly or indirectly, by using a persistence API such as JDBC, Hibernate, or JPA)
387
387
ensures that these resources are created, reused, and cleaned up properly. The section
388
388
also discusses how transaction synchronization is (optionally) triggered through the
389
389
relevant `PlatformTransactionManager`.
@@ -530,16 +530,16 @@ on unchecked exceptions), it is often useful to customize this behavior.
530
530
531
531
It is not sufficient merely to tell you to annotate your classes with the
532
532
`@Transactional` annotation, add `@EnableTransactionManagement` to your configuration,
533
- and expect you to understand how it all works. To provide a deeper understanding, this section explains the inner
534
- workings of the Spring Framework's declarative transaction infrastructure in the event
535
- of transaction-related issues.
533
+ and expect you to understand how it all works. To provide a deeper understanding,
534
+ this section explains the inner workings of the Spring Framework's declarative
535
+ transaction infrastructure in the event of transaction-related issues.
536
536
537
537
The most important concepts to grasp with regard to the Spring Framework's declarative
538
538
transaction support are that this support is enabled
539
- <<core.adoc#aop-understanding-aop-proxies,via AOP proxies>> and that the transactional advice
540
- is driven by metadata (currently XML- or annotation-based). The combination of AOP
541
- with transactional metadata yields an AOP proxy that uses a `TransactionInterceptor` in
542
- conjunction with an appropriate `PlatformTransactionManager` implementation to drive
539
+ <<core.adoc#aop-understanding-aop-proxies,via AOP proxies>> and that the transactional
540
+ advice is driven by metadata (currently XML- or annotation-based). The combination of
541
+ AOP with transactional metadata yields an AOP proxy that uses a `TransactionInterceptor`
542
+ in conjunction with an appropriate `PlatformTransactionManager` implementation to drive
543
543
transactions around method invocations.
544
544
545
545
NOTE: Spring AOP is covered in <<core.adoc#aop, the AOP section>>.
@@ -1073,13 +1073,13 @@ source code puts the declarations much closer to the affected code. There is not
1073
1073
danger of undue coupling, because code that is meant to be used transactionally is
1074
1074
almost always deployed that way anyway.
1075
1075
1076
- NOTE: The standard `javax.transaction.Transactional` annotation is also supported as a drop-in
1077
- replacement to Spring's own annotation. Please refer to JTA 1.2 documentation for more
1078
- details.
1076
+ NOTE: The standard `javax.transaction.Transactional` annotation is also supported as a
1077
+ drop-in replacement to Spring's own annotation. Please refer to JTA 1.2 documentation
1078
+ for more details.
1079
1079
1080
1080
The ease-of-use afforded by the use of the `@Transactional` annotation is best
1081
- illustrated with an example, which is explained in the text that follows. Consider the
1082
- following class definition:
1081
+ illustrated with an example, which is explained in the text that follows.
1082
+ Consider the following class definition:
1083
1083
1084
1084
====
1085
1085
[source,java,indent=0]
@@ -1154,8 +1154,8 @@ for full details.
1154
1154
When you use proxies, you should apply the `@Transactional` annotation only to methods
1155
1155
with public visibility. If you do annotate protected, private or package-visible
1156
1156
methods with the `@Transactional` annotation, no error is raised, but the annotated
1157
- method does not exhibit the configured transactional settings. If you need to annotate non-public methods, consider using
1158
- AspectJ (described later).
1157
+ method does not exhibit the configured transactional settings. If you need to annotate
1158
+ non-public methods, consider using AspectJ (described later).
1159
1159
****
1160
1160
1161
1161
You can place the `@Transactional` annotation before an interface definition, a method
@@ -1167,28 +1167,27 @@ the metadata to configure the appropriate beans with transactional behavior. In
1167
1167
preceding example, the `<tx:annotation-driven/>` element switches on the
1168
1168
transactional behavior.
1169
1169
1170
- TIP: The Spring team recommends that you annotate only concrete classes (and methods of concrete
1171
- classes) with the `@Transactional` annotation, as opposed to annotating interfaces. You
1172
- certainly can place the `@Transactional` annotation on an interface (or an interface
1170
+ TIP: The Spring team recommends that you annotate only concrete classes (and methods of
1171
+ concrete classes) with the `@Transactional` annotation, as opposed to annotating interfaces.
1172
+ You certainly can place the `@Transactional` annotation on an interface (or an interface
1173
1173
method), but this works only as you would expect it to if you use interface-based
1174
1174
proxies. The fact that Java annotations are not inherited from interfaces means that,
1175
1175
if you use class-based proxies (`proxy-target-class="true"`) or the weaving-based
1176
1176
aspect (`mode="aspectj"`), the transaction settings are not recognized by the
1177
1177
proxying and weaving infrastructure, and the object is not wrapped in a
1178
1178
transactional proxy, which would be decidedly bad.
1179
1179
1180
- NOTE: In proxy mode (which is the default), only external method calls coming in through the
1181
- proxy are intercepted. This means that self-invocation (in effect, a method within the
1182
- target object calling another method of the target object) does not lead to an actual
1180
+ NOTE: In proxy mode (which is the default), only external method calls coming in through
1181
+ the proxy are intercepted. This means that self-invocation (in effect, a method within
1182
+ the target object calling another method of the target object) does not lead to an actual
1183
1183
transaction at runtime even if the invoked method is marked with `@Transactional`. Also,
1184
1184
the proxy must be fully initialized to provide the expected behavior, so you should not
1185
1185
rely on this feature in your initialization code (that is, `@PostConstruct`).
1186
1186
1187
- Consider using of AspectJ mode (see the `mode` attribute in the following table) if you expect
1188
- self-invocations to be wrapped with transactions as well. In this case, there no
1189
- proxy in the first place. Instead, the target class is woven (that is, its
1190
- byte code is modified) to turn `@Transactional` into runtime behavior on
1191
- any kind of method.
1187
+ Consider using of AspectJ mode (see the `mode` attribute in the following table) if you
1188
+ expect self-invocations to be wrapped with transactions as well. In this case, there no
1189
+ proxy in the first place. Instead, the target class is woven (that is, its byte code is
1190
+ modified) to turn `@Transactional` into runtime behavior on any kind of method.
1192
1191
1193
1192
[[tx-annotation-driven-settings]]
1194
1193
.Annotation driven transaction settings
@@ -1232,9 +1231,9 @@ any kind of method.
1232
1231
No specified ordering means that the AOP subsystem determines the order of the advice.
1233
1232
|===
1234
1233
1235
- NOTE: The default advice mode for processing `@Transactional` annotations is `proxy`, which
1236
- allows for interception of calls through the proxy only. Local calls within the same
1237
- class cannot get intercepted that way. For a more advanced mode of interception,
1234
+ NOTE: The default advice mode for processing `@Transactional` annotations is `proxy`,
1235
+ which allows for interception of calls through the proxy only. Local calls within the
1236
+ same class cannot get intercepted that way. For a more advanced mode of interception,
1238
1237
consider switching to `aspectj` mode in combination with compile-time or load-time weaving.
1239
1238
1240
1239
NOTE: The `proxy-target-class` attribute controls what type of transactional proxies are
@@ -1245,9 +1244,9 @@ interface-based proxies are created. (See <<aop-proxying>> for a discussion of t
1245
1244
different proxy types.)
1246
1245
1247
1246
NOTE: `@EnableTransactionManagement` and `<tx:annotation-driven/>` looks for
1248
- `@Transactional` only on beans in the same application context in which they are defined. This
1249
- means that, if you put annotation-driven configuration in a `WebApplicationContext` for
1250
- a `DispatcherServlet`, it checks for `@Transactional` beans only in your controllers
1247
+ `@Transactional` only on beans in the same application context in which they are defined.
1248
+ This means that, if you put annotation-driven configuration in a `WebApplicationContext`
1249
+ for a `DispatcherServlet`, it checks for `@Transactional` beans only in your controllers
1251
1250
and not your services. See <<web.adoc#mvc-servlet, MVC>> for more information.
1252
1251
1253
1252
The most derived location takes precedence when evaluating the transactional settings
@@ -1279,10 +1278,10 @@ precedence over the transactional settings defined at the class level.
1279
1278
[[transaction-declarative-attransactional-settings]]
1280
1279
===== `@Transactional` Settings
1281
1280
1282
- The `@Transactional` annotation is metadata that specifies that an interface, class, or
1283
- method must have transactional semantics (for example, "`start a brand new read-only
1284
- transaction when this method is invoked, suspending any existing transaction`"). The
1285
- default `@Transactional` settings are as follows:
1281
+ The `@Transactional` annotation is metadata that specifies that an interface, class,
1282
+ or method must have transactional semantics (for example, "`start a brand new read-only
1283
+ transaction when this method is invoked, suspending any existing transaction`").
1284
+ The default `@Transactional` settings are as follows:
1286
1285
1287
1286
* The propagation setting is `PROPAGATION_REQUIRED.`
1288
1287
* The isolation level is `ISOLATION_DEFAULT.`
@@ -1291,8 +1290,8 @@ default `@Transactional` settings are as follows:
1291
1290
system, or to none if timeouts are not supported.
1292
1291
* Any `RuntimeException` triggers rollback, and any checked `Exception` does not.
1293
1292
1294
- You can change these default settings. The following table summarizes the various properties of the `@Transactional`
1295
- annotation:
1293
+ You can change these default settings. The following table summarizes the various
1294
+ properties of the `@Transactional` annotation:
1296
1295
1297
1296
[[tx-attransactional-properties]]
1298
1297
.@Transactional Settings
@@ -1350,10 +1349,10 @@ name of the transaction would be: `com.example.BusinessService.handlePayment`.
1350
1349
Most Spring applications need only a single transaction manager, but there may be
1351
1350
situations where you want multiple independent transaction managers in a single
1352
1351
application. You can use the `value` attribute of the `@Transactional` annotation to
1353
- optionally specify the identity of the `PlatformTransactionManager` to be used. This can
1354
- either be the bean name or the qualifier value of the transaction manager bean. For
1355
- example, using the qualifier notation, you can combine the following Java code with the
1356
- following transaction manager bean declarations in the application context:
1352
+ optionally specify the identity of the `PlatformTransactionManager` to be used.
1353
+ This can either be the bean name or the qualifier value of the transaction manager bean.
1354
+ For example, using the qualifier notation, you can combine the following Java code with
1355
+ the following transaction manager bean declarations in the application context:
1357
1356
1358
1357
====
1359
1358
[source,java,indent=0]
@@ -1390,18 +1389,18 @@ The following listing shows the bean declarations:
1390
1389
----
1391
1390
====
1392
1391
1393
- In this case, the two methods on `TransactionalService` run under separate
1394
- transaction managers, differentiated by the `order` and `account` qualifiers. The
1395
- default `<tx:annotation-driven>` target bean name, `transactionManager`, is still
1396
- used if no specifically qualified `PlatformTransactionManager` bean is found.
1392
+ In this case, the two methods on `TransactionalService` run under separate transaction
1393
+ managers, differentiated by the `order` and `account` qualifiers. The default
1394
+ `<tx:annotation-driven>` target bean name, `transactionManager`, is still used if no
1395
+ specifically qualified `PlatformTransactionManager` bean is found.
1397
1396
1398
1397
[[tx-custom-attributes]]
1399
1398
===== Custom Shortcut Annotations
1400
1399
1401
- If you find you repeatedly use the same attributes with `@Transactional` on many
1402
- different methods, <<core.adoc#beans-meta-annotations,Spring's meta-annotation support>> lets
1403
- you define custom shortcut annotations for your specific use cases. For example, consider
1404
- the following annotation definitions:
1400
+ If you find you repeatedly use the same attributes with `@Transactional` on many different
1401
+ methods, <<core.adoc#beans-meta-annotations,Spring's meta-annotation support>> lets you
1402
+ define custom shortcut annotations for your specific use cases. For example, consider the
1403
+ following annotation definitions:
1405
1404
1406
1405
====
1407
1406
[source,java,indent=0]
@@ -1702,10 +1701,10 @@ You can configure additional aspects in similar fashion.
1702
1701
[[transaction-declarative-aspectj]]
1703
1702
==== Using `@Transactional` with AspectJ
1704
1703
1705
- You can also use the Spring Framework's `@Transactional` support outside of a
1706
- Spring container by means of an AspectJ aspect. To do so, first annotate your
1707
- classes (and optionally your classes' methods) with the `@Transactional` annotation, and
1708
- then link (weave) your application with the
1704
+ You can also use the Spring Framework's `@Transactional` support outside of a Spring
1705
+ container by means of an AspectJ aspect. To do so, first annotate your classes
1706
+ (and optionally your classes' methods) with the `@Transactional` annotation,
1707
+ and then link (weave) your application with the
1709
1708
`org.springframework.transaction.aspectj.AnnotationTransactionAspect` defined in the
1710
1709
`spring-aspects.jar` file. You must also configure The aspect with a transaction
1711
1710
manager. You can use the Spring Framework's IoC container to take care of
@@ -5533,9 +5532,9 @@ these annotated methods. The following example shows how to do so:
5533
5532
----
5534
5533
====
5535
5534
5536
- In the container, you need to set up the `PlatformTransactionManager`
5537
- implementation (as a bean) and a `<tx:annotation-driven/>` entry,
5538
- opting into `@Transactional` processing at runtime. The following example shows how to do so:
5535
+ In the container, you need to set up the `PlatformTransactionManager` implementation
5536
+ (as a bean) and a `<tx:annotation-driven/>` entry, opting into `@Transactional`
5537
+ processing at runtime. The following example shows how to do so:
5539
5538
5540
5539
====
5541
5540
[source,xml,indent=0]
0 commit comments