2015-03-03 18:38:01 +08:00
[[spring-data-tier]]
= Data Access
2017-01-05 00:51:58 +08:00
:doc-root: https://docs.spring.io
:api-spring-framework: {doc-root}/spring-framework/docs/{spring-version}/javadoc-api/org/springframework
2017-03-29 20:20:12 +08:00
:toc: left
2017-10-06 10:23:38 +08:00
:toclevels: 4
2017-11-21 05:28:00 +08:00
:tabsize: 4
2017-09-18 18:16:47 +08:00
:docinfo1:
2015-03-03 18:38:01 +08:00
This part of the reference documentation is concerned with data access and the
interaction between the data access layer and the business or service layer.
Spring's comprehensive transaction management support is covered in some detail,
followed by thorough coverage of the various data access frameworks and technologies
2018-09-05 23:15:53 +08:00
with which the Spring Framework integrates.
2017-10-19 02:24:17 +08:00
2018-10-25 21:15:58 +08:00
2015-03-03 18:38:01 +08:00
[[transaction]]
== Transaction Management
Comprehensive transaction support is among the most compelling reasons to use the Spring
Framework. The Spring Framework provides a consistent abstraction for transaction
management that delivers the following benefits:
2018-09-05 23:15:53 +08:00
* A consistent programming model across different transaction APIs, such as Java
Transaction API (JTA), JDBC, Hibernate, and the Java Persistence API (JPA).
2019-03-05 20:08:34 +08:00
* Support for <<transaction-declarative, declarative transaction management>>.
* A simpler API for <<transaction-programmatic, programmatic>> transaction management
than complex transaction APIs, such as JTA.
2015-03-03 18:38:01 +08:00
* Excellent integration with Spring's data access abstractions.
2018-09-05 23:15:53 +08:00
The following sections describe the Spring Framework's transaction features and
technologies:
2015-03-03 18:38:01 +08:00
2019-03-05 20:08:34 +08:00
* <<transaction-motivation, Advantages of the Spring Framework's transaction support
2018-09-05 23:15:53 +08:00
model>> describes why you would use the Spring Framework's transaction abstraction
2015-03-03 18:38:01 +08:00
instead of EJB Container-Managed Transactions (CMT) or choosing to drive local
2018-09-05 23:15:53 +08:00
transactions through a proprietary API, such as Hibernate.
2019-03-05 20:08:34 +08:00
* <<transaction-strategies, Understanding the Spring Framework transaction abstraction>>
2015-03-03 18:38:01 +08:00
outlines the core classes and describes how to configure and obtain `DataSource`
instances from a variety of sources.
2019-03-05 20:08:34 +08:00
* <<tx-resource-synchronization, Synchronizing resources with transactions>> describes
2015-03-03 18:38:01 +08:00
how the application code ensures that resources are created, reused, and cleaned up
properly.
2019-03-05 20:08:34 +08:00
* <<transaction-declarative, Declarative transaction management>> describes support for
2015-03-03 18:38:01 +08:00
declarative transaction management.
2019-03-05 20:08:34 +08:00
* <<transaction-programmatic, Programmatic transaction management>> covers support for
2015-03-03 18:38:01 +08:00
programmatic (that is, explicitly coded) transaction management.
2019-03-05 20:08:34 +08:00
* <<transaction-event, Transaction bound event>> describes how you could use application
2015-05-18 22:47:48 +08:00
events within a transaction.
2015-03-03 18:38:01 +08:00
2019-03-05 20:08:34 +08:00
The chapter also includes discussions of best practices,
<<transaction-application-server-integration, application server integration>>,
and <<transaction-solutions-to-common-problems, solutions to common problems>>.
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[transaction-motivation]]
2018-09-05 23:15:53 +08:00
=== Advantages of the Spring Framework's Transaction Support Model
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Traditionally, Java EE developers have had two choices for transaction management:
2018-09-05 23:15:53 +08:00
global or local transactions, both of which have profound limitations. Global
2015-03-03 18:38:01 +08:00
and local transaction management is reviewed in the next two sections, followed by a
discussion of how the Spring Framework's transaction management support addresses the
limitations of the global and local transaction models.
[[transaction-global]]
2018-09-05 23:15:53 +08:00
==== Global Transactions
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Global transactions let you work with multiple transactional resources, typically
2015-03-03 18:38:01 +08:00
relational databases and message queues. The application server manages global
2018-09-05 23:15:53 +08:00
transactions through the JTA, which is a cumbersome API (partly due to its
2015-03-03 18:38:01 +08:00
exception model). Furthermore, a JTA `UserTransaction` normally needs to be sourced from
2018-09-05 23:15:53 +08:00
JNDI, meaning that you also need to use JNDI in order to use JTA. The use
of global transactions limits any potential reuse of application code, as JTA is
2015-03-03 18:38:01 +08:00
normally only available in an application server environment.
2018-09-05 23:15:53 +08:00
Previously, the preferred way to use global transactions was through EJB CMT
(Container Managed Transaction). CMT is a form of declarative transaction
management (as distinguished from programmatic transaction management). EJB CMT
removes the need for transaction-related JNDI lookups, although the use of EJB
2015-03-03 18:38:01 +08:00
itself necessitates the use of JNDI. It removes most but not all of the need to write
Java code to control transactions. The significant downside is that CMT is tied to JTA
and an application server environment. Also, it is only available if one chooses to
2018-09-05 23:15:53 +08:00
implement business logic in EJBs (or at least behind a transactional EJB facade). The
2015-03-03 18:38:01 +08:00
negatives of EJB in general are so great that this is not an attractive proposition,
especially in the face of compelling alternatives for declarative transaction management.
[[transaction-local]]
2018-09-05 23:15:53 +08:00
==== Local Transactions
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Local transactions are resource-specific, such as a transaction associated with a JDBC
2018-09-05 23:15:53 +08:00
connection. Local transactions may be easier to use but have a significant disadvantage:
They cannot work across multiple transactional resources. For example, code that manages
transactions by using a JDBC connection cannot run within a global JTA transaction. Because
2015-03-03 18:38:01 +08:00
the application server is not involved in transaction management, it cannot help ensure
correctness across multiple resources. (It is worth noting that most applications use a
single transaction resource.) Another downside is that local transactions are invasive
to the programming model.
[[transaction-programming-model]]
2018-09-05 23:15:53 +08:00
==== Spring Framework's Consistent Programming Model
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Spring resolves the disadvantages of global and local transactions. It lets
application developers use a consistent programming model in any environment.
2015-03-03 18:38:01 +08:00
You write your code once, and it can benefit from different transaction management
strategies in different environments. The Spring Framework provides both declarative and
programmatic transaction management. Most users prefer declarative transaction
2018-09-05 23:15:53 +08:00
management, which we recommend in most cases.
2015-03-03 18:38:01 +08:00
With programmatic transaction management, developers work with the Spring Framework
transaction abstraction, which can run over any underlying transaction infrastructure.
With the preferred declarative model, developers typically write little or no code
2018-09-05 23:15:53 +08:00
related to transaction management and, hence, do not depend on the Spring Framework
transaction API or any other transaction API.
2015-03-03 18:38:01 +08:00
.Do you need an application server for transaction management?
****
The Spring Framework's transaction management support changes traditional rules as to
when an enterprise Java application requires an application server.
2018-09-05 23:15:53 +08:00
In particular, you do not need an application server purely for declarative transactions
2015-03-03 18:38:01 +08:00
through EJBs. In fact, even if your application server has powerful JTA capabilities,
you may decide that the Spring Framework's declarative transactions offer more power and
a more productive programming model than EJB CMT.
2018-09-05 23:15:53 +08:00
Typically, you need an application server's JTA capability only if your application needs
2015-03-03 18:38:01 +08:00
to handle transactions across multiple resources, which is not a requirement for many
applications. Many high-end applications use a single, highly scalable database (such as
2018-09-05 23:15:53 +08:00
Oracle RAC) instead. Stand-alone transaction managers (such as
2019-03-21 06:48:14 +08:00
https://www.atomikos.com/[Atomikos Transactions] and http://jotm.objectweb.org/[JOTM])
2018-09-05 23:15:53 +08:00
are other options. Of course, you may need other application server capabilities, such as
2015-03-03 18:38:01 +08:00
Java Message Service (JMS) and Java EE Connector Architecture (JCA).
2018-09-05 23:15:53 +08:00
The Spring Framework gives you the choice of when to scale your application to a fully
loaded application server. Gone are the days when the only alternative to using EJB
CMT or JTA was to write code with local transactions (such as those on JDBC connections)
2015-03-03 18:38:01 +08:00
and face a hefty rework if you need that code to run within global, container-managed
transactions. With the Spring Framework, only some of the bean definitions in your
2018-09-05 23:15:53 +08:00
configuration file need to change (rather than your code).
2015-03-03 18:38:01 +08:00
****
[[transaction-strategies]]
2018-09-05 23:15:53 +08:00
=== Understanding the Spring Framework Transaction Abstraction
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The key to the Spring transaction abstraction is the notion of a transaction
strategy. A transaction strategy is defined by the
`org.springframework.transaction.PlatformTransactionManager` interface, which the following listing shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public interface PlatformTransactionManager {
2017-11-20 04:17:32 +08:00
TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
2015-03-03 18:38:01 +08:00
void commit(TransactionStatus status) throws TransactionException;
void rollback(TransactionStatus status) throws TransactionException;
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
interface PlatformTransactionManager {
@Throws(TransactionException::class)
fun getTransaction(definition: TransactionDefinition): TransactionStatus
@Throws(TransactionException::class)
fun commit(status: TransactionStatus)
@Throws(TransactionException::class)
fun rollback(status: TransactionStatus)
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
This is primarily a service provider interface (SPI), although you can use it
2019-03-05 20:08:34 +08:00
<<transaction-programmatic-ptm, programmatically>> from your application code. Because
2018-09-05 23:15:53 +08:00
`PlatformTransactionManager` is an interface, it can be easily mocked or stubbed as
necessary. It is not tied to a lookup strategy, such as JNDI.
2015-03-03 18:38:01 +08:00
`PlatformTransactionManager` implementations are defined like any other object (or bean)
in the Spring Framework IoC container. This benefit alone makes Spring Framework
2018-09-05 23:15:53 +08:00
transactions a worthwhile abstraction, even when you work with JTA. You can test transactional code
much more easily than if it used JTA directly.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Again, in keeping with Spring's philosophy, the `TransactionException` that can be thrown
by any of the `PlatformTransactionManager` interface's methods is unchecked (that
2015-03-03 18:38:01 +08:00
is, it extends the `java.lang.RuntimeException` class). Transaction infrastructure
failures are almost invariably fatal. In rare cases where application code can actually
recover from a transaction failure, the application developer can still choose to catch
and handle `TransactionException`. The salient point is that developers are not
2018-09-05 23:15:53 +08:00
_forced_ to do so.
2015-03-03 18:38:01 +08:00
The `getTransaction(..)` method returns a `TransactionStatus` object, depending on a
`TransactionDefinition` parameter. The returned `TransactionStatus` might represent a
2018-09-05 23:15:53 +08:00
new transaction or can represent an existing transaction, if a matching transaction
2015-03-03 18:38:01 +08:00
exists in the current call stack. The implication in this latter case is that, as with
2018-09-05 23:15:53 +08:00
Java EE transaction contexts, a `TransactionStatus` is associated with a thread of
2015-03-03 18:38:01 +08:00
execution.
The `TransactionDefinition` interface specifies:
2018-09-05 23:15:53 +08:00
* Propagation: Typically, all code executed within a transaction scope runs in
that transaction. However, you can specify the behavior if
a transactional method is executed when a transaction context already exists. For
example, code can continue running in the existing transaction (the common case), or
the existing transaction can be suspended and a new transaction created. Spring
offers all of the transaction propagation options familiar from EJB CMT. To read
2015-03-03 18:38:01 +08:00
about the semantics of transaction propagation in Spring, see <<tx-propagation>>.
2018-09-05 23:15:53 +08:00
* Isolation: The degree to which this transaction is isolated from the work of other
2018-02-06 05:51:43 +08:00
transactions. For example, can this transaction see uncommitted writes from other
transactions?
2018-09-05 23:15:53 +08:00
* Timeout: How long this transaction runs before timing out and being automatically rolled back
by the underlying transaction infrastructure.
* Read-only status: You can use a read-only transaction when your code reads but
2015-03-03 18:38:01 +08:00
does not modify data. Read-only transactions can be a useful optimization in some
2018-09-05 23:15:53 +08:00
cases, such as when you use Hibernate.
2015-03-03 18:38:01 +08:00
These settings reflect standard transactional concepts. If necessary, refer to resources
that discuss transaction isolation levels and other core transaction concepts.
Understanding these concepts is essential to using the Spring Framework or any
transaction management solution.
The `TransactionStatus` interface provides a simple way for transactional code to
control transaction execution and query transaction status. The concepts should be
2018-09-05 23:15:53 +08:00
familiar, as they are common to all transaction APIs. The following listing shows the
`TransactionStatus` interface:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public interface TransactionStatus extends SavepointManager {
boolean isNewTransaction();
boolean hasSavepoint();
void setRollbackOnly();
boolean isRollbackOnly();
void flush();
boolean isCompleted();
2019-09-03 22:03:28 +08:00
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
interface TransactionStatus : SavepointManager {
fun isNewTransaction(): Boolean
fun hasSavepoint(): Boolean
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
fun setRollbackOnly()
fun isRollbackOnly(): Boolean
fun flush()
fun isCompleted(): Boolean
2015-03-03 18:38:01 +08:00
}
----
Regardless of whether you opt for declarative or programmatic transaction management in
Spring, defining the correct `PlatformTransactionManager` implementation is absolutely
essential. You typically define this implementation through dependency injection.
`PlatformTransactionManager` implementations normally require knowledge of the
environment in which they work: JDBC, JTA, Hibernate, and so on. The following examples
2018-09-05 23:15:53 +08:00
show how you can define a local `PlatformTransactionManager` implementation (in this case,
with plain JDBC.)
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
You can define a JDBC `DataSource` by creating a bean similar to the following:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
----
2018-09-05 23:15:53 +08:00
The related `PlatformTransactionManager` bean definition then has a reference to
the `DataSource` definition. It should resemble the following example:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
----
2018-09-05 23:15:53 +08:00
If you use JTA in a Java EE container, then you use a container `DataSource`, obtained
through JNDI, in conjunction with Spring's `JtaTransactionManager`. The following example shows what the JTA
2015-03-03 18:38:01 +08:00
and JNDI lookup version would look like:
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/beans/spring-beans.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/jee
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/jee/spring-jee.xsd">
2015-03-03 18:38:01 +08:00
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/jpetstore"/>
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
<!-- other <bean/> definitions here -->
</beans>
----
2018-09-05 23:15:53 +08:00
The `JtaTransactionManager` does not need to know about the `DataSource` (or any other
specific resources) because it uses the container's global transaction management
2015-03-03 18:38:01 +08:00
infrastructure.
2019-03-05 20:08:34 +08:00
NOTE: The preceding definition of the `dataSource` bean uses the `<jndi-lookup/>` tag
from the `jee` namespace. For more information see
2018-09-05 23:15:53 +08:00
<<integration.adoc#xsd-schemas-jee, The JEE Schema>>.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
You can also use easily Hibernate local transactions, as shown in the following
2018-08-16 23:30:44 +08:00
examples. In this case, you need to define a Hibernate `LocalSessionFactoryBean`,
2018-09-05 23:15:53 +08:00
which your application code can use to obtain Hibernate `Session` instances.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The `DataSource` bean definition is similar to the local JDBC example shown
previously and, thus, is not shown in the following example.
2015-03-03 18:38:01 +08:00
2019-03-05 20:08:34 +08:00
NOTE: If the `DataSource` (used by any non-JTA transaction manager) is looked up through
JNDI and managed by a Java EE container, it should be non-transactional, because the
Spring Framework (rather than the Java EE container) manages the transactions.
2015-03-03 18:38:01 +08:00
The `txManager` bean in this case is of the `HibernateTransactionManager` type. In the
same way as the `DataSourceTransactionManager` needs a reference to the `DataSource`,
the `HibernateTransactionManager` needs a reference to the `SessionFactory`.
2018-09-05 23:15:53 +08:00
The following example declares `sessionFactory` and `txManager` beans:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
2015-12-18 07:27:19 +08:00
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
2015-03-03 18:38:01 +08:00
<property name="mappingResources">
<list>
<value>org/springframework/samples/petclinic/hibernate/petclinic.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
</value>
</property>
</bean>
2015-12-18 07:27:19 +08:00
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
2015-03-03 18:38:01 +08:00
</bean>
----
2018-09-05 23:15:53 +08:00
If you use Hibernate and Java EE container-managed JTA transactions, you
should use the same `JtaTransactionManager` as in the previous JTA example for
JDBC, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
----
2018-09-05 23:15:53 +08:00
NOTE: If you use JTA, your transaction manager definition should look the same, regardless
of what data access technology you use, be it JDBC, Hibernate JPA, or any other supported
2015-03-03 18:38:01 +08:00
technology. This is due to the fact that JTA transactions are global transactions, which
can enlist any transactional resource.
In all these cases, application code does not need to change. You can change how
transactions are managed merely by changing configuration, even if that change means
moving from local to global transactions or vice versa.
[[tx-resource-synchronization]]
2018-09-05 23:15:53 +08:00
=== Synchronizing Resources with Transactions
2017-10-19 02:24:17 +08:00
2018-11-05 19:27:35 +08:00
How to create different transaction managers and how they are linked to related resources
that need to be synchronized to transactions (for example `DataSourceTransactionManager`
to a JDBC `DataSource`, `HibernateTransactionManager` to a Hibernate `SessionFactory`,
and so forth) should now be clear. This section describes how the application code
(directly or indirectly, by using a persistence API such as JDBC, Hibernate, or JPA)
2015-03-03 18:38:01 +08:00
ensures that these resources are created, reused, and cleaned up properly. The section
2018-09-05 23:15:53 +08:00
also discusses how transaction synchronization is (optionally) triggered through the
2015-03-03 18:38:01 +08:00
relevant `PlatformTransactionManager`.
[[tx-resource-synchronization-high]]
2018-09-05 23:15:53 +08:00
==== High-level Synchronization Approach
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The preferred approach is to use Spring's highest-level template based persistence
integration APIs or to use native ORM APIs with transaction-aware factory beans or
2015-03-03 18:38:01 +08:00
proxies for managing the native resource factories. These transaction-aware solutions
internally handle resource creation and reuse, cleanup, optional transaction
2018-09-05 23:15:53 +08:00
synchronization of the resources, and exception mapping. Thus, user data access code does
not have to address these tasks but can focus purely on non-boilerplate
persistence logic. Generally, you use the native ORM API or take a template approach
2015-03-03 18:38:01 +08:00
for JDBC access by using the `JdbcTemplate`. These solutions are detailed in subsequent
chapters of this reference documentation.
[[tx-resource-synchronization-low]]
2018-09-05 23:15:53 +08:00
==== Low-level Synchronization Approach
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Classes such as `DataSourceUtils` (for JDBC), `EntityManagerFactoryUtils` (for JPA),
2016-07-05 05:34:48 +08:00
`SessionFactoryUtils` (for Hibernate), and so on exist at a lower level. When you want the
application code to deal directly with the resource types of the native persistence APIs,
you use these classes to ensure that proper Spring Framework-managed instances are obtained,
transactions are (optionally) synchronized, and exceptions that occur in the process are
properly mapped to a consistent API.
2015-03-03 18:38:01 +08:00
For example, in the case of JDBC, instead of the traditional JDBC approach of calling
2018-09-05 23:15:53 +08:00
the `getConnection()` method on the `DataSource`, you can instead use Spring's
`org.springframework.jdbc.datasource.DataSourceUtils` class, as follows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
Connection conn = DataSourceUtils.getConnection(dataSource);
----
If an existing transaction already has a connection synchronized (linked) to it, that
instance is returned. Otherwise, the method call triggers the creation of a new
2018-09-05 23:15:53 +08:00
connection, which is (optionally) synchronized to any existing transaction and made
available for subsequent reuse in that same transaction. As mentioned earlier, any
2015-03-03 18:38:01 +08:00
`SQLException` is wrapped in a Spring Framework `CannotGetJdbcConnectionException`, one
2018-09-05 23:15:53 +08:00
of the Spring Framework's hierarchy of unchecked `DataAccessException` types. This approach
gives you more information than can be obtained easily from the `SQLException` and
ensures portability across databases and even across different persistence technologies.
2015-03-03 18:38:01 +08:00
This approach also works without Spring transaction management (transaction
2018-09-05 23:15:53 +08:00
synchronization is optional), so you can use it whether or not you use Spring for
2015-03-03 18:38:01 +08:00
transaction management.
2018-09-05 23:15:53 +08:00
Of course, once you have used Spring's JDBC support, JPA support, or Hibernate support,
you generally prefer not to use `DataSourceUtils` or the other helper classes,
because you are much happier working through the Spring abstraction than directly
2015-03-03 18:38:01 +08:00
with the relevant APIs. For example, if you use the Spring `JdbcTemplate` or
`jdbc.object` package to simplify your use of JDBC, correct connection retrieval occurs
2018-09-05 23:15:53 +08:00
behind the scenes and you need not write any special code.
2015-03-03 18:38:01 +08:00
[[tx-resource-synchronization-tadsp]]
2018-09-05 23:15:53 +08:00
==== `TransactionAwareDataSourceProxy`
2015-03-03 18:38:01 +08:00
At the very lowest level exists the `TransactionAwareDataSourceProxy` class. This is a
proxy for a target `DataSource`, which wraps the target `DataSource` to add awareness of
Spring-managed transactions. In this respect, it is similar to a transactional JNDI
2018-09-05 23:15:53 +08:00
`DataSource`, as provided by a Java EE server.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
You should almost never need or want to use this class, except when existing
2015-03-03 18:38:01 +08:00
code must be called and passed a standard JDBC `DataSource` interface implementation. In
2018-09-05 23:15:53 +08:00
that case, it is possible that this code is usable but is participating in Spring-managed
transactions. You can write your new code by using the higher-level
abstractions mentioned earlier.
2015-03-03 18:38:01 +08:00
[[transaction-declarative]]
=== Declarative transaction management
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
NOTE: Most Spring Framework users choose declarative transaction management. This option has
the least impact on application code and, hence, is most consistent with the ideals of a
non-invasive lightweight container.
2015-03-03 18:38:01 +08:00
The Spring Framework's declarative transaction management is made possible with Spring
2018-09-05 23:15:53 +08:00
aspect-oriented programming (AOP). However, as the transactional aspects code comes
2015-03-03 18:38:01 +08:00
with the Spring Framework distribution and may be used in a boilerplate fashion, AOP
concepts do not generally have to be understood to make effective use of this code.
2018-09-05 23:15:53 +08:00
The Spring Framework's declarative transaction management is similar to EJB CMT, in that
you can specify transaction behavior (or lack of it) down to the individual method level.
You can make a `setRollbackOnly()` call within a transaction context, if
2015-03-03 18:38:01 +08:00
necessary. The differences between the two types of transaction management are:
* Unlike EJB CMT, which is tied to JTA, the Spring Framework's declarative transaction
management works in any environment. It can work with JTA transactions or local
2018-09-05 23:15:53 +08:00
transactions by using JDBC, JPA, or Hibernate by adjusting the configuration
2015-03-03 18:38:01 +08:00
files.
* You can apply the Spring Framework declarative transaction management to any class,
not merely special classes such as EJBs.
* The Spring Framework offers declarative
2019-03-05 20:08:34 +08:00
<<transaction-declarative-rolling-back, rollback rules>>, a feature with no EJB
2015-03-03 18:38:01 +08:00
equivalent. Both programmatic and declarative support for rollback rules is provided.
2018-09-05 23:15:53 +08:00
* The Spring Framework lets you customize transactional behavior by using AOP.
2015-03-03 18:38:01 +08:00
For example, you can insert custom behavior in the case of transaction rollback. You
2018-09-05 23:15:53 +08:00
can also add arbitrary advice, along with transactional advice. With EJB CMT, you
cannot influence the container's transaction management, except with
2015-03-03 18:38:01 +08:00
`setRollbackOnly()`.
* The Spring Framework does not support propagation of transaction contexts across
2018-09-05 23:15:53 +08:00
remote calls, as high-end application servers do. If you need this feature, we
2015-03-03 18:38:01 +08:00
recommend that you use EJB. However, consider carefully before using such a feature,
2018-09-05 23:15:53 +08:00
because, normally, one does not want transactions to span remote calls.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The concept of rollback rules is important. They let you specify which exceptions
(and throwables) should cause automatic rollback. You can specify this declaratively, in
2015-03-03 18:38:01 +08:00
configuration, not in Java code. So, although you can still call `setRollbackOnly()` on
the `TransactionStatus` object to roll back the current transaction back, most often you
can specify a rule that `MyApplicationException` must always result in rollback. The
significant advantage to this option is that business objects do not depend on the
transaction infrastructure. For example, they typically do not need to import Spring
transaction APIs or other Spring APIs.
Although EJB container default behavior automatically rolls back the transaction on a
2018-09-05 23:15:53 +08:00
system exception (usually a runtime exception), EJB CMT does not roll back the
transaction automatically on an application exception (that is, a checked exception
2015-03-03 18:38:01 +08:00
other than `java.rmi.RemoteException`). While the Spring default behavior for
declarative transaction management follows EJB convention (roll back is automatic only
on unchecked exceptions), it is often useful to customize this behavior.
[[tx-decl-explained]]
2018-09-05 23:15:53 +08:00
==== Understanding the Spring Framework's Declarative Transaction Implementation
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
It is not sufficient merely to tell you to annotate your classes with the
2015-03-03 18:38:01 +08:00
`@Transactional` annotation, add `@EnableTransactionManagement` to your configuration,
2018-11-05 19:27:35 +08:00
and expect you to understand how it all works. To provide a deeper understanding,
this section explains the inner workings of the Spring Framework's declarative
transaction infrastructure in the event of transaction-related issues.
2015-03-03 18:38:01 +08:00
The most important concepts to grasp with regard to the Spring Framework's declarative
transaction support are that this support is enabled
2019-03-05 20:08:34 +08:00
<<core.adoc#aop-understanding-aop-proxies, via AOP proxies>> and that the transactional
2018-11-05 19:27:35 +08:00
advice is driven by metadata (currently XML- or annotation-based). The combination of
AOP with transactional metadata yields an AOP proxy that uses a `TransactionInterceptor`
in conjunction with an appropriate `PlatformTransactionManager` implementation to drive
2018-09-05 23:15:53 +08:00
transactions around method invocations.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
NOTE: Spring AOP is covered in <<core.adoc#aop, the AOP section>>.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following images shows a Conceptual view of calling a method on a transactional proxy:
2015-03-03 18:38:01 +08:00
2017-01-05 00:51:58 +08:00
image::images/tx.png[]
2015-03-03 18:38:01 +08:00
[[transaction-declarative-first-example]]
2018-09-05 23:15:53 +08:00
==== Example of Declarative Transaction Implementation
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Consider the following interface and its attendant implementation. This example uses
2015-03-03 18:38:01 +08:00
`Foo` and `Bar` classes as placeholders so that you can concentrate on the transaction
usage without focusing on a particular domain model. For the purposes of this example,
the fact that the `DefaultFooService` class throws `UnsupportedOperationException`
2018-09-05 23:15:53 +08:00
instances in the body of each implemented method is good. That behavior lets you see
transactions be created and then rolled back in response to the
`UnsupportedOperationException` instance. The following listing shows the `FooService` interface:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
// the service interface that we want to make transactional
package x.y.service;
public interface FooService {
Foo getFoo(String fooName);
Foo getFoo(String fooName, String barName);
void insertFoo(Foo foo);
void updateFoo(Foo foo);
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
// the service interface that we want to make transactional
package x.y.service
interface FooService {
fun getFoo(fooName: String): Foo
fun getFoo(fooName: String, barName: String): Foo
fun insertFoo(foo: Foo)
fun updateFoo(foo: Foo)
}
----
2018-09-05 23:15:53 +08:00
2018-11-19 11:43:58 +08:00
The following example shows an implementation of the preceding interface:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
package x.y.service;
public class DefaultFooService implements FooService {
2019-09-03 22:03:28 +08:00
@Override
2015-03-03 18:38:01 +08:00
public Foo getFoo(String fooName) {
2019-09-03 22:03:28 +08:00
// ...
2015-03-03 18:38:01 +08:00
}
2019-09-03 22:03:28 +08:00
@Override
2015-03-03 18:38:01 +08:00
public Foo getFoo(String fooName, String barName) {
2019-09-03 22:03:28 +08:00
// ...
2015-03-03 18:38:01 +08:00
}
2019-09-03 22:03:28 +08:00
@Override
2015-03-03 18:38:01 +08:00
public void insertFoo(Foo foo) {
2019-09-03 22:03:28 +08:00
// ...
2015-03-03 18:38:01 +08:00
}
2019-09-03 22:03:28 +08:00
@Override
2015-03-03 18:38:01 +08:00
public void updateFoo(Foo foo) {
2019-09-03 22:03:28 +08:00
// ...
}
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
package x.y.service
class DefaultFooService : FooService {
override fun getFoo(fooName: String): Foo {
// ...
}
override fun getFoo(fooName: String, barName: String): Foo {
// ...
2015-03-03 18:38:01 +08:00
}
2019-09-03 22:03:28 +08:00
override fun insertFoo(foo: Foo) {
// ...
}
override fun updateFoo(foo: Foo) {
// ...
}
2015-03-03 18:38:01 +08:00
}
----
Assume that the first two methods of the `FooService` interface, `getFoo(String)` and
`getFoo(String, String)`, must execute in the context of a transaction with read-only
semantics, and that the other methods, `insertFoo(Foo)` and `updateFoo(Foo)`, must
execute in the context of a transaction with read-write semantics. The following
2018-09-05 23:15:53 +08:00
configuration is explained in detail in the next few paragraphs:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim"]
2015-03-03 18:38:01 +08:00
----
<!-- from the file 'context.xml' -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/beans/spring-beans.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/tx
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/tx/spring-tx.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/aop
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/aop/spring-aop.xsd">
2015-03-03 18:38:01 +08:00
<!-- this is the service object that we want to make transactional -->
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="get*" read-only="true"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- ensure that the above transactional advice runs for any execution
of an operation defined by the FooService interface -->
<aop:config>
<aop:pointcut id="fooServiceOperation" expression="execution(* x.y.service.FooService.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
</aop:config>
<!-- don't forget the DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!-- similarly, don't forget the PlatformTransactionManager -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- other <bean/> definitions here -->
</beans>
----
2018-09-05 23:15:53 +08:00
Examine the preceding configuration. It assumes that you want to make a service object, the `fooService`
2015-03-03 18:38:01 +08:00
bean, transactional. The transaction semantics to apply are encapsulated in the
2018-09-05 23:15:53 +08:00
`<tx:advice/>` definition. The `<tx:advice/>` definition reads as "`all methods, on
starting with `get`, are to execute in the context of a read-only transaction, and all
other methods are to execute with the default transaction semantics`". The
2015-03-03 18:38:01 +08:00
`transaction-manager` attribute of the `<tx:advice/>` tag is set to the name of the
2018-09-05 23:15:53 +08:00
`PlatformTransactionManager` bean that is going to drive the transactions (in this
case, the `txManager` bean).
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
TIP: You can omit the `transaction-manager` attribute in the transactional advice
2016-12-09 01:24:12 +08:00
(`<tx:advice/>`) if the bean name of the `PlatformTransactionManager` that you want to
2015-03-03 18:38:01 +08:00
wire in has the name `transactionManager`. If the `PlatformTransactionManager` bean that
2018-09-05 23:15:53 +08:00
you want to wire in has any other name, you must use the `transaction-manager`
2015-03-03 18:38:01 +08:00
attribute explicitly, as in the preceding example.
The `<aop:config/>` definition ensures that the transactional advice defined by the
2018-09-05 23:15:53 +08:00
`txAdvice` bean executes at the appropriate points in the program. First, you define a
2019-03-05 20:08:34 +08:00
pointcut that matches the execution of any operation defined in the `FooService` interface
(`fooServiceOperation`). Then you associate the pointcut with the `txAdvice` by using an
advisor. The result indicates that, at the execution of a `fooServiceOperation`,
2018-09-05 23:15:53 +08:00
the advice defined by `txAdvice` is run.
2015-03-03 18:38:01 +08:00
The expression defined within the `<aop:pointcut/>` element is an AspectJ pointcut
2019-03-05 20:08:34 +08:00
expression. See <<core.adoc#aop, the AOP section>> for more details on pointcut
expressions in Spring.
2015-03-03 18:38:01 +08:00
A common requirement is to make an entire service layer transactional. The best way to
2018-09-05 23:15:53 +08:00
do this is to change the pointcut expression to match any operation in your
service layer. The following example shows how to do so:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim"]
2015-03-03 18:38:01 +08:00
----
<aop:config>
<aop:pointcut id="fooServiceMethods" expression="execution(* x.y.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods"/>
</aop:config>
----
2019-03-05 20:08:34 +08:00
NOTE: In the preceding example, it is assumed that all your service interfaces are defined
in the `x.y.service` package. See <<core.adoc#aop, the AOP section>> for more details.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Now that we have analyzed the configuration, you may be asking yourself,
"`What does all this configuration actually do?`"
The configuration shown earlier is used to create a transactional proxy around the object
that is created from the `fooService` bean definition. The proxy is configured with
2019-03-05 20:08:34 +08:00
the transactional advice so that, when an appropriate method is invoked on the proxy,
a transaction is started, suspended, marked as read-only, and so on, depending on the
transaction configuration associated with that method. Consider the following program
that test drives the configuration shown earlier:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public final class Boot {
public static void main(final String[] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml", Boot.class);
FooService fooService = (FooService) ctx.getBean("fooService");
fooService.insertFoo (new Foo());
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
import org.springframework.beans.factory.getBean
fun main() {
val ctx = ClassPathXmlApplicationContext("context.xml")
val fooService = ctx.getBean<FooService>("fooService")
fooService.insertFoo(Foo())
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The output from running the preceding program should resemble the following (the Log4J
2015-03-03 18:38:01 +08:00
output and the stack trace from the UnsupportedOperationException thrown by the
2018-09-05 23:15:53 +08:00
insertFoo(..) method of the DefaultFooService class have been truncated for clarity):
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<!-- the Spring container is starting up... -->
[AspectJInvocationContextExposingAdvisorAutoProxyCreator] - Creating implicit proxy for bean 'fooService' with 0 common interceptors and 1 specific interceptors
<!-- the DefaultFooService is actually proxied -->
[JdkDynamicAopProxy] - Creating JDK dynamic proxy for [x.y.service.DefaultFooService]
<!-- ... the insertFoo(..) method is now being invoked on the proxy -->
[TransactionInterceptor] - Getting transaction for x.y.service.FooService.insertFoo
<!-- the transactional advice kicks in here... -->
[DataSourceTransactionManager] - Creating new transaction with name [x.y.service.FooService.insertFoo]
[DataSourceTransactionManager] - Acquired Connection [org.apache.commons.dbcp.PoolableConnection@a53de4] for JDBC transaction
<!-- the insertFoo(..) method from DefaultFooService throws an exception... -->
[RuleBasedTransactionAttribute] - Applying rules to determine whether transaction should rollback on java.lang.UnsupportedOperationException
[TransactionInterceptor] - Invoking rollback for transaction on x.y.service.FooService.insertFoo due to throwable [java.lang.UnsupportedOperationException]
<!-- and the transaction is rolled back (by default, RuntimeException instances cause rollback) -->
[DataSourceTransactionManager] - Rolling back JDBC transaction on Connection [org.apache.commons.dbcp.PoolableConnection@a53de4]
[DataSourceTransactionManager] - Releasing JDBC Connection after transaction
[DataSourceUtils] - Returning JDBC Connection to DataSource
Exception in thread "main" java.lang.UnsupportedOperationException at x.y.service.DefaultFooService.insertFoo(DefaultFooService.java:14)
<!-- AOP infrastructure stack trace elements removed for clarity -->
at $Proxy0.insertFoo(Unknown Source)
at Boot.main(Boot.java:11)
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[transaction-declarative-rolling-back]]
2018-09-05 23:15:53 +08:00
==== Rolling Back a Declarative Transaction
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
The previous section outlined the basics of how to specify transactional settings for
classes, typically service layer classes, declaratively in your application. This
2018-09-05 23:15:53 +08:00
section describes how you can control the rollback of transactions in a simple,
2015-03-03 18:38:01 +08:00
declarative fashion.
The recommended way to indicate to the Spring Framework's transaction infrastructure
that a transaction's work is to be rolled back is to throw an `Exception` from code that
is currently executing in the context of a transaction. The Spring Framework's
2018-09-05 23:15:53 +08:00
transaction infrastructure code catches any unhandled `Exception` as it bubbles up
the call stack and makes a determination whether to mark the transaction for rollback.
2015-03-03 18:38:01 +08:00
In its default configuration, the Spring Framework's transaction infrastructure code
2018-09-05 23:15:53 +08:00
marks a transaction for rollback only in the case of runtime, unchecked exceptions.
That is, when the thrown exception is an instance or subclass of `RuntimeException`. (
`Error` instances also, by default, result in a rollback). Checked exceptions that are
thrown from a transactional method do not result in rollback in the default
2015-03-03 18:38:01 +08:00
configuration.
You can configure exactly which `Exception` types mark a transaction for rollback,
including checked exceptions. The following XML snippet demonstrates how you configure
2018-09-05 23:15:53 +08:00
rollback for a checked, application-specific `Exception` type:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" rollback-for="NoProductInStockException"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
----
2018-09-05 23:15:53 +08:00
If you do not want a transaction rolled
back when an exception is thrown, you can also specify 'no rollback rules'. The following example tells the Spring Framework's
2015-03-03 18:38:01 +08:00
transaction infrastructure to commit the attendant transaction even in the face of an
2018-09-05 23:15:53 +08:00
unhandled `InstrumentNotFoundException`:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="updateStock" no-rollback-for="InstrumentNotFoundException"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
----
2018-02-01 04:06:43 +08:00
When the Spring Framework's transaction infrastructure catches an exception and it
2018-09-05 23:15:53 +08:00
consults the configured rollback rules to determine whether to mark the transaction for
rollback, the strongest matching rule wins. So, in the case of the following
2015-03-03 18:38:01 +08:00
configuration, any exception other than an `InstrumentNotFoundException` results in a
2018-09-05 23:15:53 +08:00
rollback of the attendant transaction:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*" rollback-for="Throwable" no-rollback-for="InstrumentNotFoundException"/>
</tx:attributes>
</tx:advice>
----
2018-09-05 23:15:53 +08:00
You can also indicate a required rollback programmatically. Although simple,
this process is quite invasive and tightly couples your code to the Spring Framework's
transaction infrastructure. The following example shows how to programmatically indicate
a required rollback:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public void resolvePosition() {
try {
// some business logic...
} catch (NoProductInStockException ex) {
// trigger rollback programmatically
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
fun resolvePosition() {
try {
// some business logic...
} catch (ex: NoProductInStockException) {
// trigger rollback programmatically
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
You are strongly encouraged to use the declarative approach to rollback, if at all
2015-03-03 18:38:01 +08:00
possible. Programmatic rollback is available should you absolutely need it, but its
usage flies in the face of achieving a clean POJO-based architecture.
[[transaction-declarative-diff-tx]]
2018-09-05 23:15:53 +08:00
==== Configuring Different Transactional Semantics for Different Beans
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Consider the scenario where you have a number of service layer objects, and you want to
2018-09-05 23:15:53 +08:00
apply a totally different transactional configuration to each of them. You can do so
2015-03-03 18:38:01 +08:00
by defining distinct `<aop:advisor/>` elements with differing `pointcut` and
`advice-ref` attribute values.
As a point of comparison, first assume that all of your service layer classes are
defined in a root `x.y.service` package. To make all beans that are instances of classes
defined in that package (or in subpackages) and that have names ending in `Service` have
2018-09-05 23:15:53 +08:00
the default transactional configuration, you could write the following:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim"]
2015-03-03 18:38:01 +08:00
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/beans/spring-beans.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/tx
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/tx/spring-tx.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/aop
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/aop/spring-aop.xsd">
2015-03-03 18:38:01 +08:00
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* x.y.service..*Service.*(..))"/>
<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
</aop:config>
<!-- these two beans will be transactional... -->
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<bean id="barService" class="x.y.service.extras.SimpleBarService"/>
<!-- ... and these two beans won't -->
<bean id="anotherService" class="org.xyz.SomeService"/> <!-- (not in the right package) -->
<bean id="barManager" class="x.y.service.SimpleBarManager"/> <!-- (doesn't end in 'Service') -->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- other transaction infrastructure beans such as a PlatformTransactionManager omitted... -->
</beans>
----
The following example shows how to configure two distinct beans with totally different
2018-09-05 23:15:53 +08:00
transactional settings:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim"]
2015-03-03 18:38:01 +08:00
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/beans/spring-beans.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/tx
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/tx/spring-tx.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/aop
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/aop/spring-aop.xsd">
2015-03-03 18:38:01 +08:00
<aop:config>
<aop:pointcut id="defaultServiceOperation"
expression="execution(* x.y.service.*Service.*(..))"/>
<aop:pointcut id="noTxServiceOperation"
expression="execution(* x.y.service.ddl.DefaultDdlManager.*(..))"/>
<aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice"/>
<aop:advisor pointcut-ref="noTxServiceOperation" advice-ref="noTxAdvice"/>
</aop:config>
<!-- this bean will be transactional (see the 'defaultServiceOperation' pointcut) -->
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<!-- this bean will also be transactional, but with totally different transactional settings -->
<bean id="anotherFooService" class="x.y.service.ddl.DefaultDdlManager"/>
<tx:advice id="defaultTxAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<tx:advice id="noTxAdvice">
<tx:attributes>
<tx:method name="*" propagation="NEVER"/>
</tx:attributes>
</tx:advice>
<!-- other transaction infrastructure beans such as a PlatformTransactionManager omitted... -->
</beans>
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[transaction-declarative-txadvice-settings]]
2018-09-05 23:15:53 +08:00
==== <tx:advice/> Settings
2015-03-03 18:38:01 +08:00
2018-11-19 11:43:58 +08:00
This section summarizes the various transactional settings that you can specify by using
2015-03-03 18:38:01 +08:00
the `<tx:advice/>` tag. The default `<tx:advice/>` settings are:
2019-03-05 20:08:34 +08:00
* The <<tx-propagation, propagation setting>> is `REQUIRED.`
2018-09-05 23:15:53 +08:00
* The isolation level is `DEFAULT.`
* The transaction is read-write.
* The transaction timeout defaults to the default timeout of the underlying transaction
system or none if timeouts are not supported.
2015-03-03 18:38:01 +08:00
* Any `RuntimeException` triggers rollback, and any checked `Exception` does not.
2018-09-05 23:15:53 +08:00
You can change these default settings. The following table summarizes the various attributes of the `<tx:method/>` tags
that are nested within `<tx:advice/>` and `<tx:attributes/>` tags:
2015-03-03 18:38:01 +08:00
[[tx-method-settings]]
.<tx:method/> settings
|===
| Attribute| Required?| Default| Description
| `name`
| Yes
|
2018-09-05 23:15:53 +08:00
| Method names with which the transaction attributes are to be associated. The
2015-03-03 18:38:01 +08:00
wildcard ({asterisk}) character can be used to associate the same transaction attribute
2018-09-05 23:15:53 +08:00
settings with a number of methods (for example, `get*`, `handle*`, `on*Event`, and so
forth).
2015-03-03 18:38:01 +08:00
| `propagation`
| No
2018-09-05 23:15:53 +08:00
| `REQUIRED`
2015-03-03 18:38:01 +08:00
| Transaction propagation behavior.
| `isolation`
| No
2018-09-05 23:15:53 +08:00
| `DEFAULT`
| Transaction isolation level. Only applicable to propagation settings of `REQUIRED` or `REQUIRES_NEW`.
2015-03-03 18:38:01 +08:00
| `timeout`
| No
| -1
2018-09-05 23:15:53 +08:00
| Transaction timeout (seconds). Only applicable to propagation `REQUIRED` or `REQUIRES_NEW`.
2015-03-03 18:38:01 +08:00
| `read-only`
| No
| false
2018-09-05 23:15:53 +08:00
| Read-write versus read-only transaction. Applies only to `REQUIRED` or `REQUIRES_NEW`.
2015-03-03 18:38:01 +08:00
| `rollback-for`
| No
|
2018-09-05 23:15:53 +08:00
| Comma-delimited list of `Exception` instances that trigger rollback. For example,
2019-11-30 01:41:59 +08:00
`com.foo.MyBusinessException,ServletException`.
2015-03-03 18:38:01 +08:00
| `no-rollback-for`
| No
|
2018-09-05 23:15:53 +08:00
| Comma-delimited list of `Exception` instances that do not trigger rollback. For example,
2019-11-30 01:41:59 +08:00
`com.foo.MyBusinessException,ServletException`.
2015-03-03 18:38:01 +08:00
|===
[[transaction-declarative-annotations]]
2018-09-05 23:15:53 +08:00
==== Using `@Transactional`
2015-03-03 18:38:01 +08:00
In addition to the XML-based declarative approach to transaction configuration, you can
use an annotation-based approach. Declaring transaction semantics directly in the Java
source code puts the declarations much closer to the affected code. There is not much
danger of undue coupling, because code that is meant to be used transactionally is
almost always deployed that way anyway.
2018-11-05 19:27:35 +08:00
NOTE: The standard `javax.transaction.Transactional` annotation is also supported as a
drop-in replacement to Spring's own annotation. Please refer to JTA 1.2 documentation
for more details.
2015-03-03 18:38:01 +08:00
The ease-of-use afforded by the use of the `@Transactional` annotation is best
2018-11-05 19:27:35 +08:00
illustrated with an example, which is explained in the text that follows.
Consider the following class definition:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
// the service class that we want to make transactional
2019-03-13 22:12:51 +08:00
@Transactional
2015-03-03 18:38:01 +08:00
public class DefaultFooService implements FooService {
2019-09-03 22:03:28 +08:00
Foo getFoo(String fooName) {
// ...
}
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
Foo getFoo(String fooName, String barName) {
// ...
}
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
void insertFoo(Foo foo) {
// ...
}
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
void updateFoo(Foo foo) {
// ...
}
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
// the service class that we want to make transactional
@Transactional
class DefaultFooService : FooService {
override fun getFoo(fooName: String): Foo {
// ...
}
override fun getFoo(fooName: String, barName: String): Foo {
// ...
}
override fun insertFoo(foo: Foo) {
// ...
}
override fun updateFoo(foo: Foo) {
// ...
}
2015-03-03 18:38:01 +08:00
}
----
2018-11-06 02:32:34 +08:00
Used at the class level as above, the annotation indicates a default for all methods
of the declaring class (as well as its subclasses). Alternatively, each method can
get annotated individually. Note that a class-level annotation does not apply to
ancestor classes up the class hierarchy; in such a scenario, methods need to be
locally redeclared in order to participate in a subclass-level annotation.
When a POJO class such as the one above is defined as a bean in a Spring context,
you can make the bean instance transactional through an `@EnableTransactionManagement`
annotation in a `@Configuration` class. See the
{api-spring-framework}/transaction/annotation/EnableTransactionManagement.html[javadoc]
for full details.
In XML configuration, the `<tx:annotation-driven/>` tag provides similar convenience:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<!-- from the file 'context.xml' -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/beans/spring-beans.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/tx
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/tx/spring-tx.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/aop
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/aop/spring-aop.xsd">
2015-03-03 18:38:01 +08:00
<!-- this is the service object that we want to make transactional -->
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<!-- enable the configuration of transactional behavior based on annotations -->
2018-09-05 23:15:53 +08:00
<tx:annotation-driven transaction-manager="txManager"/><!-- a PlatformTransactionManager is still required --> <1>
2018-11-06 02:32:34 +08:00
2015-03-03 18:38:01 +08:00
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (this dependency is defined somewhere else) -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- other <bean/> definitions here -->
</beans>
----
2018-09-05 23:15:53 +08:00
<1> The line that makes the bean instance transactional.
2018-11-27 06:15:55 +08:00
2018-09-05 23:15:53 +08:00
2018-10-25 21:15:58 +08:00
TIP: You can omit the `transaction-manager` attribute in the `<tx:annotation-driven/>` tag
if the bean name of the `PlatformTransactionManager` that you want to wire in has the name,
2015-03-03 18:38:01 +08:00
`transactionManager`. If the `PlatformTransactionManager` bean that you want to
2018-10-25 21:15:58 +08:00
dependency-inject has any other name, you have to use the `transaction-manager` attribute,
as in the preceding example.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
.Method visibility and `@Transactional`
2015-03-03 18:38:01 +08:00
****
2018-09-05 23:15:53 +08:00
When you use proxies, you should apply the `@Transactional` annotation only to methods
with public visibility. If you do annotate protected, private or package-visible
2015-03-03 18:38:01 +08:00
methods with the `@Transactional` annotation, no error is raised, but the annotated
2018-11-05 19:27:35 +08:00
method does not exhibit the configured transactional settings. If you need to annotate
non-public methods, consider using AspectJ (described later).
2015-03-03 18:38:01 +08:00
****
2018-11-06 02:32:34 +08:00
You can apply the `@Transactional` annotation to an interface definition, a method
2018-09-05 23:15:53 +08:00
on an interface, a class definition, or a public method on a class. However, the
2015-03-03 18:38:01 +08:00
mere presence of the `@Transactional` annotation is not enough to activate the
2018-11-06 02:32:34 +08:00
transactional behavior. The `@Transactional` annotation is merely metadata that can
be consumed by some runtime infrastructure that is `@Transactional`-aware and that
can use the metadata to configure the appropriate beans with transactional behavior.
In the preceding example, the `<tx:annotation-driven/>` element switches on the
2015-03-03 18:38:01 +08:00
transactional behavior.
2018-11-05 19:27:35 +08:00
TIP: The Spring team recommends that you annotate only concrete classes (and methods of
concrete classes) with the `@Transactional` annotation, as opposed to annotating interfaces.
You certainly can place the `@Transactional` annotation on an interface (or an interface
2018-09-05 23:15:53 +08:00
method), but this works only as you would expect it to if you use interface-based
proxies. The fact that Java annotations are not inherited from interfaces means that,
if you use class-based proxies (`proxy-target-class="true"`) or the weaving-based
2018-11-06 02:32:34 +08:00
aspect (`mode="aspectj"`), the transaction settings are not recognized by the proxying
and weaving infrastructure, and the object is not wrapped in a transactional proxy.
2018-09-05 23:15:53 +08:00
2018-11-05 19:27:35 +08:00
NOTE: In proxy mode (which is the default), only external method calls coming in through
the proxy are intercepted. This means that self-invocation (in effect, a method within
the target object calling another method of the target object) does not lead to an actual
2015-03-03 18:38:01 +08:00
transaction at runtime even if the invoked method is marked with `@Transactional`. Also,
2018-09-05 23:15:53 +08:00
the proxy must be fully initialized to provide the expected behavior, so you should not
rely on this feature in your initialization code (that is, `@PostConstruct`).
2015-03-03 18:38:01 +08:00
2018-11-05 19:27:35 +08:00
Consider using of AspectJ mode (see the `mode` attribute in the following table) if you
expect self-invocations to be wrapped with transactions as well. In this case, there no
proxy in the first place. Instead, the target class is woven (that is, its byte code is
modified) to turn `@Transactional` into runtime behavior on any kind of method.
2015-03-03 18:38:01 +08:00
[[tx-annotation-driven-settings]]
.Annotation driven transaction settings
|===
| XML Attribute| Annotation Attribute| Default| Description
| `transaction-manager`
2018-10-25 21:15:58 +08:00
| N/A (see {api-spring-framework}/transaction/annotation/TransactionManagementConfigurer.html[`TransactionManagementConfigurer`] javadoc)
2018-09-05 23:15:53 +08:00
| `transactionManager`
| Name of the transaction manager to use. Required only if the name of the transaction
manager is not `transactionManager`, as in the preceding example.
2015-03-03 18:38:01 +08:00
| `mode`
| `mode`
2018-09-05 23:15:53 +08:00
| `proxy`
| The default mode (`proxy`) processes annotated beans to be proxied by using Spring's AOP
framework (following proxy semantics, as discussed earlier, applying to method calls
coming in through the proxy only). The alternative mode (`aspectj`) instead weaves the
2015-03-03 18:38:01 +08:00
affected classes with Spring's AspectJ transaction aspect, modifying the target class
byte code to apply to any kind of method call. AspectJ weaving requires
2018-09-05 23:15:53 +08:00
`spring-aspects.jar` in the classpath as well as having load-time weaving (or compile-time
2017-03-29 20:20:12 +08:00
weaving) enabled. (See <<core.adoc#aop-aj-ltw-spring, Spring configuration>>
for details on how to set up load-time weaving.)
2015-03-03 18:38:01 +08:00
| `proxy-target-class`
| `proxyTargetClass`
2018-09-05 23:15:53 +08:00
| `false`
| Applies to `proxy` mode only. Controls what type of transactional proxies are created
2015-03-03 18:38:01 +08:00
for classes annotated with the `@Transactional` annotation. If the
2018-09-05 23:15:53 +08:00
`proxy-target-class` attribute is set to `true`, class-based proxies are created.
2015-03-03 18:38:01 +08:00
If `proxy-target-class` is `false` or if the attribute is omitted, then standard JDK
2019-03-05 20:08:34 +08:00
interface-based proxies are created. (See <<core.adoc#aop-proxying, Proxying Mechanisms>>
2017-03-29 20:20:12 +08:00
for a detailed examination of the different proxy types.)
2015-03-03 18:38:01 +08:00
| `order`
| `order`
2018-09-05 23:15:53 +08:00
| `Ordered.LOWEST_PRECEDENCE`
2015-03-03 18:38:01 +08:00
| Defines the order of the transaction advice that is applied to beans annotated with
`@Transactional`. (For more information about the rules related to ordering of AOP
2019-03-05 20:08:34 +08:00
advice, see <<core.adoc#aop-ataspectj-advice-ordering, Advice Ordering>>.)
2017-03-29 20:20:12 +08:00
No specified ordering means that the AOP subsystem determines the order of the advice.
2015-03-03 18:38:01 +08:00
|===
2018-11-05 19:27:35 +08:00
NOTE: The default advice mode for processing `@Transactional` annotations is `proxy`,
which allows for interception of calls through the proxy only. Local calls within the
same class cannot get intercepted that way. For a more advanced mode of interception,
2018-09-05 23:15:53 +08:00
consider switching to `aspectj` mode in combination with compile-time or load-time weaving.
2017-10-23 02:34:34 +08:00
2018-09-05 23:15:53 +08:00
NOTE: The `proxy-target-class` attribute controls what type of transactional proxies are
2015-03-03 18:38:01 +08:00
created for classes annotated with the `@Transactional` annotation. If
`proxy-target-class` is set to `true`, class-based proxies are created. If
`proxy-target-class` is `false` or if the attribute is omitted, standard JDK
2020-01-24 04:01:58 +08:00
interface-based proxies are created. (See <<core.adoc#aop-proxying>> for a discussion of the
2015-03-03 18:38:01 +08:00
different proxy types.)
2018-09-05 23:15:53 +08:00
NOTE: `@EnableTransactionManagement` and `<tx:annotation-driven/>` looks for
2018-11-05 19:27:35 +08:00
`@Transactional` only on beans in the same application context in which they are defined.
This means that, if you put annotation-driven configuration in a `WebApplicationContext`
for a `DispatcherServlet`, it checks for `@Transactional` beans only in your controllers
2017-03-29 20:20:12 +08:00
and not your services. See <<web.adoc#mvc-servlet, MVC>> for more information.
2015-03-03 18:38:01 +08:00
The most derived location takes precedence when evaluating the transactional settings
for a method. In the case of the following example, the `DefaultFooService` class is
annotated at the class level with the settings for a read-only transaction, but the
`@Transactional` annotation on the `updateFoo(Foo)` method in the same class takes
precedence over the transactional settings defined at the class level.
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
@Transactional(readOnly = true)
public class DefaultFooService implements FooService {
public Foo getFoo(String fooName) {
2019-09-03 22:03:28 +08:00
// ...
2015-03-03 18:38:01 +08:00
}
// these settings have precedence for this method
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void updateFoo(Foo foo) {
2019-09-03 22:03:28 +08:00
// ...
}
}
----
[source,kotlin,indent=0,subs="verbatim",role="secondary"]
.Kotlin
----
@Transactional(readOnly = true)
class DefaultFooService : FooService {
override fun getFoo(fooName: String): Foo {
// ...
}
// these settings have precedence for this method
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
override fun updateFoo(foo: Foo) {
// ...
2015-03-03 18:38:01 +08:00
}
}
----
2018-09-05 23:15:53 +08:00
2019-09-03 22:03:28 +08:00
2015-03-03 18:38:01 +08:00
[[transaction-declarative-attransactional-settings]]
2018-09-05 23:15:53 +08:00
===== `@Transactional` Settings
2015-03-03 18:38:01 +08:00
2018-11-05 19:27:35 +08:00
The `@Transactional` annotation is metadata that specifies that an interface, class,
or method must have transactional semantics (for example, "`start a brand new read-only
transaction when this method is invoked, suspending any existing transaction`").
The default `@Transactional` settings are as follows:
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
* The propagation setting is `PROPAGATION_REQUIRED.`
* The isolation level is `ISOLATION_DEFAULT.`
* The transaction is read-write.
* The transaction timeout defaults to the default timeout of the underlying transaction
2015-03-03 18:38:01 +08:00
system, or to none if timeouts are not supported.
* Any `RuntimeException` triggers rollback, and any checked `Exception` does not.
2018-11-05 19:27:35 +08:00
You can change these default settings. The following table summarizes the various
properties of the `@Transactional` annotation:
2015-03-03 18:38:01 +08:00
[[tx-attransactional-properties]]
2016-03-03 16:35:40 +08:00
.@Transactional Settings
2015-03-03 18:38:01 +08:00
|===
| Property| Type| Description
| <<tx-multiple-tx-mgrs-with-attransactional,value>>
2018-09-05 23:15:53 +08:00
| `String`
| Optional qualifier that specifies the transaction manager to be used.
2015-03-03 18:38:01 +08:00
| <<tx-propagation,propagation>>
2018-09-05 23:15:53 +08:00
| `enum`: `Propagation`
2015-03-03 18:38:01 +08:00
| Optional propagation setting.
| `isolation`
2018-09-05 23:15:53 +08:00
| `enum`: `Isolation`
2018-11-19 11:43:58 +08:00
| Optional isolation level. Applies only to propagation values of `REQUIRED` or `REQUIRES_NEW`.
2015-03-03 18:38:01 +08:00
| `timeout`
2018-09-05 23:15:53 +08:00
| `int` (in seconds of granularity)
2018-11-19 11:43:58 +08:00
| Optional transaction timeout. Applies only to propagation values of `REQUIRED` or `REQUIRES_NEW`.
2018-02-06 05:51:43 +08:00
| `readOnly`
2018-09-05 23:15:53 +08:00
| `boolean`
2018-11-19 11:43:58 +08:00
| Read-write versus read-only transaction. Only applicable to values of `REQUIRED` or `REQUIRES_NEW`.
2015-03-03 18:38:01 +08:00
| `rollbackFor`
| Array of `Class` objects, which must be derived from `Throwable.`
2018-09-05 23:15:53 +08:00
| Optional array of exception classes that must cause rollback.
2015-03-03 18:38:01 +08:00
| `rollbackForClassName`
2018-09-05 23:15:53 +08:00
| Array of class names. The classes must be derived from `Throwable.`
| Optional array of names of exception classes that must cause rollback.
2015-03-03 18:38:01 +08:00
| `noRollbackFor`
| Array of `Class` objects, which must be derived from `Throwable.`
2018-09-05 23:15:53 +08:00
| Optional array of exception classes that must not cause rollback.
2015-03-03 18:38:01 +08:00
| `noRollbackForClassName`
| Array of `String` class names, which must be derived from `Throwable.`
2018-09-05 23:15:53 +08:00
| Optional array of names of exception classes that must not cause rollback.
2020-04-09 20:55:18 +08:00
| `label`
| Array of `String` labels to add an expressive description to the transaction.
| Labels may be evaluated by transaction managers to associate
implementation-specific behavior with the actual transaction.
2015-03-03 18:38:01 +08:00
|===
2018-09-05 23:15:53 +08:00
Currently, you cannot have explicit control over the name of a transaction, where 'name'
means the transaction name that appears in a transaction monitor, if applicable
2015-03-03 18:38:01 +08:00
(for example, WebLogic's transaction monitor), and in logging output. For declarative
2018-09-05 23:15:53 +08:00
transactions, the transaction name is always the fully-qualified class name + `.`
+ the method name of the transactionally advised class. For example, if the
2015-03-03 18:38:01 +08:00
`handlePayment(..)` method of the `BusinessService` class started a transaction, the
2018-09-05 23:15:53 +08:00
name of the transaction would be: `com.example.BusinessService.handlePayment`.
2015-03-03 18:38:01 +08:00
[[tx-multiple-tx-mgrs-with-attransactional]]
2018-09-05 23:15:53 +08:00
===== Multiple Transaction Managers with `@Transactional`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Most Spring applications need only a single transaction manager, but there may be
2015-03-03 18:38:01 +08:00
situations where you want multiple independent transaction managers in a single
2020-05-08 00:42:02 +08:00
application. You can use the `value` or `transactionManager` attribute of the
`@Transactional` annotation to optionally specify the identity of the
`PlatformTransactionManager` to be used. This can either be the bean name or the
qualifier value of the transaction manager bean. For example, using the qualifier
notation, you can combine the following Java code with the following transaction manager
bean declarations in the application context:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class TransactionalService {
2016-08-26 19:57:18 +08:00
@Transactional("order")
2015-03-03 18:38:01 +08:00
public void setSomething(String name) { ... }
2016-08-26 19:57:18 +08:00
@Transactional("account")
2015-03-03 18:38:01 +08:00
public void doSomething() { ... }
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim",role="secondary"]
.Kotlin
----
class TransactionalService {
@Transactional("order")
fun setSomething(name: String) {
// ...
}
@Transactional("account")
fun doSomething() {
// ...
}
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following listing shows the bean declarations:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<tx:annotation-driven/>
<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
...
<qualifier value="order"/>
</bean>
<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
...
<qualifier value="account"/>
</bean>
----
2018-09-05 23:15:53 +08:00
2018-11-05 19:27:35 +08:00
In this case, the two methods on `TransactionalService` run under separate transaction
managers, differentiated by the `order` and `account` qualifiers. The default
`<tx:annotation-driven>` target bean name, `transactionManager`, is still used if no
specifically qualified `PlatformTransactionManager` bean is found.
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[tx-custom-attributes]]
2020-05-08 00:42:02 +08:00
===== Custom Composed Annotations
2017-10-19 02:24:17 +08:00
2018-11-05 19:27:35 +08:00
If you find you repeatedly use the same attributes with `@Transactional` on many different
2019-03-05 20:08:34 +08:00
methods, <<core.adoc#beans-meta-annotations, Spring's meta-annotation support>> lets you
2020-05-08 00:42:02 +08:00
define custom composed annotations for your specific use cases. For example, consider the
2018-11-05 19:27:35 +08:00
following annotation definitions:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
2020-05-08 00:42:02 +08:00
@Transactional(transactionManager = "order", label = "causal-consistency")
2015-03-03 18:38:01 +08:00
public @interface OrderTx {
}
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
2020-05-08 00:42:02 +08:00
@Transactional(transactionManager = "account", label = "retryable")
2015-03-03 18:38:01 +08:00
public @interface AccountTx {
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim",role="secondary"]
.Kotlin
----
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.TYPE)
@Retention(AnnotationRetention.RUNTIME)
2020-05-08 00:42:02 +08:00
@Transactional(transactionManager = "order", label = ["causal-consistency"])
2019-09-03 22:03:28 +08:00
annotation class OrderTx
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.TYPE)
@Retention(AnnotationRetention.RUNTIME)
2020-05-08 00:42:02 +08:00
@Transactional(transactionManager = "account", label = ["retryable"])
2019-09-03 22:03:28 +08:00
annotation class AccountTx
----
2015-03-03 18:38:01 +08:00
2020-05-08 00:42:02 +08:00
The preceding annotations let us write the example from the previous section as follows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class TransactionalService {
@OrderTx
2019-09-03 22:03:28 +08:00
public void setSomething(String name) {
// ...
}
2015-03-03 18:38:01 +08:00
@AccountTx
2019-09-03 22:03:28 +08:00
public void doSomething() {
// ...
}
}
----
[source,kotlin,indent=0,subs="verbatim",role="secondary"]
.Kotlin
----
class TransactionalService {
@OrderTx
fun setSomething(name: String) {
// ...
}
@AccountTx
fun doSomething() {
// ...
}
2015-03-03 18:38:01 +08:00
}
----
2018-09-05 23:15:53 +08:00
2020-04-09 20:55:18 +08:00
In the preceding example, we used the syntax to define the transaction manager qualifier
and transactional labels, but we could also have included propagation behavior,
rollback rules, timeouts, and other features.
2015-03-03 18:38:01 +08:00
[[tx-propagation]]
2018-09-05 23:15:53 +08:00
==== Transaction Propagation
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
This section describes some semantics of transaction propagation in Spring. Note
that this section is not an introduction to transaction propagation proper. Rather, it
2015-03-03 18:38:01 +08:00
details some of the semantics regarding transaction propagation in Spring.
2018-09-05 23:15:53 +08:00
In Spring-managed transactions, be aware of the difference between physical and
logical transactions, and how the propagation setting applies to this difference.
2015-03-03 18:38:01 +08:00
[[tx-propagation-required]]
2018-09-05 23:15:53 +08:00
===== Understanding `PROPAGATION_REQUIRED`
2017-10-19 02:24:17 +08:00
2017-01-05 00:51:58 +08:00
image::images/tx_prop_required.png[]
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
`PROPAGATION_REQUIRED` enforces a physical transaction, either locally for the current
scope if no transaction exists yet or participating in an existing 'outer' transaction
2018-02-06 05:51:43 +08:00
defined for a larger scope. This is a fine default in common call stack arrangements
2018-09-05 23:15:53 +08:00
within the same thread (for example, a service facade that delegates to several repository methods
where all the underlying resources have to participate in the service-level transaction).
NOTE: By default, a participating transaction joins the characteristics of the outer scope,
silently ignoring the local isolation level, timeout value, or read-only flag (if any).
Consider switching the `validateExistingTransactions` flag to `true` on your transaction
manager if you want isolation level declarations to be rejected when participating in
an existing transaction with a different isolation level. This non-lenient mode also
rejects read-only mismatches (that is, an inner read-write transaction that tries to participate
in a read-only outer scope).
When the propagation setting is `PROPAGATION_REQUIRED`, a logical transaction scope
2015-03-03 18:38:01 +08:00
is created for each method upon which the setting is applied. Each such logical
transaction scope can determine rollback-only status individually, with an outer
2018-02-06 05:51:43 +08:00
transaction scope being logically independent from the inner transaction scope.
2018-09-05 23:15:53 +08:00
In the case of standard `PROPAGATION_REQUIRED` behavior, all these scopes are
2015-03-03 18:38:01 +08:00
mapped to the same physical transaction. So a rollback-only marker set in the inner
2018-09-05 23:15:53 +08:00
transaction scope does affect the outer transaction's chance to actually commit.
2015-03-03 18:38:01 +08:00
However, in the case where an inner transaction scope sets the rollback-only marker, the
2018-09-05 23:15:53 +08:00
outer transaction has not decided on the rollback itself, so the rollback (silently
2015-03-03 18:38:01 +08:00
triggered by the inner transaction scope) is unexpected. A corresponding
2018-09-05 23:15:53 +08:00
`UnexpectedRollbackException` is thrown at that point. This is expected behavior so
2015-03-03 18:38:01 +08:00
that the caller of a transaction can never be misled to assume that a commit was
2018-09-05 23:15:53 +08:00
performed when it really was not. So, if an inner transaction (of which the outer caller
2015-03-03 18:38:01 +08:00
is not aware) silently marks a transaction as rollback-only, the outer caller still
calls commit. The outer caller needs to receive an `UnexpectedRollbackException` to
indicate clearly that a rollback was performed instead.
[[tx-propagation-requires_new]]
2018-09-05 23:15:53 +08:00
===== Understanding `PROPAGATION_REQUIRES_NEW`
2017-10-19 02:24:17 +08:00
2017-01-05 00:51:58 +08:00
image::images/tx_prop_requires_new.png[]
2015-03-03 18:38:01 +08:00
2018-02-06 05:51:43 +08:00
`PROPAGATION_REQUIRES_NEW`, in contrast to `PROPAGATION_REQUIRED`, always uses an
2018-09-05 23:15:53 +08:00
independent physical transaction for each affected transaction scope, never
2018-02-06 05:51:43 +08:00
participating in an existing transaction for an outer scope. In such an arrangement,
2018-09-05 23:15:53 +08:00
the underlying resource transactions are different and, hence, can commit or roll back
2015-03-03 18:38:01 +08:00
independently, with an outer transaction not affected by an inner transaction's rollback
2018-09-05 23:15:53 +08:00
status and with an inner transaction's locks released immediately after its completion.
Such an independent inner transaction can also declare its own isolation level, timeout,
and read-only settings and not inherit an outer transaction's characteristics.
2015-03-03 18:38:01 +08:00
[[tx-propagation-nested]]
2018-09-05 23:15:53 +08:00
===== Understanding `PROPAGATION_NESTED`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
`PROPAGATION_NESTED` uses a single physical transaction with multiple savepoints
that it can roll back to. Such partial rollbacks let an inner transaction scope
trigger a rollback for its scope, with the outer transaction being able to continue
2015-03-03 18:38:01 +08:00
the physical transaction despite some operations having been rolled back. This setting
2018-09-05 23:15:53 +08:00
is typically mapped onto JDBC savepoints, so it works only with JDBC resource
transactions. See Spring's {api-spring-framework}/jdbc/datasource/DataSourceTransactionManager.html[`DataSourceTransactionManager`].
2015-03-03 18:38:01 +08:00
[[transaction-declarative-applying-more-than-just-tx-advice]]
2018-09-05 23:15:53 +08:00
==== Advising Transactional Operations
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Suppose you want to execute both transactional operations and some basic profiling advice.
2015-03-03 18:38:01 +08:00
How do you effect this in the context of `<tx:annotation-driven/>`?
When you invoke the `updateFoo(Foo)` method, you want to see the following actions:
2018-09-05 23:15:53 +08:00
* The configured profiling aspect starts.
* The transactional advice executes.
* The method on the advised object executes.
* The transaction commits.
* The profiling aspect reports the exact duration of the whole transactional method invocation.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
NOTE: This chapter is not concerned with explaining AOP in any great detail (except as it
applies to transactions). See <<core.adoc#aop,AOP>> for detailed coverage of the AOP
2015-03-03 18:38:01 +08:00
configuration and AOP in general.
2018-09-05 23:15:53 +08:00
The following code shows the simple profiling aspect discussed earlier:
2018-09-21 21:40:00 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
package x.y;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;
import org.springframework.core.Ordered;
public class SimpleProfiler implements Ordered {
private int order;
// allows us to control the ordering of advice
public int getOrder() {
return this.order;
}
public void setOrder(int order) {
this.order = order;
}
2019-03-13 22:12:51 +08:00
// this method is the around advice
2015-03-03 18:38:01 +08:00
public Object profile(ProceedingJoinPoint call) throws Throwable {
Object returnValue;
StopWatch clock = new StopWatch(getClass().getName());
try {
clock.start(call.toShortString());
returnValue = call.proceed();
} finally {
clock.stop();
System.out.println(clock.prettyPrint());
}
return returnValue;
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim",role="secondary"]
.Kotlin
----
class SimpleProfiler : Ordered {
private var order: Int = 0
// allows us to control the ordering of advice
override fun getOrder(): Int {
return this.order
}
fun setOrder(order: Int) {
this.order = order
}
// this method is the around advice
fun profile(call: ProceedingJoinPoint): Any {
var returnValue: Any
val clock = StopWatch(javaClass.name)
try {
clock.start(call.toShortString())
returnValue = call.proceed()
} finally {
clock.stop()
println(clock.prettyPrint())
}
return returnValue
}
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The ordering of advice
is controlled through the `Ordered` interface. For full details on advice ordering, see
<<core.adoc#aop-ataspectj-advice-ordering,Advice ordering>>.
The following configuration creates a `fooService` bean that has profiling and
transactional aspects applied to it in the desired order:
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim"]
2015-03-03 18:38:01 +08:00
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/beans/spring-beans.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/tx
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/tx/spring-tx.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/aop
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/aop/spring-aop.xsd">
2015-03-03 18:38:01 +08:00
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<!-- this is the aspect -->
<bean id="profiler" class="x.y.SimpleProfiler">
<!-- execute before the transactional advice (hence the lower order number) -->
2018-03-05 19:22:17 +08:00
<property name="order" value="1"/>
2015-03-03 18:38:01 +08:00
</bean>
2018-03-05 19:22:17 +08:00
<tx:annotation-driven transaction-manager="txManager" order="200"/>
2015-03-03 18:38:01 +08:00
<aop:config>
<!-- this advice will execute around the transactional advice -->
<aop:aspect id="profilingAspect" ref="profiler">
<aop:pointcut id="serviceMethodWithReturnValue"
expression="execution(!void x.y..*Service.*(..))"/>
<aop:around method="profile" pointcut-ref="serviceMethodWithReturnValue"/>
</aop:aspect>
</aop:config>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
----
2018-09-05 23:15:53 +08:00
You can configure any number
2015-03-03 18:38:01 +08:00
of additional aspects in similar fashion.
2018-09-05 23:15:53 +08:00
The following example creates the same setup as the previous two examples but uses the purely XML
declarative approach:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim"]
2015-03-03 18:38:01 +08:00
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/beans/spring-beans.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/tx
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/tx/spring-tx.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/aop
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/aop/spring-aop.xsd">
2015-03-03 18:38:01 +08:00
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<!-- the profiling advice -->
<bean id="profiler" class="x.y.SimpleProfiler">
<!-- execute before the transactional advice (hence the lower order number) -->
2018-03-05 19:22:17 +08:00
<property name="order" value="1"/>
2015-03-03 18:38:01 +08:00
</bean>
<aop:config>
<aop:pointcut id="entryPointMethod" expression="execution(* x.y..*Service.*(..))"/>
<!-- will execute after the profiling advice (c.f. the order attribute) -->
2018-03-05 19:22:17 +08:00
<aop:advisor advice-ref="txAdvice" pointcut-ref="entryPointMethod" order="2"/>
2015-03-03 18:38:01 +08:00
<!-- order value is higher than the profiling aspect -->
<aop:aspect id="profilingAspect" ref="profiler">
<aop:pointcut id="serviceMethodWithReturnValue"
expression="execution(!void x.y..*Service.*(..))"/>
<aop:around method="profile" pointcut-ref="serviceMethodWithReturnValue"/>
</aop:aspect>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- other <bean/> definitions such as a DataSource and a PlatformTransactionManager here -->
</beans>
----
2018-09-05 23:15:53 +08:00
The result of the preceding configuration is a `fooService` bean that has profiling and
transactional aspects applied to it in that order. If you want the profiling advice
to execute after the transactional advice on the way in and before the
transactional advice on the way out, you can swap the value of the profiling
2015-03-03 18:38:01 +08:00
aspect bean's `order` property so that it is higher than the transactional advice's
order value.
2018-09-27 02:17:07 +08:00
You can configure additional aspects in similar fashion.
2015-03-03 18:38:01 +08:00
[[transaction-declarative-aspectj]]
2018-09-05 23:15:53 +08:00
==== Using `@Transactional` with AspectJ
2015-03-03 18:38:01 +08:00
2018-11-05 19:27:35 +08:00
You can also use the Spring Framework's `@Transactional` support outside of a Spring
container by means of an AspectJ aspect. To do so, first annotate your classes
(and optionally your classes' methods) with the `@Transactional` annotation,
and then link (weave) your application with the
2015-03-03 18:38:01 +08:00
`org.springframework.transaction.aspectj.AnnotationTransactionAspect` defined in the
2018-09-05 23:15:53 +08:00
`spring-aspects.jar` file. You must also configure The aspect with a transaction
manager. You can use the Spring Framework's IoC container to take care of
2015-03-03 18:38:01 +08:00
dependency-injecting the aspect. The simplest way to configure the transaction
management aspect is to use the `<tx:annotation-driven/>` element and specify the `mode`
attribute to `aspectj` as described in <<transaction-declarative-annotations>>. Because
2018-09-05 23:15:53 +08:00
we focus here on applications that run outside of a Spring container, we show
2015-03-03 18:38:01 +08:00
you how to do it programmatically.
2018-09-05 23:15:53 +08:00
NOTE: Prior to continuing, you may want to read <<transaction-declarative-annotations>> and
2017-03-29 20:20:12 +08:00
<<core.adoc#aop, AOP>> respectively.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following example shows how to create a transaction manager and configure the
`AnnotationTransactionAspect` to use it:
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
// construct an appropriate transaction manager
DataSourceTransactionManager txManager = new DataSourceTransactionManager(getDataSource());
// configure the AnnotationTransactionAspect to use it; this must be done before executing any transactional methods
AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager);
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
// construct an appropriate transaction manager
val txManager = DataSourceTransactionManager(getDataSource())
// configure the AnnotationTransactionAspect to use it; this must be done before executing any transactional methods
AnnotationTransactionAspect.aspectOf().transactionManager = txManager
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
NOTE: When you use this aspect, you must annotate the implementation class (or the methods
within that class or both), not the interface (if any) that the class implements. AspectJ
follows Java's rule that annotations on interfaces are not inherited.
2015-03-03 18:38:01 +08:00
The `@Transactional` annotation on a class specifies the default transaction semantics
2018-03-05 19:22:17 +08:00
for the execution of any public method in the class.
2015-03-03 18:38:01 +08:00
The `@Transactional` annotation on a method within the class overrides the default
2018-09-27 02:17:07 +08:00
transaction semantics given by the class annotation (if present). You can annotate any method,
2018-09-05 23:15:53 +08:00
regardless of visibility.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
To weave your applications with the `AnnotationTransactionAspect`, you must either build
2015-03-03 18:38:01 +08:00
your application with AspectJ (see the
2019-03-21 06:48:14 +08:00
https://www.eclipse.org/aspectj/doc/released/devguide/index.html[AspectJ Development
2018-09-05 23:15:53 +08:00
Guide]) or use load-time weaving. See <<core.adoc#aop-aj-ltw,Load-time weaving with
2017-03-29 20:20:12 +08:00
AspectJ in the Spring Framework>> for a discussion of load-time weaving with AspectJ.
2015-03-03 18:38:01 +08:00
[[transaction-programmatic]]
2018-09-05 23:15:53 +08:00
=== Programmatic Transaction Management
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The Spring Framework provides two means of programmatic transaction management, by using:
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
* The `TransactionTemplate`.
* A `PlatformTransactionManager` implementation directly.
2015-03-03 18:38:01 +08:00
The Spring team generally recommends the `TransactionTemplate` for programmatic
transaction management. The second approach is similar to using the JTA
`UserTransaction` API, although exception handling is less cumbersome.
[[tx-prog-template]]
2018-09-05 23:15:53 +08:00
==== Using the `TransactionTemplate`
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The `TransactionTemplate` adopts the same approach as other Spring templates, such as
the `JdbcTemplate`. It uses a callback approach (to free application code from having to
do the boilerplate acquisition and release transactional resources) and results in
code that is intention driven, in that your code focuses solely on what
you want to do.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
NOTE: As the examples that follow show, using the `TransactionTemplate` absolutely
2015-03-03 18:38:01 +08:00
couples you to Spring's transaction infrastructure and APIs. Whether or not programmatic
transaction management is suitable for your development needs is a decision that you
2018-09-05 23:15:53 +08:00
have to make yourself.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Application code that must execute in a transactional context and that explicitly uses the
`TransactionTemplate` resembles the next example. You, as an application
developer, can write a `TransactionCallback` implementation (typically expressed as an
2015-03-03 18:38:01 +08:00
anonymous inner class) that contains the code that you need to execute in the context of
2018-09-05 23:15:53 +08:00
a transaction. You can then pass an instance of your custom `TransactionCallback` to the
`execute(..)` method exposed on the `TransactionTemplate`. The following example shows how to do so:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class SimpleService implements Service {
// single TransactionTemplate shared amongst all methods in this instance
private final TransactionTemplate transactionTemplate;
// use constructor-injection to supply the PlatformTransactionManager
public SimpleService(PlatformTransactionManager transactionManager) {
this.transactionTemplate = new TransactionTemplate(transactionManager);
}
public Object someServiceMethod() {
return transactionTemplate.execute(new TransactionCallback() {
// the code in this method executes in a transactional context
public Object doInTransaction(TransactionStatus status) {
updateOperation1();
return resultOfUpdateOperation2();
}
});
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
// use constructor-injection to supply the PlatformTransactionManager
class SimpleService(transactionManager: PlatformTransactionManager) : Service {
// single TransactionTemplate shared amongst all methods in this instance
private val transactionTemplate = TransactionTemplate(transactionManager)
fun someServiceMethod() = transactionTemplate.execute<Any?> {
updateOperation1()
resultOfUpdateOperation2()
}
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
If there is no return value, you can use the convenient `TransactionCallbackWithoutResult` class
with an anonymous class, as follows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
2019-03-13 22:12:51 +08:00
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
2015-03-03 18:38:01 +08:00
protected void doInTransactionWithoutResult(TransactionStatus status) {
updateOperation1();
updateOperation2();
}
});
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
transactionTemplate.execute(object : TransactionCallbackWithoutResult() {
override fun doInTransactionWithoutResult(status: TransactionStatus) {
updateOperation1()
updateOperation2()
}
})
----
2015-03-03 18:38:01 +08:00
Code within the callback can roll the transaction back by calling the
2018-09-05 23:15:53 +08:00
`setRollbackOnly()` method on the supplied `TransactionStatus` object, as follows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
updateOperation1();
updateOperation2();
2018-07-10 21:30:56 +08:00
} catch (SomeBusinessException ex) {
2019-03-13 22:12:51 +08:00
status.setRollbackOnly();
2015-03-03 18:38:01 +08:00
}
}
});
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
transactionTemplate.execute(object : TransactionCallbackWithoutResult() {
override fun doInTransactionWithoutResult(status: TransactionStatus) {
try {
updateOperation1()
updateOperation2()
} catch (ex: SomeBusinessException) {
status.setRollbackOnly()
}
}
})
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[tx-prog-template-settings]]
2018-09-05 23:15:53 +08:00
===== Specifying Transaction Settings
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
You can specify transaction settings (such as the propagation mode, the isolation level,
the timeout, and so forth) on the `TransactionTemplate` either programmatically or in
configuration. By default, `TransactionTemplate` instances have the
2015-03-03 18:38:01 +08:00
<<transaction-declarative-txadvice-settings,default transactional settings>>. The
following example shows the programmatic customization of the transactional settings for
a specific `TransactionTemplate:`
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class SimpleService implements Service {
private final TransactionTemplate transactionTemplate;
public SimpleService(PlatformTransactionManager transactionManager) {
this.transactionTemplate = new TransactionTemplate(transactionManager);
// the transaction settings can be set here explicitly if so desired
this.transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_UNCOMMITTED);
this.transactionTemplate.setTimeout(30); // 30 seconds
// and so forth...
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class SimpleService(transactionManager: PlatformTransactionManager) : Service {
private val transactionTemplate = TransactionTemplate(transactionManager).apply {
// the transaction settings can be set here explicitly if so desired
isolationLevel = TransactionDefinition.ISOLATION_READ_UNCOMMITTED
timeout = 30 // 30 seconds
// and so forth...
}
}
----
2015-03-03 18:38:01 +08:00
The following example defines a `TransactionTemplate` with some custom transactional
2018-09-05 23:15:53 +08:00
settings by using Spring XML configuration:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<bean id="sharedTransactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="isolationLevelName" value="ISOLATION_READ_UNCOMMITTED"/>
<property name="timeout" value="30"/>
2019-07-08 01:11:50 +08:00
</bean>
2015-03-03 18:38:01 +08:00
----
2018-09-05 23:15:53 +08:00
You can then inject the `sharedTransactionTemplate`
into as many services as are required.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Finally, instances of the `TransactionTemplate` class are thread-safe, in that instances
do not maintain any conversational state. `TransactionTemplate` instances do, however,
maintain configuration state. So, while a number of classes may share a single instance
2015-03-03 18:38:01 +08:00
of a `TransactionTemplate`, if a class needs to use a `TransactionTemplate` with
2018-09-05 23:15:53 +08:00
different settings (for example, a different isolation level), you need to create
2015-03-03 18:38:01 +08:00
two distinct `TransactionTemplate` instances.
[[transaction-programmatic-ptm]]
2018-09-05 23:15:53 +08:00
==== Using the `PlatformTransactionManager`
2015-03-03 18:38:01 +08:00
You can also use the `org.springframework.transaction.PlatformTransactionManager`
2018-09-05 23:15:53 +08:00
directly to manage your transaction. To do so, pass the implementation of the
`PlatformTransactionManager` you use to your bean through a bean reference. Then,
by using the `TransactionDefinition` and `TransactionStatus` objects, you can initiate
transactions, roll back, and commit. The following example shows how to do so:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
2018-09-05 23:15:53 +08:00
// explicitly setting the transaction name is something that can be done only programmatically
2015-03-03 18:38:01 +08:00
def.setName("SomeTxName");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txManager.getTransaction(def);
try {
// execute your business logic here
}
catch (MyException ex) {
txManager.rollback(status);
throw ex;
}
txManager.commit(status);
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val def = DefaultTransactionDefinition()
// explicitly setting the transaction name is something that can be done only programmatically
def.setName("SomeTxName")
def.propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRED
val status = txManager.getTransaction(def)
try {
// execute your business logic here
} catch (ex: MyException) {
txManager.rollback(status)
throw ex
}
txManager.commit(status)
----
2015-03-03 18:38:01 +08:00
[[tx-decl-vs-prog]]
2018-09-05 23:15:53 +08:00
=== Choosing Between Programmatic and Declarative Transaction Management
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Programmatic transaction management is usually a good idea only if you have a small
number of transactional operations. For example, if you have a web application that
2018-09-05 23:15:53 +08:00
requires transactions only for certain update operations, you may not want to set up
transactional proxies by using Spring or any other technology. In this case, using the
`TransactionTemplate` may be a good approach. Being able to set the transaction name
explicitly is also something that can be done only by using the programmatic approach to
2015-03-03 18:38:01 +08:00
transaction management.
On the other hand, if your application has numerous transactional operations,
declarative transaction management is usually worthwhile. It keeps transaction
2018-09-05 23:15:53 +08:00
management out of business logic and is not difficult to configure. When using the
2015-03-03 18:38:01 +08:00
Spring Framework, rather than EJB CMT, the configuration cost of declarative transaction
management is greatly reduced.
2017-10-19 02:24:17 +08:00
2015-05-18 22:47:48 +08:00
[[transaction-event]]
2018-09-05 23:15:53 +08:00
=== Transaction-bound Events
2015-03-03 18:38:01 +08:00
2015-05-18 22:47:48 +08:00
As of Spring 4.2, the listener of an event can be bound to a phase of the transaction. The
2018-09-05 23:15:53 +08:00
typical example is to handle the event when the transaction has completed successfully. Doing so
lets events be used with more flexibility when the outcome of the current transaction
2015-05-18 22:47:48 +08:00
actually matters to the listener.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
You can register a regular event listener by using the `@EventListener` annotation. If you need
to bind it to the transaction, use `@TransactionalEventListener`. When you do so, the listener
is bound to the commit phase of the transaction by default.
2015-05-18 22:47:48 +08:00
2018-09-05 23:15:53 +08:00
The next example shows this concept. Assume that a component publishes an order-created
event and that we want to define a listener that should only handle that event once the
transaction in which it has been published has committed successfully. The following
example sets up such an event listener:
2015-05-18 22:47:48 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-05-18 22:47:48 +08:00
----
@Component
2017-11-21 05:28:00 +08:00
public class MyComponent {
2015-05-18 22:47:48 +08:00
2017-11-21 05:28:00 +08:00
@TransactionalEventListener
public void handleOrderCreatedEvent(CreationEvent<Order> creationEvent) {
2019-09-03 22:03:28 +08:00
// ...
}
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Component
class MyComponent {
@TransactionalEventListener
fun handleOrderCreatedEvent(creationEvent: CreationEvent<Order>) {
// ...
2017-11-21 05:28:00 +08:00
}
}
2015-05-18 22:47:48 +08:00
----
2018-09-05 23:15:53 +08:00
The `@TransactionalEventListener` annotation exposes a `phase` attribute that lets you customize
the phase of the transaction to which the listener should be bound. The valid phases are `BEFORE_COMMIT`,
`AFTER_COMMIT` (default), `AFTER_ROLLBACK`, and `AFTER_COMPLETION` that aggregates the transaction
2015-05-18 22:47:48 +08:00
completion (be it a commit or a rollback).
2018-09-05 23:15:53 +08:00
If no transaction is running, the listener is not invoked at all, since we cannot honor the required
semantics. You can, however, override that behavior by setting the `fallbackExecution` attribute
2015-05-18 22:47:48 +08:00
of the annotation to `true`.
2015-03-03 18:38:01 +08:00
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
[[transaction-application-server-integration]]
=== Application server-specific integration
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Spring's transaction abstraction is generally application server-agnostic. Additionally,
Spring's `JtaTransactionManager` class (which can optionally perform a JNDI lookup for
the JTA `UserTransaction` and `TransactionManager` objects) autodetects the location for
2015-03-03 18:38:01 +08:00
the latter object, which varies by application server. Having access to the JTA
2018-10-25 21:15:58 +08:00
`TransactionManager` allows for enhanced transaction semantics -- in particular,
supporting transaction suspension. See the
{api-spring-framework}/transaction/jta/JtaTransactionManager.html[`JtaTransactionManager`]
javadoc for details.
2015-03-03 18:38:01 +08:00
Spring's `JtaTransactionManager` is the standard choice to run on Java EE application
2018-09-05 23:15:53 +08:00
servers and is known to work on all common servers. Advanced functionality, such as
transaction suspension, works on many servers as well (including GlassFish, JBoss and
Geronimo) without any special configuration required. However, for fully supported
transaction suspension and further advanced integration, Spring includes special adapters
2015-03-03 18:38:01 +08:00
for WebLogic Server and WebSphere. These adapters are discussed in the following
sections.
2018-09-05 23:15:53 +08:00
For standard scenarios, including WebLogic Server and WebSphere, consider using the
convenient `<tx:jta-transaction-manager/>` configuration element. When configured,
2015-03-03 18:38:01 +08:00
this element automatically detects the underlying server and chooses the best
2018-09-05 23:15:53 +08:00
transaction manager available for the platform. This means that you need not explicitly
configure server-specific adapter classes (as discussed in the following sections).
Rather, they are chosen automatically, with the standard
`JtaTransactionManager` as the default fallback.
2015-03-03 18:38:01 +08:00
[[transaction-application-server-integration-websphere]]
==== IBM WebSphere
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
On WebSphere 6.1.0.9 and above, the recommended Spring JTA transaction manager to use is
2018-09-05 23:15:53 +08:00
`WebSphereUowTransactionManager`. This special adapter uses IBM's `UOWManager` API,
2016-12-09 01:24:12 +08:00
which is available in WebSphere Application Server 6.1.0.9 and later. With this adapter,
2018-09-05 23:15:53 +08:00
Spring-driven transaction suspension (suspend and resume as initiated by
2016-12-09 01:24:12 +08:00
`PROPAGATION_REQUIRES_NEW`) is officially supported by IBM.
2015-03-03 18:38:01 +08:00
[[transaction-application-server-integration-weblogic]]
==== Oracle WebLogic Server
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
On WebLogic Server 9.0 or above, you would typically use the
2015-03-03 18:38:01 +08:00
`WebLogicJtaTransactionManager` instead of the stock `JtaTransactionManager` class. This
special WebLogic-specific subclass of the normal `JtaTransactionManager` supports the
full power of Spring's transaction definitions in a WebLogic-managed transaction
2018-09-05 23:15:53 +08:00
environment, beyond standard JTA semantics. Features include transaction names,
2015-03-03 18:38:01 +08:00
per-transaction isolation levels, and proper resuming of transactions in all cases.
[[transaction-solutions-to-common-problems]]
2018-09-05 23:15:53 +08:00
=== Solutions to Common Problems
2018-11-19 11:43:58 +08:00
This section describes solutions to some common problems.
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[transaction-solutions-to-common-problems-wrong-ptm]]
2018-09-05 23:15:53 +08:00
==== Using the Wrong Transaction Manager for a Specific `DataSource`
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Use the correct `PlatformTransactionManager` implementation based on your choice of
2015-03-03 18:38:01 +08:00
transactional technologies and requirements. Used properly, the Spring Framework merely
2018-09-05 23:15:53 +08:00
provides a straightforward and portable abstraction. If you use global
transactions, you must use the
2015-03-03 18:38:01 +08:00
`org.springframework.transaction.jta.JtaTransactionManager` class (or an
<<transaction-application-server-integration,application server-specific subclass>> of
2018-09-05 23:15:53 +08:00
it) for all your transactional operations. Otherwise, the transaction infrastructure
tries to perform local transactions on such resources as container `DataSource`
2015-03-03 18:38:01 +08:00
instances. Such local transactions do not make sense, and a good application server
treats them as errors.
[[transaction-resources]]
2018-09-05 23:15:53 +08:00
=== Further Resources
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
For more information about the Spring Framework's transaction support, see:
2015-03-03 18:38:01 +08:00
2019-03-21 06:48:14 +08:00
* https://www.javaworld.com/javaworld/jw-01-2009/jw-01-spring-transactions.html[Distributed
2015-03-03 18:38:01 +08:00
transactions in Spring, with and without XA] is a JavaWorld presentation in which
Spring's David Syer guides you through seven patterns for distributed
transactions in Spring applications, three of them with XA and four without.
2019-03-21 06:48:14 +08:00
* https://www.infoq.com/minibooks/JTDS[_Java Transaction Design Strategies_] is a book
available from https://www.infoq.com/[InfoQ] that provides a well-paced introduction
2015-03-03 18:38:01 +08:00
to transactions in Java. It also includes side-by-side examples of how to configure
and use transactions with both the Spring Framework and EJB3.
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
[[dao]]
2018-09-05 23:15:53 +08:00
== DAO Support
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
The Data Access Object (DAO) support in Spring is aimed at making it easy to work with
2018-09-05 23:15:53 +08:00
data access technologies (such as JDBC, Hibernate, or JPA) in a consistent way. This
lets you switch between the aforementioned persistence technologies fairly easily,
and it also lets you code without worrying about catching exceptions that are
2015-03-03 18:38:01 +08:00
specific to each technology.
[[dao-exceptions]]
2018-09-05 23:15:53 +08:00
=== Consistent Exception Hierarchy
2018-09-27 02:17:07 +08:00
Spring provides a convenient translation from technology-specific exceptions, such as
2018-09-05 23:15:53 +08:00
`SQLException` to its own exception class hierarchy, which has `DataAccessException` as
the root exception. These exceptions wrap the original exception so that there is never any
risk that you might lose any information about what might have gone wrong.
In addition to JDBC exceptions, Spring can also wrap JPA- and Hibernate-specific exceptions,
converting them to a set of focused runtime exceptions.
2018-09-27 02:17:07 +08:00
This lets you handle most non-recoverable persistence exceptions
2018-09-05 23:15:53 +08:00
in only the appropriate layers, without having annoying boilerplate
catch-and-throw blocks and exception declarations in your DAOs. (You can still trap
and handle exceptions anywhere you need to though.) As mentioned above, JDBC
2015-03-03 18:38:01 +08:00
exceptions (including database-specific dialects) are also converted to the same
2018-09-05 23:15:53 +08:00
hierarchy, meaning that you can perform some operations with JDBC within a consistent
2015-03-03 18:38:01 +08:00
programming model.
2018-09-05 23:15:53 +08:00
The preceding discussion holds true for the various template classes in Spring's support for various ORM
frameworks. If you use the interceptor-based classes, the application must care
about handling `HibernateExceptions` and `PersistenceExceptions` itself, preferably by
delegating to the `convertHibernateAccessException(..)` or
`convertJpaAccessException()` methods, respectively, of `SessionFactoryUtils`. These methods convert the exceptions
to exceptions that are compatible with the exceptions in the `org.springframework.dao`
exception hierarchy. As `PersistenceExceptions` are unchecked, they can get
thrown, too (sacrificing generic DAO abstraction in terms of exceptions, though).
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following image shows the exception hierarchy that Spring provides. (Note that the
2015-03-03 18:38:01 +08:00
class hierarchy detailed in the image shows only a subset of the entire
`DataAccessException` hierarchy.)
2017-04-05 06:09:35 +08:00
image::images/DataAccessException.png[]
2015-03-03 18:38:01 +08:00
[[dao-annotations]]
2018-09-05 23:15:53 +08:00
=== Annotations Used to Configure DAO or Repository Classes
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
The best way to guarantee that your Data Access Objects (DAOs) or repositories provide
exception translation is to use the `@Repository` annotation. This annotation also
2018-09-05 23:15:53 +08:00
lets the component scanning support find and configure your DAOs and repositories
without having to provide XML configuration entries for them. The following example shows
how to use the `@Repository` annotation:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
2019-09-03 22:03:28 +08:00
@Repository // <1>
2015-03-03 18:38:01 +08:00
public class SomeMovieFinder implements MovieFinder {
// ...
}
----
2018-09-05 23:15:53 +08:00
<1> The `@Repository` annotation.
2018-11-27 06:15:55 +08:00
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Repository // <1>
class SomeMovieFinder : MovieFinder {
// ...
}
----
<1> The `@Repository` annotation.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Any DAO or repository implementation needs access to a persistence resource,
depending on the persistence technology used. For example, a JDBC-based repository
needs access to a JDBC `DataSource`, and a JPA-based repository needs access to an
2015-03-03 18:38:01 +08:00
`EntityManager`. The easiest way to accomplish this is to have this resource dependency
2018-09-27 02:17:07 +08:00
injected by using one of the `@Autowired`, `@Inject`, `@Resource` or `@PersistenceContext`
2018-09-05 23:15:53 +08:00
annotations. The following example works for a JPA repository:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
@Repository
public class JpaMovieFinder implements MovieFinder {
@PersistenceContext
private EntityManager entityManager;
// ...
2019-09-03 22:03:28 +08:00
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Repository
class JpaMovieFinder : MovieFinder {
@PersistenceContext
private lateinit var entityManager: EntityManager
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
// ...
2015-03-03 18:38:01 +08:00
}
----
2019-09-03 22:03:28 +08:00
2018-09-05 23:15:53 +08:00
If you use the classic Hibernate APIs, you can inject `SessionFactory`, as the following
example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
@Repository
public class HibernateMovieFinder implements MovieFinder {
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
// ...
2019-09-03 22:03:28 +08:00
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Repository
class HibernateMovieFinder(private val sessionFactory: SessionFactory) : MovieFinder {
// ...
2015-03-03 18:38:01 +08:00
}
----
2019-09-03 22:03:28 +08:00
The last example we show here is for typical JDBC support. You could have the
`DataSource` injected into an initialization method or a constructor, where you would create a
2018-09-05 23:15:53 +08:00
`JdbcTemplate` and other data access support classes (such as `SimpleJdbcCall` and others) by using
this `DataSource`. The following example autowires a `DataSource`:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
@Repository
public class JdbcMovieFinder implements MovieFinder {
private JdbcTemplate jdbcTemplate;
@Autowired
public void init(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
// ...
2019-09-03 22:03:28 +08:00
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Repository
class JdbcMovieFinder(dataSource: DataSource) : MovieFinder {
private val jdbcTemplate = JdbcTemplate(dataSource)
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
// ...
2015-03-03 18:38:01 +08:00
}
----
2018-09-05 23:15:53 +08:00
NOTE: See the specific coverage of each persistence technology for details on how to
configure the application context to take advantage of these annotations.
2015-03-03 18:38:01 +08:00
2017-10-19 02:24:17 +08:00
2018-10-25 21:15:58 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc]]
2018-09-05 23:15:53 +08:00
== Data Access with JDBC
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The value provided by the Spring Framework JDBC abstraction is perhaps best shown by
the sequence of actions outlined in the following table below. The table shows which actions Spring
takes care of and which actions are your responsibility.
2015-03-03 18:38:01 +08:00
[[jdbc-who-does-what]]
.Spring JDBC - who does what?
|===
| Action| Spring| You
| Define connection parameters.
|
| X
| Open the connection.
| X
|
| Specify the SQL statement.
|
| X
| Declare parameters and provide parameter values
|
| X
| Prepare and execute the statement.
| X
|
| Set up the loop to iterate through the results (if any).
| X
|
| Do the work for each iteration.
|
| X
| Process any exception.
| X
|
| Handle transactions.
| X
|
2018-09-05 23:15:53 +08:00
| Close the connection, the statement, and the resultset.
2015-03-03 18:38:01 +08:00
| X
|
|===
The Spring Framework takes care of all the low-level details that can make JDBC such a
2018-09-05 23:15:53 +08:00
tedious API.
2015-03-03 18:38:01 +08:00
[[jdbc-choose-style]]
2018-09-21 22:32:06 +08:00
=== Choosing an Approach for JDBC Database Access
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
You can choose among several approaches to form the basis for your JDBC database access.
2018-09-05 23:15:53 +08:00
In addition to three flavors of `JdbcTemplate`, a new `SimpleJdbcInsert` and
`SimpleJdbcCall` approach optimizes database metadata, and the RDBMS Object style takes a
2015-03-03 18:38:01 +08:00
more object-oriented approach similar to that of JDO Query design. Once you start using
one of these approaches, you can still mix and match to include a feature from a
different approach. All approaches require a JDBC 2.0-compliant driver, and some
advanced features require a JDBC 3.0 driver.
2018-09-05 23:15:53 +08:00
* `JdbcTemplate` is the classic and most popular Spring JDBC approach. This
"`lowest-level`" approach and all others use a JdbcTemplate under the covers.
* `NamedParameterJdbcTemplate` wraps a `JdbcTemplate` to provide named parameters
instead of the traditional JDBC `?` placeholders. This approach provides better
2015-03-03 18:38:01 +08:00
documentation and ease of use when you have multiple parameters for an SQL statement.
2018-09-05 23:15:53 +08:00
* `SimpleJdbcInsert` and `SimpleJdbcCall` optimize database metadata to limit the amount
of necessary configuration. This approach simplifies coding so that you need to
provide only the name of the table or procedure and provide a map of parameters matching
the column names. This works only if the database provides adequate metadata. If the
database does not provide this metadata, you have to provide explicit
2015-03-03 18:38:01 +08:00
configuration of the parameters.
2018-09-05 23:15:53 +08:00
* RDBMS objects, including `MappingSqlQuery`, `SqlUpdate` and `StoredProcedure`, require
you to create reusable and thread-safe objects during initialization of your data-access
layer. This approach is modeled after JDO Query, wherein you define your query
2015-03-03 18:38:01 +08:00
string, declare parameters, and compile the query. Once you do that, execute methods
2018-09-05 23:15:53 +08:00
can be called multiple times with various parameter values.
2015-03-03 18:38:01 +08:00
[[jdbc-packages]]
2018-09-21 22:32:06 +08:00
=== Package Hierarchy
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The Spring Framework's JDBC abstraction framework consists of four different packages:
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
* `core`: The `org.springframework.jdbc.core` package contains the `JdbcTemplate` class and its
2015-03-03 18:38:01 +08:00
various callback interfaces, plus a variety of related classes. A subpackage named
`org.springframework.jdbc.core.simple` contains the `SimpleJdbcInsert` and
`SimpleJdbcCall` classes. Another subpackage named
`org.springframework.jdbc.core.namedparam` contains the `NamedParameterJdbcTemplate`
class and the related support classes. See <<jdbc-core>>, <<jdbc-advanced-jdbc>>, and
2015-07-30 03:14:16 +08:00
<<jdbc-simple-jdbc>>.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
* `datasource`: The `org.springframework.jdbc.datasource` package contains a utility class for easy
`DataSource` access and various simple `DataSource` implementations that you can use for
2015-07-30 03:14:16 +08:00
testing and running unmodified JDBC code outside of a Java EE container. A subpackage
named `org.springfamework.jdbc.datasource.embedded` provides support for creating
2018-09-05 23:15:53 +08:00
embedded databases by using Java database engines, such as HSQL, H2, and Derby. See
2015-07-30 03:14:16 +08:00
<<jdbc-connections>> and <<jdbc-embedded-database-support>>.
2015-03-03 18:38:01 +08:00
2018-12-26 22:08:44 +08:00
* `object`: The `org.springframework.jdbc.object` package contains classes that represent RDBMS
2015-07-30 03:14:16 +08:00
queries, updates, and stored procedures as thread-safe, reusable objects. See
<<jdbc-object>>. This approach is modeled by JDO, although objects returned by queries
2018-09-05 23:15:53 +08:00
are naturally disconnected from the database. This higher-level of JDBC abstraction
2015-03-03 18:38:01 +08:00
depends on the lower-level abstraction in the `org.springframework.jdbc.core` package.
2018-12-26 22:08:44 +08:00
* `support`: The `org.springframework.jdbc.support` package provides `SQLException` translation
2015-03-03 18:38:01 +08:00
functionality and some utility classes. Exceptions thrown during JDBC processing are
translated to exceptions defined in the `org.springframework.dao` package. This means
that code using the Spring JDBC abstraction layer does not need to implement JDBC or
RDBMS-specific error handling. All translated exceptions are unchecked, which gives you
2018-09-05 23:15:53 +08:00
the option of catching the exceptions from which you can recover while letting other
exceptions be propagated to the caller. See <<jdbc-SQLExceptionTranslator>>.
2015-03-03 18:38:01 +08:00
[[jdbc-core]]
2018-09-05 23:15:53 +08:00
=== Using the JDBC Core Classes to Control Basic JDBC Processing and Error Handling
2018-09-21 22:32:06 +08:00
This section covers how to use the JDBC core classes to control basic JDBC processing,
2018-09-05 23:15:53 +08:00
including error handling. It includes the following topics:
* <<jdbc-JdbcTemplate>>
* <<jdbc-NamedParameterJdbcTemplate>>
* <<jdbc-SQLExceptionTranslator>>
* <<jdbc-statements-executing>>
* <<jdbc-statements-querying>>
* <<jdbc-updates>>
2018-11-19 11:43:58 +08:00
* <<jdbc-auto-generated-keys>>
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc-JdbcTemplate]]
2018-09-05 23:15:53 +08:00
==== Using `JdbcTemplate`
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
`JdbcTemplate` is the central class in the JDBC core package. It handles the
creation and release of resources, which helps you avoid common errors, such as
2015-03-03 18:38:01 +08:00
forgetting to close the connection. It performs the basic tasks of the core JDBC
2018-09-05 23:15:53 +08:00
workflow (such as statement creation and execution), leaving application code to provide
SQL and extract results. The `JdbcTemplate` class:
* Runs SQL queries
* Updates statements and stored procedure calls
* Performs iteration over `ResultSet` instances and extraction of returned parameter values.
* Catches JDBC exceptions and translates them to the generic, more informative, exception
hierarchy defined in the `org.springframework.dao` package. (See <<dao-exceptions>>.)
When you use the `JdbcTemplate` for your code, you need only to implement callback
interfaces, giving them a clearly defined contract. Given a `Connection` provided by the
`JdbcTemplate` class, the `PreparedStatementCreator`
callback interface creates a prepared statement, providing SQL and any necessary parameters. The same is true for the
2015-03-03 18:38:01 +08:00
`CallableStatementCreator` interface, which creates callable statements. The
`RowCallbackHandler` interface extracts values from each row of a `ResultSet`.
2018-09-05 23:15:53 +08:00
You can use `JdbcTemplate` within a DAO implementation through direct instantiation
with a `DataSource` reference, or you can configure it in a Spring IoC container and give it to
2015-03-03 18:38:01 +08:00
DAOs as a bean reference.
2018-09-05 23:15:53 +08:00
NOTE: The `DataSource` should always be configured as a bean in the Spring IoC container. In
2015-03-03 18:38:01 +08:00
the first case the bean is given to the service directly; in the second case it is given
to the prepared template.
All SQL issued by this class is logged at the `DEBUG` level under the category
corresponding to the fully qualified class name of the template instance (typically
2018-09-05 23:15:53 +08:00
`JdbcTemplate`, but it may be different if you use a custom subclass of the
2015-03-03 18:38:01 +08:00
`JdbcTemplate` class).
2018-10-25 21:15:58 +08:00
The following sections provide some examples of `JdbcTemplate` usage. These examples
are not an exhaustive list of all of the functionality exposed by the `JdbcTemplate`.
See the attendant {api-spring-framework}/jdbc/core/JdbcTemplate.html[javadoc] for that.
2015-03-03 18:38:01 +08:00
[[jdbc-JdbcTemplate-examples-query]]
2018-10-25 21:15:58 +08:00
===== Querying (`SELECT`)
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The following query gets the number of rows in a relation:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
int rowCount = this.jdbcTemplate.queryForObject("select count(*) from t_actor", Integer.class);
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val rowCount = jdbcTemplate.queryForObject<Int>("select count(*) from t_actor")!!
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following query uses a bind variable:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
int countOfActorsNamedJoe = this.jdbcTemplate.queryForObject(
"select count(*) from t_actor where first_name = ?", Integer.class, "Joe");
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val countOfActorsNamedJoe = jdbcTemplate.queryForObject<Int>(
"select count(*) from t_actor where first_name = ?", arrayOf("Joe"))!!
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following query looks for a `String`:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
String lastName = this.jdbcTemplate.queryForObject(
"select last_name from t_actor where id = ?",
2020-01-20 08:48:52 +08:00
String.class, 1212L);
2015-03-03 18:38:01 +08:00
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val lastName = this.jdbcTemplate.queryForObject<String>(
"select last_name from t_actor where id = ?",
arrayOf(1212L))!!
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following query finds and populates a single domain object:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
2020-03-17 21:53:15 +08:00
Actor actor = jdbcTemplate.queryForObject(
2015-03-03 18:38:01 +08:00
"select first_name, last_name from t_actor where id = ?",
2020-01-20 08:48:52 +08:00
(resultSet, rowNum) -> {
2020-03-23 03:28:59 +08:00
Actor newActor = new Actor();
newActor.setFirstName(resultSet.getString("first_name"));
newActor.setLastName(resultSet.getString("last_name"));
return newActor;
2020-01-20 08:48:52 +08:00
},
1212L);
2015-03-03 18:38:01 +08:00
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val actor = jdbcTemplate.queryForObject(
"select first_name, last_name from t_actor where id = ?",
arrayOf(1212L)) { rs, _ ->
Actor(rs.getString("first_name"), rs.getString("last_name"))
}
----
2015-03-03 18:38:01 +08:00
2020-03-17 21:53:15 +08:00
The following query finds and populates a list of domain objects:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
List<Actor> actors = this.jdbcTemplate.query(
"select first_name, last_name from t_actor",
2020-01-20 08:48:52 +08:00
(resultSet, rowNum) -> {
Actor actor = new Actor();
actor.setFirstName(resultSet.getString("first_name"));
actor.setLastName(resultSet.getString("last_name"));
return actor;
2015-03-03 18:38:01 +08:00
});
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val actors = jdbcTemplate.query("select first_name, last_name from t_actor") { rs, _ ->
Actor(rs.getString("first_name"), rs.getString("last_name"))
----
2015-03-03 18:38:01 +08:00
If the last two snippets of code actually existed in the same application, it would make
2020-01-20 08:48:52 +08:00
sense to remove the duplication present in the two `RowMapper` lambda expressions and
extract them out into a single field that could then be referenced by DAO methods as needed.
For example, it may be better to write the preceding code snippet as follows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
2020-01-20 08:48:52 +08:00
private final RowMapper<Actor> actorRowMapper = (resultSet, rowNum) -> {
Actor actor = new Actor();
actor.setFirstName(resultSet.getString("first_name"));
actor.setLastName(resultSet.getString("last_name"));
return actor;
};
2015-03-03 18:38:01 +08:00
2020-01-20 08:48:52 +08:00
public List<Actor> findAllActors() {
return this.jdbcTemplate.query( "select first_name, last_name from t_actor", actorRowMapper);
2015-03-03 18:38:01 +08:00
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
2020-03-18 02:22:26 +08:00
val actorMapper = RowMapper<Actor> { rs: ResultSet, rowNum: Int ->
2020-03-18 22:34:27 +08:00
Actor(rs.getString("first_name"), rs.getString("last_name"))
2019-09-03 22:03:28 +08:00
}
2020-03-18 02:22:26 +08:00
fun findAllActors(): List<Actor> {
return jdbcTemplate.query("select first_name, last_name from t_actor", actorMapper)
2019-09-03 22:03:28 +08:00
}
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc-JdbcTemplate-examples-update]]
2018-10-25 21:15:58 +08:00
===== Updating (`INSERT`, `UPDATE`, and `DELETE`) with `JdbcTemplate`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
You can use the `update(..)` method to perform insert, update, and delete operations.
2018-11-19 11:43:58 +08:00
Parameter values are usually provided as variable arguments or, alternatively, as an object array.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following example inserts a new entry:
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
this.jdbcTemplate.update(
"insert into t_actor (first_name, last_name) values (?, ?)",
"Leonor", "Watling");
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
jdbcTemplate.update(
"insert into t_actor (first_name, last_name) values (?, ?)",
"Leonor", "Watling")
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following example updates an existing entry:
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
this.jdbcTemplate.update(
"update t_actor set last_name = ? where id = ?",
"Banjo", 5276L);
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
jdbcTemplate.update(
"update t_actor set last_name = ? where id = ?",
"Banjo", 5276L)
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following example deletes an entry:
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
this.jdbcTemplate.update(
2020-03-23 03:32:45 +08:00
"delete from t_actor where id = ?",
2015-03-03 18:38:01 +08:00
Long.valueOf(actorId));
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
2020-03-23 03:32:45 +08:00
jdbcTemplate.update("delete from t_actor where id = ?", actorId.toLong())
2019-09-03 22:03:28 +08:00
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc-JdbcTemplate-examples-other]]
2018-10-25 21:15:58 +08:00
===== Other `JdbcTemplate` Operations
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
You can use the `execute(..)` method to run any arbitrary SQL. Consequently, the
method is often used for DDL statements. It is heavily overloaded with variants that take
callback interfaces, binding variable arrays, and so on. The following example creates a
table:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
this.jdbcTemplate.execute("create table mytable (id integer, name varchar(100))");
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
jdbcTemplate.execute("create table mytable (id integer, name varchar(100))")
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following example invokes a stored procedure:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
this.jdbcTemplate.update(
"call SUPPORT.REFRESH_ACTORS_SUMMARY(?)",
Long.valueOf(unionId));
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
jdbcTemplate.update(
"call SUPPORT.REFRESH_ACTORS_SUMMARY(?)",
unionId.toLong())
----
2018-09-05 23:15:53 +08:00
2019-03-05 20:08:34 +08:00
More sophisticated stored procedure support is <<jdbc-StoredProcedure, covered later>>.
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc-JdbcTemplate-idioms]]
2018-09-05 23:15:53 +08:00
===== `JdbcTemplate` Best Practices
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Instances of the `JdbcTemplate` class are thread-safe, once configured. This is
2015-03-03 18:38:01 +08:00
important because it means that you can configure a single instance of a `JdbcTemplate`
2018-09-05 23:15:53 +08:00
and then safely inject this shared reference into multiple DAOs (or repositories).
2015-03-03 18:38:01 +08:00
The `JdbcTemplate` is stateful, in that it maintains a reference to a `DataSource`, but
2018-09-05 23:15:53 +08:00
this state is not conversational state.
2015-03-03 18:38:01 +08:00
A common practice when using the `JdbcTemplate` class (and the associated
2019-03-05 20:08:34 +08:00
<<jdbc-NamedParameterJdbcTemplate, `NamedParameterJdbcTemplate`>> class) is to
2018-09-05 23:15:53 +08:00
configure a `DataSource` in your Spring configuration file and then dependency-inject
that shared `DataSource` bean into your DAO classes. The `JdbcTemplate` is created in
the setter for the `DataSource`. This leads to DAOs that resemble the following:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcCorporateEventDao implements CorporateEventDao {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
2019-03-13 22:12:51 +08:00
this.jdbcTemplate = new JdbcTemplate(dataSource);
2015-03-03 18:38:01 +08:00
}
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcCorporateEventDao(dataSource: DataSource) : CorporateEventDao {
private val jdbcTemplate = JdbcTemplate(dataSource)
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following example shows the corresponding XML configuration:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/beans/spring-beans.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/context
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/context/spring-context.xsd">
2015-03-03 18:38:01 +08:00
<bean id="corporateEventDao" class="com.example.JdbcCorporateEventDao">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
</beans>
----
An alternative to explicit configuration is to use component-scanning and annotation
2018-09-05 23:15:53 +08:00
support for dependency injection. In this case, you can annotate the class with `@Repository`
2015-03-03 18:38:01 +08:00
(which makes it a candidate for component-scanning) and annotate the `DataSource` setter
2018-09-05 23:15:53 +08:00
method with `@Autowired`. The following example shows how to do so:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
2019-09-03 22:03:28 +08:00
@Repository // <1>
2015-03-03 18:38:01 +08:00
public class JdbcCorporateEventDao implements CorporateEventDao {
private JdbcTemplate jdbcTemplate;
2019-09-03 22:03:28 +08:00
@Autowired // <2>
2015-03-03 18:38:01 +08:00
public void setDataSource(DataSource dataSource) {
2019-09-03 22:03:28 +08:00
this.jdbcTemplate = new JdbcTemplate(dataSource); // <3>
2015-03-03 18:38:01 +08:00
}
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
}
----
2018-09-05 23:15:53 +08:00
<1> Annotate the class with `@Repository`.
2019-09-03 22:03:28 +08:00
<2> Annotate the `DataSource` setter method with `@Autowired`.
<3> Create a new `JdbcTemplate` with the `DataSource`.
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Repository // <1>
class JdbcCorporateEventDao(dataSource: DataSource) : CorporateEventDao { // <2>
private val jdbcTemplate = JdbcTemplate(dataSource) // <3>
// JDBC-backed implementations of the methods on the CorporateEventDao follow...
}
----
<1> Annotate the class with `@Repository`.
<2> Constructor injection of the `DataSource`.
2018-09-05 23:15:53 +08:00
<3> Create a new `JdbcTemplate` with the `DataSource`.
2018-11-27 06:15:55 +08:00
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following example shows the corresponding XML configuration:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/beans/spring-beans.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/context
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/context/spring-context.xsd">
2015-03-03 18:38:01 +08:00
<!-- Scans within the base package of the application for @Component classes to configure as beans -->
<context:component-scan base-package="org.springframework.docs.test" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
</beans>
----
2018-09-05 23:15:53 +08:00
If you use Spring's `JdbcDaoSupport` class and your various JDBC-backed DAO classes
extend from it, your sub-class inherits a `setDataSource(..)` method from the
2015-03-03 18:38:01 +08:00
`JdbcDaoSupport` class. You can choose whether to inherit from this class. The
`JdbcDaoSupport` class is provided as a convenience only.
Regardless of which of the above template initialization styles you choose to use (or
not), it is seldom necessary to create a new instance of a `JdbcTemplate` class each
2018-09-05 23:15:53 +08:00
time you want to run SQL. Once configured, a `JdbcTemplate` instance is thread-safe.
If your application accesses multiple
databases, you may want multiple `JdbcTemplate` instances, which requires multiple `DataSources` and, subsequently, multiple differently
configured `JdbcTemplate` instances.
2015-03-03 18:38:01 +08:00
[[jdbc-NamedParameterJdbcTemplate]]
2018-09-05 23:15:53 +08:00
==== Using `NamedParameterJdbcTemplate`
2015-03-03 18:38:01 +08:00
The `NamedParameterJdbcTemplate` class adds support for programming JDBC statements
2018-09-05 23:15:53 +08:00
by using named parameters, as opposed to programming JDBC statements using only classic
2015-03-03 18:38:01 +08:00
placeholder ( `'?'`) arguments. The `NamedParameterJdbcTemplate` class wraps a
2018-09-05 23:15:53 +08:00
`JdbcTemplate` and delegates to the wrapped `JdbcTemplate` to do much of its work. This
2015-03-03 18:38:01 +08:00
section describes only those areas of the `NamedParameterJdbcTemplate` class that differ
2018-09-05 23:15:53 +08:00
from the `JdbcTemplate` itself -- namely, programming JDBC statements by using named
2018-11-19 11:43:58 +08:00
parameters. The following example shows how to use `NamedParameterJdbcTemplate`:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
// some JDBC-backed DAO class...
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public int countOfActorsByFirstName(String firstName) {
String sql = "select count(*) from T_ACTOR where first_name = :first_name";
SqlParameterSource namedParameters = new MapSqlParameterSource("first_name", firstName);
return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
private val namedParameterJdbcTemplate = NamedParameterJdbcTemplate(dataSource)
fun countOfActorsByFirstName(firstName: String): Int {
val sql = "select count(*) from T_ACTOR where first_name = :first_name"
val namedParameters = MapSqlParameterSource("first_name", firstName)
return namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Int::class.java)!!
}
----
2015-03-03 18:38:01 +08:00
Notice the use of the named parameter notation in the value assigned to the `sql`
2018-09-05 23:15:53 +08:00
variable and the corresponding value that is plugged into the `namedParameters`
2015-03-03 18:38:01 +08:00
variable (of type `MapSqlParameterSource`).
Alternatively, you can pass along named parameters and their corresponding values to a
`NamedParameterJdbcTemplate` instance by using the `Map`-based style.The remaining
methods exposed by the `NamedParameterJdbcOperations` and implemented by the
`NamedParameterJdbcTemplate` class follow a similar pattern and are not covered here.
2018-09-05 23:15:53 +08:00
The following example shows the use of the `Map`-based style:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
// some JDBC-backed DAO class...
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public int countOfActorsByFirstName(String firstName) {
String sql = "select count(*) from T_ACTOR where first_name = :first_name";
Map<String, String> namedParameters = Collections.singletonMap("first_name", firstName);
return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
// some JDBC-backed DAO class...
private val namedParameterJdbcTemplate = NamedParameterJdbcTemplate(dataSource)
fun countOfActorsByFirstName(firstName: String): Int {
val sql = "select count(*) from T_ACTOR where first_name = :first_name"
val namedParameters = mapOf("first_name" to firstName)
return namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Int::class.java)!!
}
----
2015-03-03 18:38:01 +08:00
One nice feature related to the `NamedParameterJdbcTemplate` (and existing in the same
Java package) is the `SqlParameterSource` interface. You have already seen an example of
2018-09-05 23:15:53 +08:00
an implementation of this interface in one of the previous code snippets (the
2015-03-03 18:38:01 +08:00
`MapSqlParameterSource` class). An `SqlParameterSource` is a source of named parameter
2018-09-05 23:15:53 +08:00
values to a `NamedParameterJdbcTemplate`. The `MapSqlParameterSource` class is a
simple implementation that is an adapter around a `java.util.Map`, where the keys
2015-03-03 18:38:01 +08:00
are the parameter names and the values are the parameter values.
Another `SqlParameterSource` implementation is the `BeanPropertySqlParameterSource`
class. This class wraps an arbitrary JavaBean (that is, an instance of a class that
2019-03-21 06:48:14 +08:00
adheres to https://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html[the
2018-09-05 23:15:53 +08:00
JavaBean conventions]) and uses the properties of the wrapped JavaBean as the source
2015-03-03 18:38:01 +08:00
of named parameter values.
2018-09-05 23:15:53 +08:00
The following example shows a typical JavaBean:
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class Actor {
private Long id;
private String firstName;
private String lastName;
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public Long getId() {
return this.id;
}
// setters omitted...
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
data class Actor(val id: Long, val firstName: String, val lastName: String)
----
2018-09-05 23:15:53 +08:00
The following example uses a `NamedParameterJdbcTemplate` to return the count of the
members of the class shown in the preceding example:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
// some JDBC-backed DAO class...
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public int countOfActors(Actor exampleActor) {
// notice how the named parameters match the properties of the above 'Actor' class
String sql = "select count(*) from T_ACTOR where first_name = :firstName and last_name = :lastName";
SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(exampleActor);
return this.namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Integer.class);
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
// some JDBC-backed DAO class...
private val namedParameterJdbcTemplate = NamedParameterJdbcTemplate(dataSource)
private val namedParameterJdbcTemplate = NamedParameterJdbcTemplate(dataSource)
fun countOfActors(exampleActor: Actor): Int {
// notice how the named parameters match the properties of the above 'Actor' class
val sql = "select count(*) from T_ACTOR where first_name = :firstName and last_name = :lastName"
val namedParameters = BeanPropertySqlParameterSource(exampleActor)
return namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Int::class.java)!!
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Remember that the `NamedParameterJdbcTemplate` class wraps a classic `JdbcTemplate`
template. If you need access to the wrapped `JdbcTemplate` instance to access
functionality that is present only in the `JdbcTemplate` class, you can use the
2015-03-03 18:38:01 +08:00
`getJdbcOperations()` method to access the wrapped `JdbcTemplate` through the
`JdbcOperations` interface.
See also <<jdbc-JdbcTemplate-idioms>> for guidelines on using the
`NamedParameterJdbcTemplate` class in the context of an application.
[[jdbc-SQLExceptionTranslator]]
2018-09-05 23:15:53 +08:00
==== Using `SQLExceptionTranslator`
2015-03-03 18:38:01 +08:00
`SQLExceptionTranslator` is an interface to be implemented by classes that can translate
between `SQLExceptions` and Spring's own `org.springframework.dao.DataAccessException`,
which is agnostic in regard to data access strategy. Implementations can be generic (for
example, using SQLState codes for JDBC) or proprietary (for example, using Oracle error
codes) for greater precision.
`SQLErrorCodeSQLExceptionTranslator` is the implementation of `SQLExceptionTranslator`
that is used by default. This implementation uses specific vendor codes. It is more
precise than the `SQLState` implementation. The error code translations are based on
codes held in a JavaBean type class called `SQLErrorCodes`. This class is created and
2018-09-05 23:15:53 +08:00
populated by an `SQLErrorCodesFactory`, which (as the name suggests) is a factory for
2015-03-03 18:38:01 +08:00
creating `SQLErrorCodes` based on the contents of a configuration file named
`sql-error-codes.xml`. This file is populated with vendor codes and based on the
2018-09-05 23:15:53 +08:00
`DatabaseProductName` taken from `DatabaseMetaData`. The codes for the actual
2015-03-03 18:38:01 +08:00
database you are using are used.
The `SQLErrorCodeSQLExceptionTranslator` applies matching rules in the following sequence:
2018-09-05 23:15:53 +08:00
. Any custom translation implemented by a subclass. Normally, the provided concrete
`SQLErrorCodeSQLExceptionTranslator` is used, so this rule does not apply. It
applies only if you have actually provided a subclass implementation.
. Any custom implementation of the `SQLExceptionTranslator` interface that is provided
2015-03-03 18:38:01 +08:00
as the `customSqlExceptionTranslator` property of the `SQLErrorCodes` class.
2018-09-05 23:15:53 +08:00
. The list of instances of the `CustomSQLErrorCodesTranslation` class (provided for the
`customTranslations` property of the `SQLErrorCodes` class) are searched for a match.
. Error code matching is applied.
. Use the fallback translator. `SQLExceptionSubclassTranslator` is the default fallback
translator. If this translation is not available, the next fallback translator is
2015-03-03 18:38:01 +08:00
the `SQLStateSQLExceptionTranslator`.
2018-09-05 23:15:53 +08:00
NOTE: The `SQLErrorCodesFactory` is used by default to define `Error` codes and custom exception
translations. They are looked up in a file named `sql-error-codes.xml` from the
classpath, and the matching `SQLErrorCodes` instance is located based on the database
name from the database metadata of the database in use.
You can extend `SQLErrorCodeSQLExceptionTranslator`, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class CustomSQLErrorCodesTranslator extends SQLErrorCodeSQLExceptionTranslator {
2019-09-03 22:03:28 +08:00
protected DataAccessException customTranslate(String task, String sql, SQLException sqlEx) {
if (sqlEx.getErrorCode() == -12345) {
return new DeadlockLoserDataAccessException(task, sqlEx);
2015-03-03 18:38:01 +08:00
}
return null;
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class CustomSQLErrorCodesTranslator : SQLErrorCodeSQLExceptionTranslator() {
override fun customTranslate(task: String, sql: String?, sqlEx: SQLException): DataAccessException? {
if (sqlEx.errorCode == -12345) {
return DeadlockLoserDataAccessException(task, sqlEx)
}
return null;
}
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
In the preceding example, the specific error code (`-12345`) is translated, while other errors are
2015-03-03 18:38:01 +08:00
left to be translated by the default translator implementation. To use this custom
2018-09-05 23:15:53 +08:00
translator, you must pass it to the `JdbcTemplate` through the method
`setExceptionTranslator`, and you must use this `JdbcTemplate` for all of the data access
processing where this translator is needed. The following example shows how you can use this custom
translator:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
// create a JdbcTemplate and set data source
this.jdbcTemplate = new JdbcTemplate();
this.jdbcTemplate.setDataSource(dataSource);
// create a custom translator and set the DataSource for the default translation lookup
CustomSQLErrorCodesTranslator tr = new CustomSQLErrorCodesTranslator();
tr.setDataSource(dataSource);
this.jdbcTemplate.setExceptionTranslator(tr);
}
public void updateShippingCharge(long orderId, long pct) {
// use the prepared JdbcTemplate for this update
this.jdbcTemplate.update("update orders" +
" set shipping_charge = shipping_charge * ? / 100" +
" where id = ?", pct, orderId);
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
// create a JdbcTemplate and set data source
private val jdbcTemplate = JdbcTemplate(dataSource).apply {
// create a custom translator and set the DataSource for the default translation lookup
exceptionTranslator = CustomSQLErrorCodesTranslator().apply {
this.dataSource = dataSource
}
}
fun updateShippingCharge(orderId: Long, pct: Long) {
// use the prepared JdbcTemplate for this update
this.jdbcTemplate!!.update("update orders" +
" set shipping_charge = shipping_charge * ? / 100" +
" where id = ?", pct, orderId)
}
----
2015-03-03 18:38:01 +08:00
The custom translator is passed a data source in order to look up the error codes in
`sql-error-codes.xml`.
[[jdbc-statements-executing]]
2018-09-05 23:15:53 +08:00
==== Running Statements
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Running an SQL statement requires very little code. You need a `DataSource` and a
2015-03-03 18:38:01 +08:00
`JdbcTemplate`, including the convenience methods that are provided with the
`JdbcTemplate`. The following example shows what you need to include for a minimal but
fully functional class that creates a new table:
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
public class ExecuteAStatement {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public void doExecute() {
this.jdbcTemplate.execute("create table mytable (id integer, name varchar(100))");
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
import javax.sql.DataSource
import org.springframework.jdbc.core.JdbcTemplate
class ExecuteAStatement(dataSource: DataSource) {
private val jdbcTemplate = JdbcTemplate(dataSource)
fun doExecute() {
jdbcTemplate.execute("create table mytable (id integer, name varchar(100))")
}
}
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc-statements-querying]]
2018-09-05 23:15:53 +08:00
==== Running Queries
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Some query methods return a single value. To retrieve a count or a specific value from
one row, use `queryForObject(..)`. The latter converts the returned JDBC `Type` to the
2018-09-05 23:15:53 +08:00
Java class that is passed in as an argument. If the type conversion is invalid, an
`InvalidDataAccessApiUsageException` is thrown. The following example contains two
query methods, one for an `int` and one that queries for a `String`:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
public class RunAQuery {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public int getCount() {
return this.jdbcTemplate.queryForObject("select count(*) from mytable", Integer.class);
}
public String getName() {
return this.jdbcTemplate.queryForObject("select name from mytable", String.class);
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
import javax.sql.DataSource
import org.springframework.jdbc.core.JdbcTemplate
class RunAQuery(dataSource: DataSource) {
private val jdbcTemplate = JdbcTemplate(dataSource)
val count: Int
get() = jdbcTemplate.queryForObject("select count(*) from mytable")!!
val name: String?
get() = jdbcTemplate.queryForObject("select name from mytable")
}
----
2015-03-03 18:38:01 +08:00
In addition to the single result query methods, several methods return a list with an
2018-09-05 23:15:53 +08:00
entry for each row that the query returned. The most generic method is `queryForList(..)`,
2018-07-08 20:11:04 +08:00
which returns a `List` where each element is a `Map` containing one entry for each column,
2018-09-05 23:15:53 +08:00
using the column name as the key. If you add a method to the preceding example to retrieve a
list of all the rows, it might be as follows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public List<Map<String, Object>> getList() {
return this.jdbcTemplate.queryForList("select * from mytable");
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
private val jdbcTemplate = JdbcTemplate(dataSource)
fun getList(): List<Map<String, Any>> {
return jdbcTemplate.queryForList("select * from mytable")
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The returned list would resemble the following:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[literal,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
[{name=Bob, id=1}, {name=Mary, id=2}]
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc-updates]]
2018-09-05 23:15:53 +08:00
==== Updating the Database
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The following example updates a column for a certain primary key:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
public class ExecuteAnUpdate {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public void setName(int id, String name) {
this.jdbcTemplate.update("update mytable set name = ? where id = ?", name, id);
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
import javax.sql.DataSource
import org.springframework.jdbc.core.JdbcTemplate
class ExecuteAnUpdate(dataSource: DataSource) {
private val jdbcTemplate = JdbcTemplate(dataSource)
fun setName(id: Int, name: String) {
jdbcTemplate.update("update mytable set name = ? where id = ?", name, id)
}
}
----
2018-09-05 23:15:53 +08:00
In the preceding example,
an SQL statement has placeholders for row parameters. You can pass the parameter values
2018-11-19 11:43:58 +08:00
in as varargs or ,alternatively, as an array of objects. Thus, you should explicitly wrap primitives
2018-09-05 23:15:53 +08:00
in the primitive wrapper classes, or you should use auto-boxing.
2015-03-03 18:38:01 +08:00
2018-11-19 11:43:58 +08:00
[[jdbc-auto-generated-keys]]
2018-09-05 23:15:53 +08:00
==== Retrieving Auto-generated Keys
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
An `update()` convenience method supports the retrieval of primary keys generated by the
2018-09-05 23:15:53 +08:00
database. This support is part of the JDBC 3.0 standard. See Chapter 13.6 of the
2015-03-03 18:38:01 +08:00
specification for details. The method takes a `PreparedStatementCreator` as its first
argument, and this is the way the required insert statement is specified. The other
2017-11-20 04:17:32 +08:00
argument is a `KeyHolder`, which contains the generated key on successful return from the
2018-09-05 23:15:53 +08:00
update. There is no standard single way to create an appropriate `PreparedStatement`
2017-11-20 04:17:32 +08:00
(which explains why the method signature is the way it is). The following example works
on Oracle but may not work on other platforms:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
final String INSERT_SQL = "insert into my_test (name) values(?)";
final String name = "Rob";
KeyHolder keyHolder = new GeneratedKeyHolder();
2020-01-20 08:48:52 +08:00
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(INSERT_SQL, new String[] { "id" });
ps.setString(1, name);
return ps;
}, keyHolder);
2015-03-03 18:38:01 +08:00
// keyHolder.getKey() now contains the generated key
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val INSERT_SQL = "insert into my_test (name) values(?)"
val name = "Rob"
val keyHolder = GeneratedKeyHolder()
jdbcTemplate.update({
it.prepareStatement (INSERT_SQL, arrayOf("id")).apply { setString(1, name) }
}, keyHolder)
// keyHolder.getKey() now contains the generated key
----
2015-03-03 18:38:01 +08:00
[[jdbc-connections]]
2018-09-05 23:15:53 +08:00
=== Controlling Database Connections
This section covers:
* <<jdbc-datasource>>
* <<jdbc-DataSourceUtils>>
* <<jdbc-SmartDataSource>>
* <<jdbc-AbstractDataSource>>
* <<jdbc-SingleConnectionDataSource>>
* <<jdbc-DriverManagerDataSource>>
* <<jdbc-TransactionAwareDataSourceProxy>>
* <<jdbc-DataSourceTransactionManager>>
2015-03-03 18:38:01 +08:00
[[jdbc-datasource]]
2018-09-05 23:15:53 +08:00
==== Using `DataSource`
2015-03-03 18:38:01 +08:00
Spring obtains a connection to the database through a `DataSource`. A `DataSource` is
2018-09-05 23:15:53 +08:00
part of the JDBC specification and is a generalized connection factory. It lets a
container or a framework hide connection pooling and transaction management issues
2015-03-03 18:38:01 +08:00
from the application code. As a developer, you need not know details about how to
2018-09-05 23:15:53 +08:00
connect to the database. That is the responsibility of the administrator who sets up
2020-01-24 21:54:02 +08:00
the datasource. You most likely fill both roles as you develop and test code, but you
do not necessarily have to know how the production data source is configured.
2015-03-03 18:38:01 +08:00
2020-01-24 21:54:02 +08:00
When you use Spring's JDBC layer, you can obtain a data source from JNDI, or you can
configure your own with a connection pool implementation provided by a third party.
Traditional choices are Apache Commons DBCP and C3P0 with bean-style `DataSource` classes;
for a modern JDBC connection pool, consider HikariCP with its builder-style API instead.
2015-03-03 18:38:01 +08:00
2020-01-24 21:54:02 +08:00
NOTE: You should use the `DriverManagerDataSource` and `SimpleDriverDataSource` classes
(as included in the Spring distribution) only for testing purposes! Those variants do not
provide pooling and perform poorly when multiple requests for a connection are made.
2015-03-03 18:38:01 +08:00
2020-01-24 21:54:02 +08:00
The following section uses Spring's `DriverManagerDataSource` implementation.
Several other `DataSource` variants are covered later.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
To configure a `DriverManagerDataSource`:
. Obtain a connection with `DriverManagerDataSource` as you typically obtain a JDBC
2018-09-21 21:40:00 +08:00
connection.
. Specify the fully qualified classname of the JDBC driver so that the `DriverManager`
can load the driver class.
. Provide a URL that varies between JDBC drivers. (See the documentation for your driver
for the correct value.)
. Provide a username and a password to connect to the database.
2018-09-05 23:15:53 +08:00
2018-09-21 21:40:00 +08:00
The following example shows how to configure a `DriverManagerDataSource` in Java:
2018-09-05 23:15:53 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
dataSource.setUrl("jdbc:hsqldb:hsql://localhost:");
dataSource.setUsername("sa");
dataSource.setPassword("");
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val dataSource = DriverManagerDataSource().apply {
setDriverClassName("org.hsqldb.jdbcDriver")
url = "jdbc:hsqldb:hsql://localhost:"
username = "sa"
password = ""
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following example shows the corresponding XML configuration:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
----
2018-09-05 23:15:53 +08:00
The next two examples show the basic connectivity and configuration for DBCP and C3P0.
2015-03-03 18:38:01 +08:00
To learn about more options that help control the pooling features, see the product
documentation for the respective connection pooling implementations.
2018-09-05 23:15:53 +08:00
The following example shows DBCP configuration:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
----
2018-09-05 23:15:53 +08:00
The following example shows C3P0 configuration:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc-DataSourceUtils]]
2018-09-05 23:15:53 +08:00
==== Using `DataSourceUtils`
2015-03-03 18:38:01 +08:00
The `DataSourceUtils` class is a convenient and powerful helper class that provides
`static` methods to obtain connections from JNDI and close connections if necessary. It
supports thread-bound connections with, for example, `DataSourceTransactionManager`.
[[jdbc-SmartDataSource]]
2018-09-05 23:15:53 +08:00
==== Implementing `SmartDataSource`
2015-03-03 18:38:01 +08:00
The `SmartDataSource` interface should be implemented by classes that can provide a
2018-09-05 23:15:53 +08:00
connection to a relational database. It extends the `DataSource` interface to let
classes that use it query whether the connection should be closed after a given
operation. This usage is efficient when you know that you need to reuse a connection.
2015-03-03 18:38:01 +08:00
[[jdbc-AbstractDataSource]]
2018-09-05 23:15:53 +08:00
==== Extending `AbstractDataSource`
2015-03-03 18:38:01 +08:00
`AbstractDataSource` is an `abstract` base class for Spring's `DataSource`
2018-09-05 23:15:53 +08:00
implementations. It implements code that is common to all `DataSource` implementations.
You should extend the `AbstractDataSource` class if you write your own `DataSource`
2015-03-03 18:38:01 +08:00
implementation.
[[jdbc-SingleConnectionDataSource]]
2018-09-05 23:15:53 +08:00
==== Using `SingleConnectionDataSource`
2015-03-03 18:38:01 +08:00
The `SingleConnectionDataSource` class is an implementation of the `SmartDataSource`
2018-09-05 23:15:53 +08:00
interface that wraps a single `Connection` that is not closed after each use.
This is not multi-threading capable.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
If any client code calls `close` on the assumption of a pooled connection (as when using
persistence tools), you should set the `suppressClose` property to `true`. This setting returns a
close-suppressing proxy that wraps the physical connection. Note that you can no longer
cast this to a native Oracle `Connection` or a similar object.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
`SingleConnectionDataSource` is primarily a test class. For example, it enables easy testing of code outside an
2015-03-03 18:38:01 +08:00
application server, in conjunction with a simple JNDI environment. In contrast to
`DriverManagerDataSource`, it reuses the same connection all the time, avoiding
excessive creation of physical connections.
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc-DriverManagerDataSource]]
2018-09-05 23:15:53 +08:00
==== Using `DriverManagerDataSource`
2015-03-03 18:38:01 +08:00
The `DriverManagerDataSource` class is an implementation of the standard `DataSource`
2018-09-05 23:15:53 +08:00
interface that configures a plain JDBC driver through bean properties and returns a new
2015-03-03 18:38:01 +08:00
`Connection` every time.
This implementation is useful for test and stand-alone environments outside of a Java EE
2018-09-05 23:15:53 +08:00
container, either as a `DataSource` bean in a Spring IoC container or in conjunction
with a simple JNDI environment. Pool-assuming `Connection.close()` calls
2015-03-03 18:38:01 +08:00
close the connection, so any `DataSource`-aware persistence code should work. However,
2018-09-05 23:15:53 +08:00
using JavaBean-style connection pools (such as `commons-dbcp`) is so easy, even in a test
2015-03-03 18:38:01 +08:00
environment, that it is almost always preferable to use such a connection pool over
`DriverManagerDataSource`.
[[jdbc-TransactionAwareDataSourceProxy]]
2018-09-05 23:15:53 +08:00
==== Using `TransactionAwareDataSourceProxy`
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
`TransactionAwareDataSourceProxy` is a proxy for a target `DataSource`. The proxy wraps that
2015-03-03 18:38:01 +08:00
target `DataSource` to add awareness of Spring-managed transactions. In this respect, it
2018-09-05 23:15:53 +08:00
is similar to a transactional JNDI `DataSource`, as provided by a Java EE server.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
NOTE: It is rarely desirable to use this class, except when already existing code must be
2015-03-03 18:38:01 +08:00
called and passed a standard JDBC `DataSource` interface implementation. In this case,
2018-09-05 23:15:53 +08:00
you can still have this code be usable and, at the same time, have this code
2015-03-03 18:38:01 +08:00
participating in Spring managed transactions. It is generally preferable to write your
2018-09-05 23:15:53 +08:00
own new code by using the higher level abstractions for resource management, such as
2015-03-03 18:38:01 +08:00
`JdbcTemplate` or `DataSourceUtils`.
2018-10-25 21:15:58 +08:00
See the {api-spring-framework}/jdbc/datasource/TransactionAwareDataSourceProxy.html[`TransactionAwareDataSourceProxy`]
javadoc for more details.
2015-03-03 18:38:01 +08:00
[[jdbc-DataSourceTransactionManager]]
2018-09-05 23:15:53 +08:00
==== Using `DataSourceTransactionManager`
2015-03-03 18:38:01 +08:00
The `DataSourceTransactionManager` class is a `PlatformTransactionManager`
implementation for single JDBC datasources. It binds a JDBC connection from the
specified data source to the currently executing thread, potentially allowing for one
thread connection per data source.
Application code is required to retrieve the JDBC connection through
`DataSourceUtils.getConnection(DataSource)` instead of Java EE's standard
`DataSource.getConnection`. It throws unchecked `org.springframework.dao` exceptions
2018-09-05 23:15:53 +08:00
instead of checked `SQLExceptions`. All framework classes (such as `JdbcTemplate`) use this
2015-03-03 18:38:01 +08:00
strategy implicitly. If not used with this transaction manager, the lookup strategy
2018-09-05 23:15:53 +08:00
behaves exactly like the common one. Thus, it can be used in any case.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The `DataSourceTransactionManager` class supports custom isolation levels and timeouts
2015-03-03 18:38:01 +08:00
that get applied as appropriate JDBC statement query timeouts. To support the latter,
application code must either use `JdbcTemplate` or call the
`DataSourceUtils.applyTransactionTimeout(..)` method for each created statement.
2018-09-05 23:15:53 +08:00
You can use this implementation instead of `JtaTransactionManager` in the single-resource
case, as it does not require the container to support JTA. Switching between
both is just a matter of configuration, provided you stick to the required connection lookup
pattern. JTA does not support custom isolation levels.
2015-03-03 18:38:01 +08:00
[[jdbc-advanced-jdbc]]
2018-09-05 23:15:53 +08:00
=== JDBC Batch Operations
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Most JDBC drivers provide improved performance if you batch multiple calls to the same
2018-09-05 23:15:53 +08:00
prepared statement. By grouping updates into batches, you limit the number of round trips
2015-03-03 18:38:01 +08:00
to the database.
[[jdbc-batch-classic]]
2018-09-05 23:15:53 +08:00
==== Basic Batch Operations with `JdbcTemplate`
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
You accomplish `JdbcTemplate` batch processing by implementing two methods of a special
2018-09-05 23:15:53 +08:00
interface, `BatchPreparedStatementSetter`, and passing that implementation in as the second parameter
in your `batchUpdate` method call. You can use the `getBatchSize` method to provide the size of
the current batch. You can use the `setValues` method to set the values for the parameters of
the prepared statement. This method is called the number of times that you
2020-03-23 03:32:45 +08:00
specified in the `getBatchSize` call. The following example updates the `t_actor` table
2018-09-05 23:15:53 +08:00
based on entries in a list, and the entire list is used as the batch:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
2017-11-20 04:17:32 +08:00
2015-03-03 18:38:01 +08:00
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public int[] batchUpdate(final List<Actor> actors) {
2017-11-20 04:17:32 +08:00
return this.jdbcTemplate.batchUpdate(
"update t_actor set first_name = ?, last_name = ? where id = ?",
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
2019-11-30 01:50:42 +08:00
Actor actor = actors.get(i);
ps.setString(1, actor.getFirstName());
ps.setString(2, actor.getLastName());
ps.setLong(3, actor.getId().longValue());
2015-03-03 18:38:01 +08:00
}
public int getBatchSize() {
return actors.size();
}
});
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val jdbcTemplate = JdbcTemplate(dataSource)
fun batchUpdate(actors: List<Actor>): IntArray {
return jdbcTemplate.batchUpdate(
"update t_actor set first_name = ?, last_name = ? where id = ?",
object: BatchPreparedStatementSetter {
override fun setValues(ps: PreparedStatement, i: Int) {
ps.setString(1, actors[i].firstName)
ps.setString(2, actors[i].lastName)
ps.setLong(3, actors[i].id)
}
override fun getBatchSize() = actors.size
})
}
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
If you process a stream of updates or reading from a file, you might have a
2015-03-03 18:38:01 +08:00
preferred batch size, but the last batch might not have that number of entries. In this
2018-09-05 23:15:53 +08:00
case, you can use the `InterruptibleBatchPreparedStatementSetter` interface, which lets
you interrupt a batch once the input source is exhausted. The `isBatchExhausted` method
lets you signal the end of the batch.
2015-03-03 18:38:01 +08:00
[[jdbc-batch-list]]
2018-09-05 23:15:53 +08:00
==== Batch Operations with a List of Objects
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Both the `JdbcTemplate` and the `NamedParameterJdbcTemplate` provides an alternate way
of providing the batch update. Instead of implementing a special batch interface, you
provide all parameter values in the call as a list. The framework loops over these
2018-09-05 23:15:53 +08:00
values and uses an internal prepared statement setter. The API varies, depending on
whether you use named parameters. For the named parameters, you provide an array of
2015-03-03 18:38:01 +08:00
`SqlParameterSource`, one entry for each member of the batch. You can use the
2017-11-20 04:17:32 +08:00
`SqlParameterSourceUtils.createBatch` convenience methods to create this array, passing
2018-09-05 23:15:53 +08:00
in an array of bean-style objects (with getter methods corresponding to parameters),
`String`-keyed `Map` instances (containing the corresponding parameters as values), or a mix of both.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The following example shows a batch update using named parameters:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
2017-11-20 04:17:32 +08:00
2015-03-03 18:38:01 +08:00
private NamedParameterTemplate namedParameterJdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
2017-11-20 04:17:32 +08:00
public int[] batchUpdate(List<Actor> actors) {
return this.namedParameterJdbcTemplate.batchUpdate(
2015-03-03 18:38:01 +08:00
"update t_actor set first_name = :firstName, last_name = :lastName where id = :id",
2017-11-20 04:17:32 +08:00
SqlParameterSourceUtils.createBatch(actors));
2015-03-03 18:38:01 +08:00
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val namedParameterJdbcTemplate = NamedParameterJdbcTemplate(dataSource)
fun batchUpdate(actors: List<Actor>): IntArray {
return this.namedParameterJdbcTemplate.batchUpdate(
"update t_actor set first_name = :firstName, last_name = :lastName where id = :id",
SqlParameterSourceUtils.createBatch(actors));
}
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
2018-09-21 21:40:00 +08:00
For an SQL statement that uses the classic `?` placeholders, you pass in a list
containing an object array with the update values. This object array must have one entry
for each placeholder in the SQL statement, and they must be in the same order as they are
defined in the SQL statement.
2015-03-03 18:38:01 +08:00
2018-09-21 21:40:00 +08:00
The following example is the same as the preceding example, except that it uses classic
JDBC `?` placeholders:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public int[] batchUpdate(final List<Actor> actors) {
List<Object[]> batch = new ArrayList<Object[]>();
for (Actor actor : actors) {
Object[] values = new Object[] {
2017-11-20 04:17:32 +08:00
actor.getFirstName(), actor.getLastName(), actor.getId()};
2015-03-03 18:38:01 +08:00
batch.add(values);
}
2017-11-20 04:17:32 +08:00
return this.jdbcTemplate.batchUpdate(
2015-03-03 18:38:01 +08:00
"update t_actor set first_name = ?, last_name = ? where id = ?",
batch);
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val jdbcTemplate = JdbcTemplate(dataSource)
fun batchUpdate(actors: List<Actor>): IntArray {
val batch = mutableListOf<Array<Any>>()
for (actor in actors) {
batch.add(arrayOf(actor.firstName, actor.lastName, actor.id))
}
return jdbcTemplate.batchUpdate(
"update t_actor set first_name = ?, last_name = ? where id = ?", batch)
}
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
2018-09-21 21:40:00 +08:00
All of the batch update methods that we described earlier return an `int` array
containing the number of affected rows for each batch entry. This count is reported by
the JDBC driver. If the count is not available, the JDBC driver returns a value of `-2`.
2015-03-03 18:38:01 +08:00
2017-11-20 04:17:32 +08:00
[NOTE]
====
2018-09-05 23:15:53 +08:00
In such a scenario, with automatic setting of values on an underlying `PreparedStatement`,
2017-11-20 04:17:32 +08:00
the corresponding JDBC type for each value needs to be derived from the given Java type.
2018-09-05 23:15:53 +08:00
While this usually works well, there is a potential for issues (for example, with Map-contained
`null` values). Spring, by default, calls `ParameterMetaData.getParameterType` in such a
case, which can be expensive with your JDBC driver. You should use a recent driver
version and consider setting the `spring.jdbc.getParameterType.ignore` property to `true`
2017-11-20 04:17:32 +08:00
(as a JVM system property or in a `spring.properties` file in the root of your classpath)
2018-09-05 23:15:53 +08:00
if you encounter a performance issue -- for example, as reported on Oracle 12c (SPR-16139).
2017-11-20 04:17:32 +08:00
2018-09-05 23:15:53 +08:00
Alternatively, you might consider specifying the corresponding JDBC types explicitly,
either through a 'BatchPreparedStatementSetter' (as shown earlier), through an explicit type
array given to a 'List<Object[]>' based call, through 'registerSqlType' calls on a
custom 'MapSqlParameterSource' instance, or through a 'BeanPropertySqlParameterSource'
that derives the SQL type from the Java-declared property type even for a null value.
2017-11-20 04:17:32 +08:00
====
2015-03-03 18:38:01 +08:00
[[jdbc-batch-multi]]
2018-09-05 23:15:53 +08:00
==== Batch Operations with Multiple Batches
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The preceding example of a batch update deals with batches that are so large that you want to
break them up into several smaller batches. You can do this with the methods
mentioned earlier by making multiple calls to the `batchUpdate` method, but there is now a
2015-03-03 18:38:01 +08:00
more convenient method. This method takes, in addition to the SQL statement, a
2018-09-05 23:15:53 +08:00
`Collection` of objects that contain the parameters, the number of updates to make for each
batch, and a `ParameterizedPreparedStatementSetter` to set the values for the parameters
2015-03-03 18:38:01 +08:00
of the prepared statement. The framework loops over the provided values and breaks the
update calls into batches of the size specified.
2018-09-05 23:15:53 +08:00
The following example shows a batch update that uses a batch size of 100:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public int[][] batchUpdate(final Collection<Actor> actors) {
int[][] updateCounts = jdbcTemplate.batchUpdate(
"update t_actor set first_name = ?, last_name = ? where id = ?",
actors,
100,
2020-03-17 21:53:15 +08:00
(PreparedStatement ps, Actor actor) -> {
ps.setString(1, actor.getFirstName());
ps.setString(2, actor.getLastName());
ps.setLong(3, actor.getId().longValue());
2015-03-03 18:38:01 +08:00
});
return updateCounts;
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val jdbcTemplate = JdbcTemplate(dataSource)
fun batchUpdate(actors: List<Actor>): Array<IntArray> {
return jdbcTemplate.batchUpdate(
"update t_actor set first_name = ?, last_name = ? where id = ?",
actors, 100) { ps, argument ->
ps.setString(1, argument.firstName)
ps.setString(2, argument.lastName)
ps.setLong(3, argument.id)
}
}
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The batch update methods for this call returns an array of `int` arrays that contain an array
2015-03-03 18:38:01 +08:00
entry for each batch with an array of the number of affected rows for each update. The top
level array's length indicates the number of batches executed and the second level array's
length indicates the number of updates in that batch. The number of updates in each batch
2018-09-05 23:15:53 +08:00
should be the batch size provided for all batches (except that the last one that might
be less), depending on the total number of update objects provided. The update count for
2015-03-03 18:38:01 +08:00
each update statement is the one reported by the JDBC driver. If the count is not
2018-09-05 23:15:53 +08:00
available, the JDBC driver returns a value of `-2`.
2015-03-03 18:38:01 +08:00
[[jdbc-simple-jdbc]]
2018-09-05 23:15:53 +08:00
=== Simplifying JDBC Operations with the `SimpleJdbc` Classes
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
The `SimpleJdbcInsert` and `SimpleJdbcCall` classes provide a simplified configuration
by taking advantage of database metadata that can be retrieved through the JDBC driver.
2018-09-05 23:15:53 +08:00
This means that you have less to configure up front, although you can override or turn off
2015-03-03 18:38:01 +08:00
the metadata processing if you prefer to provide all the details in your code.
[[jdbc-simple-jdbc-insert-1]]
2018-09-05 23:15:53 +08:00
==== Inserting Data by Using `SimpleJdbcInsert`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
We start by looking at the `SimpleJdbcInsert` class with the minimal amount of
2015-03-03 18:38:01 +08:00
configuration options. You should instantiate the `SimpleJdbcInsert` in the data access
layer's initialization method. For this example, the initializing method is the
2018-09-05 23:15:53 +08:00
`setDataSource` method. You do not need to subclass the `SimpleJdbcInsert` class. Instead,
you can create a new instance and set the table name by using the `withTableName` method.
Configuration methods for this class follow the `fluid` style that returns the instance
of the `SimpleJdbcInsert`, which lets you chain all configuration methods. The following
2018-09-27 02:17:07 +08:00
example uses only one configuration method (we show examples of multiple methods later):
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private SimpleJdbcInsert insertActor;
public void setDataSource(DataSource dataSource) {
this.insertActor = new SimpleJdbcInsert(dataSource).withTableName("t_actor");
}
public void add(Actor actor) {
Map<String, Object> parameters = new HashMap<String, Object>(3);
parameters.put("id", actor.getId());
parameters.put("first_name", actor.getFirstName());
parameters.put("last_name", actor.getLastName());
insertActor.execute(parameters);
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val insertActor = SimpleJdbcInsert(dataSource).withTableName("t_actor")
fun add(actor: Actor) {
val parameters = mutableMapOf<String, Any>()
parameters["id"] = actor.id
parameters["first_name"] = actor.firstName
parameters["last_name"] = actor.lastName
insertActor.execute(parameters)
}
// ... additional methods
}
----
2018-09-05 23:15:53 +08:00
2018-09-27 02:17:07 +08:00
The `execute` method used here takes a plain `java.util.Map` as its only parameter. The
2018-09-05 23:15:53 +08:00
important thing to note here is that the keys used for the `Map` must match the column
names of the table, as defined in the database. This is because we read the metadata
to construct the actual insert statement.
2015-03-03 18:38:01 +08:00
[[jdbc-simple-jdbc-insert-2]]
2018-09-05 23:15:53 +08:00
==== Retrieving Auto-generated Keys by Using `SimpleJdbcInsert`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The next example uses the same insert as the preceding example, but, instead of passing in the `id`, it
retrieves the auto-generated key and sets it on the new `Actor` object. When it creates
the `SimpleJdbcInsert`, in addition to specifying the table name, it specifies the name
of the generated key column with the `usingGeneratedKeyColumns` method. The following
listing shows how it works:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private SimpleJdbcInsert insertActor;
public void setDataSource(DataSource dataSource) {
this.insertActor = new SimpleJdbcInsert(dataSource)
.withTableName("t_actor")
.usingGeneratedKeyColumns("id");
}
public void add(Actor actor) {
Map<String, Object> parameters = new HashMap<String, Object>(2);
parameters.put("first_name", actor.getFirstName());
parameters.put("last_name", actor.getLastName());
Number newId = insertActor.executeAndReturnKey(parameters);
actor.setId(newId.longValue());
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val insertActor = SimpleJdbcInsert(dataSource)
.withTableName("t_actor").usingGeneratedKeyColumns("id")
fun add(actor: Actor): Actor {
val parameters = mapOf(
"first_name" to actor.firstName,
"last_name" to actor.lastName)
val newId = insertActor.executeAndReturnKey(parameters);
return actor.copy(id = newId.toLong())
}
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The main difference when you run the insert by using this second approach is that you do not
add the `id` to the `Map`, and you call the `executeAndReturnKey` method. This returns a
2015-03-03 18:38:01 +08:00
`java.lang.Number` object with which you can create an instance of the numerical type that
2018-09-05 23:15:53 +08:00
is used in your domain class. You cannot rely on all databases to return a specific Java
class here. `java.lang.Number` is the base class that you can rely on. If you have
multiple auto-generated columns or the generated values are non-numeric, you can
2015-03-03 18:38:01 +08:00
use a `KeyHolder` that is returned from the `executeAndReturnKeyHolder` method.
[[jdbc-simple-jdbc-insert-3]]
2018-09-05 23:15:53 +08:00
==== Specifying Columns for a `SimpleJdbcInsert`
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
You can limit the columns for an insert by specifying a list of column names with the
2018-09-05 23:15:53 +08:00
`usingColumns` method, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private SimpleJdbcInsert insertActor;
public void setDataSource(DataSource dataSource) {
this.insertActor = new SimpleJdbcInsert(dataSource)
.withTableName("t_actor")
.usingColumns("first_name", "last_name")
.usingGeneratedKeyColumns("id");
}
public void add(Actor actor) {
Map<String, Object> parameters = new HashMap<String, Object>(2);
parameters.put("first_name", actor.getFirstName());
parameters.put("last_name", actor.getLastName());
Number newId = insertActor.executeAndReturnKey(parameters);
actor.setId(newId.longValue());
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val insertActor = SimpleJdbcInsert(dataSource)
.withTableName("t_actor")
.usingColumns("first_name", "last_name")
.usingGeneratedKeyColumns("id")
fun add(actor: Actor): Actor {
val parameters = mapOf(
"first_name" to actor.firstName,
"last_name" to actor.lastName)
val newId = insertActor.executeAndReturnKey(parameters);
return actor.copy(id = newId.toLong())
}
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
The execution of the insert is the same as if you had relied on the metadata to determine
which columns to use.
[[jdbc-simple-jdbc-parameters]]
2018-09-05 23:15:53 +08:00
==== Using `SqlParameterSource` to Provide Parameter Values
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Using a `Map` to provide parameter values works fine, but it is not the most convenient
2015-03-03 18:38:01 +08:00
class to use. Spring provides a couple of implementations of the `SqlParameterSource`
2018-09-05 23:15:53 +08:00
interface that you can use instead. The first one is `BeanPropertySqlParameterSource`,
2015-03-03 18:38:01 +08:00
which is a very convenient class if you have a JavaBean-compliant class that contains
2018-09-05 23:15:53 +08:00
your values. It uses the corresponding getter method to extract the parameter
values. The following example shows how to use `BeanPropertySqlParameterSource`:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private SimpleJdbcInsert insertActor;
public void setDataSource(DataSource dataSource) {
this.insertActor = new SimpleJdbcInsert(dataSource)
.withTableName("t_actor")
.usingGeneratedKeyColumns("id");
}
public void add(Actor actor) {
SqlParameterSource parameters = new BeanPropertySqlParameterSource(actor);
Number newId = insertActor.executeAndReturnKey(parameters);
actor.setId(newId.longValue());
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val insertActor = SimpleJdbcInsert(dataSource)
.withTableName("t_actor")
.usingGeneratedKeyColumns("id")
fun add(actor: Actor): Actor {
val parameters = BeanPropertySqlParameterSource(actor)
val newId = insertActor.executeAndReturnKey(parameters)
return actor.copy(id = newId.toLong())
}
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Another option is the `MapSqlParameterSource` that resembles a `Map` but provides a more
convenient `addValue` method that can be chained. The following example shows how to use it:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private SimpleJdbcInsert insertActor;
public void setDataSource(DataSource dataSource) {
this.insertActor = new SimpleJdbcInsert(dataSource)
.withTableName("t_actor")
.usingGeneratedKeyColumns("id");
}
public void add(Actor actor) {
SqlParameterSource parameters = new MapSqlParameterSource()
.addValue("first_name", actor.getFirstName())
.addValue("last_name", actor.getLastName());
Number newId = insertActor.executeAndReturnKey(parameters);
actor.setId(newId.longValue());
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val insertActor = SimpleJdbcInsert(dataSource)
.withTableName("t_actor")
.usingGeneratedKeyColumns("id")
fun add(actor: Actor): Actor {
val parameters = MapSqlParameterSource()
.addValue("first_name", actor.firstName)
.addValue("last_name", actor.lastName)
val newId = insertActor.executeAndReturnKey(parameters)
return actor.copy(id = newId.toLong())
}
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
As you can see, the configuration is the same. Only the executing code has to change to
2015-03-03 18:38:01 +08:00
use these alternative input classes.
[[jdbc-simple-jdbc-call-1]]
2018-09-05 23:15:53 +08:00
==== Calling a Stored Procedure with `SimpleJdbcCall`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The `SimpleJdbcCall` class uses metadata in the database to look up names of `in`
and `out` parameters so that you do not have to explicitly declare them. You can
declare parameters if you prefer to do that or if you have parameters (such as `ARRAY`
or `STRUCT`) that do not have an automatic mapping to a Java class. The first example
2015-03-03 18:38:01 +08:00
shows a simple procedure that returns only scalar values in `VARCHAR` and `DATE` format
from a MySQL database. The example procedure reads a specified actor entry and returns
`first_name`, `last_name`, and `birth_date` columns in the form of `out` parameters.
2018-09-05 23:15:53 +08:00
The following listing shows the first example:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,sql,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
CREATE PROCEDURE read_actor (
IN in_id INTEGER,
OUT out_first_name VARCHAR(100),
OUT out_last_name VARCHAR(100),
OUT out_birth_date DATE)
BEGIN
SELECT first_name, last_name, birth_date
INTO out_first_name, out_last_name, out_birth_date
FROM t_actor where id = in_id;
END;
----
2018-09-05 23:15:53 +08:00
The `in_id` parameter contains the `id` of the actor that you are looking up. The `out`
2015-03-03 18:38:01 +08:00
parameters return the data read from the table.
2018-09-05 23:15:53 +08:00
You can declare `SimpleJdbcCall` in a manner similar to declaring `SimpleJdbcInsert`. You
should instantiate and configure the class in the initialization method of your data-access
layer. Compared to the `StoredProcedure` class, you need not create a subclass
and you need not to declare parameters that can be looked up in the database metadata.
The following example of a `SimpleJdbcCall` configuration uses the preceding stored
procedure (the only configuration option, in addition to the `DataSource`, is the name
of the stored procedure):
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private SimpleJdbcCall procReadActor;
public void setDataSource(DataSource dataSource) {
this.procReadActor = new SimpleJdbcCall(dataSource)
.withProcedureName("read_actor");
}
public Actor readActor(Long id) {
SqlParameterSource in = new MapSqlParameterSource()
.addValue("in_id", id);
Map out = procReadActor.execute(in);
Actor actor = new Actor();
actor.setId(id);
actor.setFirstName((String) out.get("out_first_name"));
actor.setLastName((String) out.get("out_last_name"));
actor.setBirthDate((Date) out.get("out_birth_date"));
return actor;
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val procReadActor = SimpleJdbcCall(dataSource)
.withProcedureName("read_actor")
fun readActor(id: Long): Actor {
val source = MapSqlParameterSource().addValue("in_id", id)
val output = procReadActor.execute(source)
return Actor(
id,
output["out_first_name"] as String,
output["out_last_name"] as String,
output["out_birth_date"] as Date)
}
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
The code you write for the execution of the call involves creating an `SqlParameterSource`
2018-09-05 23:15:53 +08:00
containing the IN parameter. You must match the name provided for the input value
2015-03-03 18:38:01 +08:00
with that of the parameter name declared in the stored procedure. The case does not have
to match because you use metadata to determine how database objects should be referred to
in a stored procedure. What is specified in the source for the stored procedure is not
necessarily the way it is stored in the database. Some databases transform names to all
2018-09-05 23:15:53 +08:00
upper case, while others use lower case or use the case as specified.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The `execute` method takes the IN parameters and returns a `Map` that contains any `out`
parameters keyed by the name, as specified in the stored procedure. In this case, they are
`out_first_name`, `out_last_name`, and `out_birth_date`.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The last part of the `execute` method creates an `Actor` instance to use to return the
2015-03-03 18:38:01 +08:00
data retrieved. Again, it is important to use the names of the `out` parameters as they
are declared in the stored procedure. Also, the case in the names of the `out`
parameters stored in the results map matches that of the `out` parameter names in the
2018-09-05 23:15:53 +08:00
database, which could vary between databases. To make your code more portable, you should
2015-05-05 16:58:21 +08:00
do a case-insensitive lookup or instruct Spring to use a `LinkedCaseInsensitiveMap`.
2018-09-05 23:15:53 +08:00
To do the latter, you can create your own `JdbcTemplate` and set the `setResultsMapCaseInsensitive`
property to `true`. Then you can pass this customized `JdbcTemplate` instance into
the constructor of your `SimpleJdbcCall`. The following example shows this configuration:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private SimpleJdbcCall procReadActor;
public void setDataSource(DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.setResultsMapCaseInsensitive(true);
this.procReadActor = new SimpleJdbcCall(jdbcTemplate)
.withProcedureName("read_actor");
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private var procReadActor = SimpleJdbcCall(JdbcTemplate(dataSource).apply {
isResultsMapCaseInsensitive = true
}).withProcedureName("read_actor")
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
By taking this action, you avoid conflicts in the case used for the names of your
returned `out` parameters.
[[jdbc-simple-jdbc-call-2]]
2018-09-05 23:15:53 +08:00
==== Explicitly Declaring Parameters to Use for a `SimpleJdbcCall`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Earlier in this chapter, we described how parameters are deduced from metadata, but you can declare them
explicitly if you wish. You can do so by creating and configuring `SimpleJdbcCall` with
2015-03-03 18:38:01 +08:00
the `declareParameters` method, which takes a variable number of `SqlParameter` objects
2019-03-05 20:08:34 +08:00
as input. See the <<jdbc-params, next section>> for details on how to define an `SqlParameter`.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
NOTE: Explicit declarations are necessary if the database you use is not a Spring-supported
database. Currently, Spring supports metadata lookup of stored procedure calls for the
2015-03-03 18:38:01 +08:00
following databases: Apache Derby, DB2, MySQL, Microsoft SQL Server, Oracle, and Sybase.
2015-07-30 02:35:59 +08:00
We also support metadata lookup of stored functions for MySQL, Microsoft SQL Server,
2015-03-03 18:38:01 +08:00
and Oracle.
2018-09-05 23:15:53 +08:00
You can opt to explicitly declare one, some, or all of the parameters. The parameter
metadata is still used where you do not explicitly declare parameters. To bypass all
processing of metadata lookups for potential parameters and use only the declared
parameters, you can call the method `withoutProcedureColumnMetaDataAccess` as part of the
2015-03-03 18:38:01 +08:00
declaration. Suppose that you have two or more different call signatures declared for a
2018-09-05 23:15:53 +08:00
database function. In this case, you call `useInParameterNames` to specify the list
2015-03-03 18:38:01 +08:00
of IN parameter names to include for a given signature.
2018-09-05 23:15:53 +08:00
The following example shows a fully declared procedure call and uses the information from
the preceding example:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private SimpleJdbcCall procReadActor;
public void setDataSource(DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.setResultsMapCaseInsensitive(true);
this.procReadActor = new SimpleJdbcCall(jdbcTemplate)
.withProcedureName("read_actor")
.withoutProcedureColumnMetaDataAccess()
.useInParameterNames("in_id")
.declareParameters(
new SqlParameter("in_id", Types.NUMERIC),
new SqlOutParameter("out_first_name", Types.VARCHAR),
new SqlOutParameter("out_last_name", Types.VARCHAR),
new SqlOutParameter("out_birth_date", Types.DATE)
);
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val procReadActor = SimpleJdbcCall(JdbcTemplate(dataSource).apply {
isResultsMapCaseInsensitive = true
}).withProcedureName("read_actor")
.withoutProcedureColumnMetaDataAccess()
.useInParameterNames("in_id")
.declareParameters(
SqlParameter("in_id", Types.NUMERIC),
SqlOutParameter("out_first_name", Types.VARCHAR),
SqlOutParameter("out_last_name", Types.VARCHAR),
SqlOutParameter("out_birth_date", Types.DATE)
)
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The execution and end results of the two examples are the same. The second example specifies all
2015-03-03 18:38:01 +08:00
details explicitly rather than relying on metadata.
[[jdbc-params]]
2018-09-05 23:15:53 +08:00
==== How to Define `SqlParameters`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
To define a parameter for the `SimpleJdbc` classes and also for the RDBMS operations
classes (covered in <<jdbc-object>>) you can use `SqlParameter` or one of its subclasses.
To do so, you typically specify the parameter name and SQL type in the constructor. The SQL type
is specified by using the `java.sql.Types` constants. Earlier in this chapter, we saw declarations
similar to the following:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
new SqlParameter("in_id", Types.NUMERIC),
2018-05-29 20:01:45 +08:00
new SqlOutParameter("out_first_name", Types.VARCHAR),
2015-03-03 18:38:01 +08:00
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
SqlParameter("in_id", Types.NUMERIC),
SqlOutParameter("out_first_name", Types.VARCHAR),
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The first line with the `SqlParameter` declares an IN parameter. You can use IN parameters
for both stored procedure calls and for queries by using the `SqlQuery` and its
subclasses (covered in <<jdbc-SqlQuery>>).
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The second line (with the `SqlOutParameter`) declares an `out` parameter to be used in a
stored procedure call. There is also an `SqlInOutParameter` for `InOut` parameters
(parameters that provide an IN value to the procedure and that also return a value).
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
NOTE: Only parameters declared as `SqlParameter` and `SqlInOutParameter` are used to
provide input values. This is different from the `StoredProcedure` class, which (for
backwards compatibility reasons) lets input values be provided for parameters
2015-03-03 18:38:01 +08:00
declared as `SqlOutParameter`.
For IN parameters, in addition to the name and the SQL type, you can specify a scale for
numeric data or a type name for custom database types. For `out` parameters, you can
provide a `RowMapper` to handle mapping of rows returned from a `REF` cursor. Another
option is to specify an `SqlReturnType` that provides an opportunity to define
customized handling of the return values.
[[jdbc-simple-jdbc-call-3]]
2018-09-05 23:15:53 +08:00
==== Calling a Stored Function by Using `SimpleJdbcCall`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
You can call a stored function in almost the same way as you call a stored procedure, except
2015-03-03 18:38:01 +08:00
that you provide a function name rather than a procedure name. You use the
2018-09-05 23:15:53 +08:00
`withFunctionName` method as part of the configuration to indicate that you want to make
2015-03-03 18:38:01 +08:00
a call to a function, and the corresponding string for a function call is generated. A
2018-09-05 23:15:53 +08:00
specialized execute call (`executeFunction`) is used to execute the function, and it
2015-03-03 18:38:01 +08:00
returns the function return value as an object of a specified type, which means you do
not have to retrieve the return value from the results map. A similar convenience method
2018-09-05 23:15:53 +08:00
(named `executeObject`) is also available for stored procedures that have only one `out`
parameter. The following example (for MySQL) is based on a stored function named `get_actor_name`
that returns an actor's full name:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,sql,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
CREATE FUNCTION get_actor_name (in_id INTEGER)
RETURNS VARCHAR(200) READS SQL DATA
BEGIN
DECLARE out_name VARCHAR(200);
SELECT concat(first_name, ' ', last_name)
INTO out_name
FROM t_actor where id = in_id;
RETURN out_name;
END;
----
2018-09-05 23:15:53 +08:00
To call this function, we again create a `SimpleJdbcCall` in the initialization method,
as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private JdbcTemplate jdbcTemplate;
private SimpleJdbcCall funcGetActorName;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.setResultsMapCaseInsensitive(true);
this.funcGetActorName = new SimpleJdbcCall(jdbcTemplate)
.withFunctionName("get_actor_name");
}
public String getActorName(Long id) {
SqlParameterSource in = new MapSqlParameterSource()
.addValue("in_id", id);
String name = funcGetActorName.executeFunction(String.class, in);
return name;
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val jdbcTemplate = JdbcTemplate(dataSource).apply {
isResultsMapCaseInsensitive = true
}
private val funcGetActorName = SimpleJdbcCall(jdbcTemplate)
.withFunctionName("get_actor_name")
fun getActorName(id: Long): String {
val source = MapSqlParameterSource().addValue("in_id", id)
return funcGetActorName.executeFunction(String::class.java, source)
}
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
2018-10-25 21:15:58 +08:00
The `executeFunction` method used returns a `String` that contains the return value from the
function call.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc-simple-jdbc-call-4]]
2018-09-05 23:15:53 +08:00
==== Returning a `ResultSet` or REF Cursor from a `SimpleJdbcCall`
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Calling a stored procedure or function that returns a result set is a bit tricky. Some
2018-09-05 23:15:53 +08:00
databases return result sets during the JDBC results processing, while others require an
2015-03-03 18:38:01 +08:00
explicitly registered `out` parameter of a specific type. Both approaches need
additional processing to loop over the result set and process the returned rows. With
2018-09-05 23:15:53 +08:00
the `SimpleJdbcCall`, you can use the `returningResultSet` method and declare a `RowMapper`
implementation to be used for a specific parameter. If the result set is
2015-03-03 18:38:01 +08:00
returned during the results processing, there are no names defined, so the returned
2018-09-05 23:15:53 +08:00
results must match the order in which you declare the `RowMapper`
2015-03-03 18:38:01 +08:00
implementations. The name specified is still used to store the processed list of results
2018-09-05 23:15:53 +08:00
in the results map that is returned from the `execute` statement.
2015-03-03 18:38:01 +08:00
2018-10-25 21:15:58 +08:00
The next example (for MySQL) uses a stored procedure that takes no IN parameters and returns
all rows from the `t_actor` table:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,sql,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
CREATE PROCEDURE read_all_actors()
BEGIN
SELECT a.id, a.first_name, a.last_name, a.birth_date FROM t_actor a;
END;
----
2018-10-25 21:15:58 +08:00
To call this procedure, you can declare the `RowMapper`. Because the class to which you want
to map follows the JavaBean rules, you can use a `BeanPropertyRowMapper` that is created by
passing in the required class to map to in the `newInstance` method.
2018-09-05 23:15:53 +08:00
The following example shows how to do so:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class JdbcActorDao implements ActorDao {
private SimpleJdbcCall procReadAllActors;
public void setDataSource(DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.setResultsMapCaseInsensitive(true);
this.procReadAllActors = new SimpleJdbcCall(jdbcTemplate)
.withProcedureName("read_all_actors")
.returningResultSet("actors",
2015-05-05 20:28:49 +08:00
BeanPropertyRowMapper.newInstance(Actor.class));
2015-03-03 18:38:01 +08:00
}
public List getActorsList() {
Map m = procReadAllActors.execute(new HashMap<String, Object>(0));
return (List) m.get("actors");
}
// ... additional methods
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class JdbcActorDao(dataSource: DataSource) : ActorDao {
private val procReadAllActors = SimpleJdbcCall(JdbcTemplate(dataSource).apply {
isResultsMapCaseInsensitive = true
}).withProcedureName("read_all_actors")
.returningResultSet("actors",
BeanPropertyRowMapper.newInstance(Actor::class.java))
fun getActorsList(): List<Actor> {
val m = procReadAllActors.execute(mapOf<String, Any>())
return m["actors"] as List<Actor>
}
// ... additional methods
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The `execute` call passes in an empty `Map`, because this call does not take any parameters.
The list of actors is then retrieved from the results map and returned to the caller.
2015-03-03 18:38:01 +08:00
[[jdbc-object]]
2018-09-05 23:15:53 +08:00
=== Modeling JDBC Operations as Java Objects
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The `org.springframework.jdbc.object` package contains classes that let you access
2015-03-03 18:38:01 +08:00
the database in a more object-oriented manner. As an example, you can execute queries
2018-09-05 23:15:53 +08:00
and get the results back as a list that contains business objects with the relational
column data mapped to the properties of the business object. You can also run stored
2015-03-03 18:38:01 +08:00
procedures and run update, delete, and insert statements.
[NOTE]
====
Many Spring developers believe that the various RDBMS operation classes described below
(with the exception of the <<jdbc-StoredProcedure, `StoredProcedure`>> class) can often
2018-09-05 23:15:53 +08:00
be replaced with straight `JdbcTemplate` calls. Often, it is simpler to write a DAO
method that calls a method on a `JdbcTemplate` directly (as opposed to
2015-03-03 18:38:01 +08:00
encapsulating a query as a full-blown class).
However, if you are getting measurable value from using the RDBMS operation classes,
2018-09-05 23:15:53 +08:00
you should continue to use these classes.
2015-03-03 18:38:01 +08:00
====
[[jdbc-SqlQuery]]
2018-09-05 23:15:53 +08:00
==== Understanding `SqlQuery`
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
`SqlQuery` is a reusable, thread-safe class that encapsulates an SQL query. Subclasses
2015-03-03 18:38:01 +08:00
must implement the `newRowMapper(..)` method to provide a `RowMapper` instance that can
create one object per row obtained from iterating over the `ResultSet` that is created
2018-09-05 23:15:53 +08:00
during the execution of the query. The `SqlQuery` class is rarely used directly, because
2015-03-03 18:38:01 +08:00
the `MappingSqlQuery` subclass provides a much more convenient implementation for
mapping rows to Java classes. Other implementations that extend `SqlQuery` are
`MappingSqlQueryWithParameters` and `UpdatableSqlQuery`.
[[jdbc-MappingSqlQuery]]
2018-09-05 23:15:53 +08:00
==== Using `MappingSqlQuery`
2015-03-03 18:38:01 +08:00
`MappingSqlQuery` is a reusable query in which concrete subclasses must implement the
abstract `mapRow(..)` method to convert each row of the supplied `ResultSet` into an
object of the type specified. The following example shows a custom query that maps the
2018-09-05 23:15:53 +08:00
data from the `t_actor` relation to an instance of the `Actor` class:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class ActorMappingQuery extends MappingSqlQuery<Actor> {
public ActorMappingQuery(DataSource ds) {
super(ds, "select id, first_name, last_name from t_actor where id = ?");
2018-05-16 06:46:57 +08:00
declareParameter(new SqlParameter("id", Types.INTEGER));
2015-03-03 18:38:01 +08:00
compile();
}
@Override
protected Actor mapRow(ResultSet rs, int rowNumber) throws SQLException {
Actor actor = new Actor();
actor.setId(rs.getLong("id"));
actor.setFirstName(rs.getString("first_name"));
actor.setLastName(rs.getString("last_name"));
return actor;
}
2019-09-03 22:03:28 +08:00
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class ActorMappingQuery(ds: DataSource) : MappingSqlQuery<Actor>(ds, "select id, first_name, last_name from t_actor where id = ?") {
init {
declareParameter(SqlParameter("id", Types.INTEGER))
compile()
}
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
override fun mapRow(rs: ResultSet, rowNumber: Int) = Actor(
rs.getLong("id"),
rs.getString("first_name"),
rs.getString("last_name")
)
2015-03-03 18:38:01 +08:00
}
2019-09-03 22:03:28 +08:00
2015-03-03 18:38:01 +08:00
----
The class extends `MappingSqlQuery` parameterized with the `Actor` type. The constructor
2018-09-05 23:15:53 +08:00
for this customer query takes a `DataSource` as the only parameter. In this
constructor, you can call the constructor on the superclass with the `DataSource` and the SQL
that should be executed to retrieve the rows for this query. This SQL is used to
create a `PreparedStatement`, so it may contain placeholders for any parameters to be
passed in during execution. You must declare each parameter by using the `declareParameter`
method passing in an `SqlParameter`. The `SqlParameter` takes a name, and the JDBC type
as defined in `java.sql.Types`. After you define all parameters, you can call the
`compile()` method so that the statement can be prepared and later run. This class is
thread-safe after it is compiled, so, as long as these instances are created when the DAO
is initialized, they can be kept as instance variables and be reused. The following
example shows how to define such a class:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
private ActorMappingQuery actorMappingQuery;
@Autowired
public void setDataSource(DataSource dataSource) {
this.actorMappingQuery = new ActorMappingQuery(dataSource);
}
public Customer getCustomer(Long id) {
return actorMappingQuery.findObject(id);
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
private val actorMappingQuery = ActorMappingQuery(dataSource)
fun getCustomer(id: Long) = actorMappingQuery.findObject(id)
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The method in the preceding example retrieves the customer with the `id` that is passed in as the
only parameter. Since we want only one object to be returned, we call the `findObject` convenience
method with the `id` as the parameter. If we had instead a query that returned a
list of objects and took additional parameters, we would use one of the `execute`
methods that takes an array of parameter values passed in as varargs. The following
example shows such a method:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public List<Actor> searchForActors(int age, String namePattern) {
List<Actor> actors = actorSearchMappingQuery.execute(age, namePattern);
return actors;
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
fun searchForActors(age: Int, namePattern: String) =
actorSearchMappingQuery.execute(age, namePattern)
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc-SqlUpdate]]
2018-09-05 23:15:53 +08:00
==== Using `SqlUpdate`
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The `SqlUpdate` class encapsulates an SQL update. As with a query, an update object is
reusable, and, as with all `RdbmsOperation` classes, an update can have parameters and is
2015-03-03 18:38:01 +08:00
defined in SQL. This class provides a number of `update(..)` methods analogous to the
`execute(..)` methods of query objects. The `SQLUpdate` class is concrete. It can be
2018-09-05 23:15:53 +08:00
subclassed -- for example, to add a custom update method.
However, you do not have to subclass the `SqlUpdate`
class, since it can easily be parameterized by setting SQL and declaring parameters.
The following example creates a custom update method named `execute`:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
import java.sql.Types;
import javax.sql.DataSource;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;
public class UpdateCreditRating extends SqlUpdate {
public UpdateCreditRating(DataSource ds) {
setDataSource(ds);
setSql("update customer set credit_rating = ? where id = ?");
declareParameter(new SqlParameter("creditRating", Types.NUMERIC));
declareParameter(new SqlParameter("id", Types.NUMERIC));
compile();
}
/**
* @param id for the Customer to be updated
* @param rating the new value for credit rating
* @return number of rows updated
*/
public int execute(int id, int rating) {
return update(rating, id);
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
import java.sql.Types
import javax.sql.DataSource
import org.springframework.jdbc.core.SqlParameter
import org.springframework.jdbc.`object`.SqlUpdate
class UpdateCreditRating(ds: DataSource) : SqlUpdate() {
init {
setDataSource(ds)
sql = "update customer set credit_rating = ? where id = ?"
declareParameter(SqlParameter("creditRating", Types.NUMERIC))
declareParameter(SqlParameter("id", Types.NUMERIC))
compile()
}
/**
* @param id for the Customer to be updated
* @param rating the new value for credit rating
* @return number of rows updated
*/
fun execute(id: Int, rating: Int): Int {
return update(rating, id)
}
}
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[jdbc-StoredProcedure]]
2018-09-05 23:15:53 +08:00
==== Using `StoredProcedure`
2015-03-03 18:38:01 +08:00
The `StoredProcedure` class is a superclass for object abstractions of RDBMS stored
procedures. This class is `abstract`, and its various `execute(..)` methods have
`protected` access, preventing use other than through a subclass that offers tighter
typing.
2018-09-05 23:15:53 +08:00
The inherited `sql` property is the name of the stored procedure in the RDBMS.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
To define a parameter for the `StoredProcedure` class, you can use an `SqlParameter` or one
of its subclasses. You must specify the parameter name and SQL type in the constructor,
as the following code snippet shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
new SqlParameter("in_id", Types.NUMERIC),
2018-05-29 20:01:45 +08:00
new SqlOutParameter("out_first_name", Types.VARCHAR),
2015-03-03 18:38:01 +08:00
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
SqlParameter("in_id", Types.NUMERIC),
SqlOutParameter("out_first_name", Types.VARCHAR),
----
2018-09-05 23:15:53 +08:00
The SQL type is specified using the `java.sql.Types` constants.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The first line (with the `SqlParameter`) declares an IN parameter. You can use IN parameters
both for stored procedure calls and for queries using the `SqlQuery` and its
subclasses (covered in <<jdbc-SqlQuery>>).
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The second line (with the `SqlOutParameter`) declares an `out` parameter to be used in the
stored procedure call. There is also an `SqlInOutParameter` for `InOut` parameters
(parameters that provide an `in` value to the procedure and that also return a value).
2015-03-03 18:38:01 +08:00
2018-05-29 20:01:45 +08:00
For `in` parameters, in addition to the name and the SQL type, you can specify a
2018-09-05 23:15:53 +08:00
scale for numeric data or a type name for custom database types. For `out` parameters,
you can provide a `RowMapper` to handle mapping of rows returned from a `REF` cursor.
Another option is to specify an `SqlReturnType` that lets you define customized
2015-03-03 18:38:01 +08:00
handling of the return values.
2018-09-05 23:15:53 +08:00
The next example of a simple DAO uses a `StoredProcedure` to call a function
(`sysdate()`), which comes with any Oracle database. To use the stored procedure
functionality, you have to create a class that extends `StoredProcedure`. In this
example, the `StoredProcedure` class is an inner class. However, if you need to reuse the
`StoredProcedure`, you can declare it as a top-level class. This example has no input
parameters, but an output parameter is declared as a date type by using the
`SqlOutParameter` class. The `execute()` method runs the procedure and extracts the
2015-03-03 18:38:01 +08:00
returned date from the results `Map`. The results `Map` has an entry for each declared
2018-09-27 02:17:07 +08:00
output parameter (in this case, only one) by using the parameter name as the key.
2018-09-05 23:15:53 +08:00
The following listing shows our custom StoredProcedure class:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.object.StoredProcedure;
public class StoredProcedureDao {
private GetSysdateProcedure getSysdate;
@Autowired
public void init(DataSource dataSource) {
this.getSysdate = new GetSysdateProcedure(dataSource);
}
public Date getSysdate() {
return getSysdate.execute();
}
private class GetSysdateProcedure extends StoredProcedure {
2019-09-03 22:03:28 +08:00
private static final String SQL = "sysdate";
public GetSysdateProcedure(DataSource dataSource) {
setDataSource(dataSource);
setFunction(true);
setSql(SQL);
declareParameter(new SqlOutParameter("date", Types.DATE));
compile();
}
public Date execute() {
// the 'sysdate' sproc has no input parameters, so an empty Map is supplied...
Map<String, Object> results = execute(new HashMap<String, Object>());
Date sysdate = (Date) results.get("date");
return sysdate;
}
}
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
import java.sql.Types
import java.util.Date
import java.util.Map
import javax.sql.DataSource
import org.springframework.jdbc.core.SqlOutParameter
import org.springframework.jdbc.object.StoredProcedure
class StoredProcedureDao(dataSource: DataSource) {
private val SQL = "sysdate"
private val getSysdate = GetSysdateProcedure(dataSource)
val sysdate: Date
get() = getSysdate.execute()
private inner class GetSysdateProcedure(dataSource: DataSource) : StoredProcedure() {
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
init {
setDataSource(dataSource)
isFunction = true
sql = SQL
declareParameter(SqlOutParameter("date", Types.DATE))
compile()
2015-03-03 18:38:01 +08:00
}
2019-09-03 22:03:28 +08:00
fun execute(): Date {
2015-03-03 18:38:01 +08:00
// the 'sysdate' sproc has no input parameters, so an empty Map is supplied...
2019-09-03 22:03:28 +08:00
val results = execute(mutableMapOf<String, Any>())
return results["date"] as Date
2015-03-03 18:38:01 +08:00
}
}
}
----
The following example of a `StoredProcedure` has two output parameters (in this case,
2018-09-05 23:15:53 +08:00
Oracle REF cursors):
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
2018-05-16 06:46:57 +08:00
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
2015-03-03 18:38:01 +08:00
import oracle.jdbc.OracleTypes;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.object.StoredProcedure;
public class TitlesAndGenresStoredProcedure extends StoredProcedure {
private static final String SPROC_NAME = "AllTitlesAndGenres";
public TitlesAndGenresStoredProcedure(DataSource dataSource) {
super(dataSource, SPROC_NAME);
declareParameter(new SqlOutParameter("titles", OracleTypes.CURSOR, new TitleMapper()));
declareParameter(new SqlOutParameter("genres", OracleTypes.CURSOR, new GenreMapper()));
compile();
}
public Map<String, Object> execute() {
// again, this sproc has no input parameters, so an empty Map is supplied
return super.execute(new HashMap<String, Object>());
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
import java.util.HashMap
import javax.sql.DataSource
import oracle.jdbc.OracleTypes
import org.springframework.jdbc.core.SqlOutParameter
import org.springframework.jdbc.`object`.StoredProcedure
class TitlesAndGenresStoredProcedure(dataSource: DataSource) : StoredProcedure(dataSource, SPROC_NAME) {
companion object {
private const val SPROC_NAME = "AllTitlesAndGenres"
}
init {
declareParameter(SqlOutParameter("titles", OracleTypes.CURSOR, TitleMapper()))
declareParameter(SqlOutParameter("genres", OracleTypes.CURSOR, GenreMapper()))
compile()
}
fun execute(): Map<String, Any> {
// again, this sproc has no input parameters, so an empty Map is supplied
return super.execute(HashMap<String, Any>())
}
}
----
2015-03-03 18:38:01 +08:00
Notice how the overloaded variants of the `declareParameter(..)` method that have been
used in the `TitlesAndGenresStoredProcedure` constructor are passed `RowMapper`
2018-09-05 23:15:53 +08:00
implementation instances. This is a very convenient and powerful way to reuse existing
functionality. The next two examples provide code for the two `RowMapper` implementations.
2015-03-03 18:38:01 +08:00
The `TitleMapper` class maps a `ResultSet` to a `Title` domain object for each row in
2018-09-05 23:15:53 +08:00
the supplied `ResultSet`, as follows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
import java.sql.ResultSet;
import java.sql.SQLException;
import com.foo.domain.Title;
2018-05-16 06:46:57 +08:00
import org.springframework.jdbc.core.RowMapper;
2015-03-03 18:38:01 +08:00
public final class TitleMapper implements RowMapper<Title> {
public Title mapRow(ResultSet rs, int rowNum) throws SQLException {
Title title = new Title();
title.setId(rs.getLong("id"));
title.setName(rs.getString("name"));
return title;
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
import java.sql.ResultSet
import com.foo.domain.Title
import org.springframework.jdbc.core.RowMapper
class TitleMapper : RowMapper<Title> {
override fun mapRow(rs: ResultSet, rowNum: Int) =
Title(rs.getLong("id"), rs.getString("name"))
}
----
2015-03-03 18:38:01 +08:00
The `GenreMapper` class maps a `ResultSet` to a `Genre` domain object for each row in
2018-09-05 23:15:53 +08:00
the supplied `ResultSet`, as follows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
import java.sql.ResultSet;
import java.sql.SQLException;
import com.foo.domain.Genre;
2018-05-16 06:46:57 +08:00
import org.springframework.jdbc.core.RowMapper;
2015-03-03 18:38:01 +08:00
public final class GenreMapper implements RowMapper<Genre> {
public Genre mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Genre(rs.getString("name"));
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
import java.sql.ResultSet
import com.foo.domain.Genre
import org.springframework.jdbc.core.RowMapper
class GenreMapper : RowMapper<Genre> {
override fun mapRow(rs: ResultSet, rowNum: Int): Genre {
return Genre(rs.getString("name"))
}
}
----
2015-03-03 18:38:01 +08:00
To pass parameters to a stored procedure that has one or more input parameters in its
definition in the RDBMS, you can code a strongly typed `execute(..)` method that would
2018-09-05 23:15:53 +08:00
delegate to the untyped `execute(Map)` method in the superclass, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
2018-05-16 06:46:57 +08:00
import javax.sql.DataSource;
import oracle.jdbc.OracleTypes;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;
2015-03-03 18:38:01 +08:00
public class TitlesAfterDateStoredProcedure extends StoredProcedure {
private static final String SPROC_NAME = "TitlesAfterDate";
private static final String CUTOFF_DATE_PARAM = "cutoffDate";
public TitlesAfterDateStoredProcedure(DataSource dataSource) {
super(dataSource, SPROC_NAME);
declareParameter(new SqlParameter(CUTOFF_DATE_PARAM, Types.DATE);
declareParameter(new SqlOutParameter("titles", OracleTypes.CURSOR, new TitleMapper()));
compile();
}
public Map<String, Object> execute(Date cutoffDate) {
Map<String, Object> inputs = new HashMap<String, Object>();
inputs.put(CUTOFF_DATE_PARAM, cutoffDate);
return super.execute(inputs);
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
import java.sql.Types
import java.util.Date
import javax.sql.DataSource
import oracle.jdbc.OracleTypes
import org.springframework.jdbc.core.SqlOutParameter
import org.springframework.jdbc.core.SqlParameter
import org.springframework.jdbc.`object`.StoredProcedure
class TitlesAfterDateStoredProcedure(dataSource: DataSource) : StoredProcedure(dataSource, SPROC_NAME) {
companion object {
private const val SPROC_NAME = "TitlesAfterDate"
private const val CUTOFF_DATE_PARAM = "cutoffDate"
}
init {
declareParameter(SqlParameter(CUTOFF_DATE_PARAM, Types.DATE))
declareParameter(SqlOutParameter("titles", OracleTypes.CURSOR, TitleMapper()))
compile()
}
fun execute(cutoffDate: Date) = super.execute(
mapOf<String, Any>(CUTOFF_DATE_PARAM to cutoffDate))
}
----
2015-03-03 18:38:01 +08:00
[[jdbc-parameter-handling]]
2018-09-05 23:15:53 +08:00
=== Common Problems with Parameter and Data Value Handling
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Common problems with parameters and data values exist in the different approaches
2018-09-05 23:15:53 +08:00
provided by Spring Framework's JDBC support. This section covers how to address them.
2015-03-03 18:38:01 +08:00
[[jdbc-type-information]]
2018-09-05 23:15:53 +08:00
==== Providing SQL Type Information for Parameters
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Usually, Spring determines the SQL type of the parameters based on the type of parameter
2015-03-03 18:38:01 +08:00
passed in. It is possible to explicitly provide the SQL type to be used when setting
2018-09-05 23:15:53 +08:00
parameter values. This is sometimes necessary to correctly set `NULL` values.
2015-03-03 18:38:01 +08:00
You can provide SQL type information in several ways:
* Many update and query methods of the `JdbcTemplate` take an additional parameter in
the form of an `int` array. This array is used to indicate the SQL type of the
2018-09-05 23:15:53 +08:00
corresponding parameter by using constant values from the `java.sql.Types` class. Provide
2015-03-03 18:38:01 +08:00
one entry for each parameter.
* You can use the `SqlParameterValue` class to wrap the parameter value that needs this
2018-09-05 23:15:53 +08:00
additional information. To do so, create a new instance for each value and pass in the SQL type
and the parameter value in the constructor. You can also provide an optional scale
2015-03-03 18:38:01 +08:00
parameter for numeric values.
2018-09-05 23:15:53 +08:00
* For methods that work with named parameters, you can use the `SqlParameterSource` classes,
2015-03-03 18:38:01 +08:00
`BeanPropertySqlParameterSource` or `MapSqlParameterSource`. They both have methods
for registering the SQL type for any of the named parameter values.
[[jdbc-lob]]
==== Handling BLOB and CLOB objects
2017-10-19 02:24:17 +08:00
2015-05-06 02:24:48 +08:00
You can store images, other binary data, and large chunks of text in the database. These
large objects are called BLOBs (Binary Large OBject) for binary data and CLOBs (Character
2018-09-05 23:15:53 +08:00
Large OBject) for character data. In Spring, you can handle these large objects by using
2015-05-06 02:24:48 +08:00
the `JdbcTemplate` directly and also when using the higher abstractions provided by RDBMS
Objects and the `SimpleJdbc` classes. All of these approaches use an implementation of
2018-09-05 23:15:53 +08:00
the `LobHandler` interface for the actual management of the LOB (Large OBject) data.
2015-05-06 02:24:48 +08:00
`LobHandler` provides access to a `LobCreator` class, through the `getLobCreator` method,
2018-09-05 23:15:53 +08:00
that is used for creating new LOB objects to be inserted.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
`LobCreator` and `LobHandler` provide the following support for LOB input and output:
2015-03-03 18:38:01 +08:00
* BLOB
2018-09-05 23:15:53 +08:00
** `byte[]`: `getBlobAsBytes` and `setBlobAsBytes`
** `InputStream`: `getBlobAsBinaryStream` and `setBlobAsBinaryStream`
2015-03-03 18:38:01 +08:00
* CLOB
2018-09-05 23:15:53 +08:00
** `String`: `getClobAsString` and `setClobAsString`
** `InputStream`: `getClobAsAsciiStream` and `setClobAsAsciiStream`
** `Reader`: `getClobAsCharacterStream` and `setClobAsCharacterStream`
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The next example shows how to create and insert a BLOB. Later we show how to read
2015-03-03 18:38:01 +08:00
it back from the database.
This example uses a `JdbcTemplate` and an implementation of the
2015-05-05 20:21:06 +08:00
`AbstractLobCreatingPreparedStatementCallback`. It implements one method,
2018-09-05 23:15:53 +08:00
`setValues`. This method provides a `LobCreator` that we use to set the values for the
2015-03-03 18:38:01 +08:00
LOB columns in your SQL insert statement.
2018-09-05 23:15:53 +08:00
For this example, we assume that there is a variable, `lobHandler`, that is already
2015-03-03 18:38:01 +08:00
set to an instance of a `DefaultLobHandler`. You typically set this value through
dependency injection.
2018-09-05 23:15:53 +08:00
The following example shows how to create and insert a BLOB:
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
final File blobIn = new File("spring2004.jpg");
final InputStream blobIs = new FileInputStream(blobIn);
final File clobIn = new File("large.txt");
final InputStream clobIs = new FileInputStream(clobIn);
final InputStreamReader clobReader = new InputStreamReader(clobIs);
2018-05-16 06:46:57 +08:00
2015-03-03 18:38:01 +08:00
jdbcTemplate.execute(
"INSERT INTO lob_table (id, a_clob, a_blob) VALUES (?, ?, ?)",
2019-09-03 22:03:28 +08:00
new AbstractLobCreatingPreparedStatementCallback(lobHandler) { // <1>
2015-03-03 18:38:01 +08:00
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
ps.setLong(1, 1L);
2019-09-03 22:03:28 +08:00
lobCreator.setClobAsCharacterStream(ps, 2, clobReader, (int)clobIn.length()); // <2>
lobCreator.setBlobAsBinaryStream(ps, 3, blobIs, (int)blobIn.length()); // <3>
2015-03-03 18:38:01 +08:00
}
}
);
2018-05-16 06:46:57 +08:00
2015-03-03 18:38:01 +08:00
blobIs.close();
clobReader.close();
----
2019-09-03 22:03:28 +08:00
<1> Pass in the `lobHandler` that (in this example) is a plain `DefaultLobHandler`.
<2> Using the method `setClobAsCharacterStream` to pass in the contents of the CLOB.
<3> Using the method `setBlobAsBinaryStream` to pass in the contents of the BLOB.
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val blobIn = File("spring2004.jpg")
val blobIs = FileInputStream(blobIn)
val clobIn = File("large.txt")
val clobIs = FileInputStream(clobIn)
val clobReader = InputStreamReader(clobIs)
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
jdbcTemplate.execute(
"INSERT INTO lob_table (id, a_clob, a_blob) VALUES (?, ?, ?)",
object: AbstractLobCreatingPreparedStatementCallback(lobHandler) { // <1>
override fun setValues(ps: PreparedStatement, lobCreator: LobCreator) {
ps.setLong(1, 1L)
lobCreator.setClobAsCharacterStream(ps, 2, clobReader, clobIn.length().toInt()) // <2>
lobCreator.setBlobAsBinaryStream(ps, 3, blobIs, blobIn.length().toInt()) // <3>
}
}
)
blobIs.close()
clobReader.close()
----
2018-09-05 23:15:53 +08:00
<1> Pass in the `lobHandler` that (in this example) is a plain `DefaultLobHandler`.
<2> Using the method `setClobAsCharacterStream` to pass in the contents of the CLOB.
<3> Using the method `setBlobAsBinaryStream` to pass in the contents of the BLOB.
2018-11-27 06:15:55 +08:00
2015-05-06 02:24:48 +08:00
[NOTE]
====
If you invoke the `setBlobAsBinaryStream`, `setClobAsAsciiStream`, or
`setClobAsCharacterStream` method on the `LobCreator` returned from
`DefaultLobHandler.getLobCreator()`, you can optionally specify a negative value for the
`contentLength` argument. If the specified content length is negative, the
2018-09-05 23:15:53 +08:00
`DefaultLobHandler` uses the JDBC 4.0 variants of the set-stream methods without a
length parameter. Otherwise, it passes the specified length on to the driver.
2015-05-06 02:24:48 +08:00
2018-09-05 23:15:53 +08:00
See the documentation for the JDBC driver you use to verify that it supports streaming a
2015-05-06 02:24:48 +08:00
LOB without providing the content length.
====
2018-09-05 23:15:53 +08:00
Now it is time to read the LOB data from the database. Again, you use a `JdbcTemplate`
2015-05-05 20:21:06 +08:00
with the same instance variable `lobHandler` and a reference to a `DefaultLobHandler`.
2018-09-05 23:15:53 +08:00
The following example shows how to do so:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
List<Map<String, Object>> l = jdbcTemplate.query("select id, a_clob, a_blob from lob_table",
new RowMapper<Map<String, Object>>() {
public Map<String, Object> mapRow(ResultSet rs, int i) throws SQLException {
Map<String, Object> results = new HashMap<String, Object>();
2019-09-03 22:03:28 +08:00
String clobText = lobHandler.getClobAsString(rs, "a_clob"); // <1>
2018-05-30 17:10:29 +08:00
results.put("CLOB", clobText);
2019-09-03 22:03:28 +08:00
byte[] blobBytes = lobHandler.getBlobAsBytes(rs, "a_blob"); // <2>
2018-05-30 17:10:29 +08:00
results.put("BLOB", blobBytes);
return results;
}
});
2015-03-03 18:38:01 +08:00
----
2019-09-03 22:03:28 +08:00
<1> Using the method `getClobAsString` to retrieve the contents of the CLOB.
<2> Using the method `getBlobAsBytes` to retrieve the contents of the BLOB.
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val l = jdbcTemplate.query("select id, a_clob, a_blob from lob_table") { rs, _ ->
val clobText = lobHandler.getClobAsString(rs, "a_clob") // <1>
val blobBytes = lobHandler.getBlobAsBytes(rs, "a_blob") // <2>
mapOf("CLOB" to clobText, "BLOB" to blobBytes)
}
----
2018-09-05 23:15:53 +08:00
<1> Using the method `getClobAsString` to retrieve the contents of the CLOB.
<2> Using the method `getBlobAsBytes` to retrieve the contents of the BLOB.
2015-03-03 18:38:01 +08:00
[[jdbc-in-clause]]
2018-09-05 23:15:53 +08:00
==== Passing in Lists of Values for IN Clause
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
The SQL standard allows for selecting rows based on an expression that includes a
variable list of values. A typical example would be `select * from T_ACTOR where id in
(1, 2, 3)`. This variable list is not directly supported for prepared statements by the
2018-09-05 23:15:53 +08:00
JDBC standard. You cannot declare a variable number of placeholders. You need a number
2015-03-03 18:38:01 +08:00
of variations with the desired number of placeholders prepared, or you need to generate
the SQL string dynamically once you know how many placeholders are required. The named
parameter support provided in the `NamedParameterJdbcTemplate` and `JdbcTemplate` takes
2018-09-05 23:15:53 +08:00
the latter approach. You can pass in the values as a `java.util.List` of primitive objects. This
list is used to insert the required placeholders and pass in the values during
2015-03-03 18:38:01 +08:00
statement execution.
2018-09-05 23:15:53 +08:00
NOTE: Be careful when passing in many values. The JDBC standard does not guarantee that you
2015-03-03 18:38:01 +08:00
can use more than 100 values for an `in` expression list. Various databases exceed this
2018-09-05 23:15:53 +08:00
number, but they usually have a hard limit for how many values are allowed. For example, Oracle's
2015-03-03 18:38:01 +08:00
limit is 1000.
In addition to the primitive values in the value list, you can create a `java.util.List`
2018-09-05 23:15:53 +08:00
of object arrays. This list can support multiple expressions being defined for the `in`
clause, such as `select * from T_ACTOR where (id, last_name) in \((1, 'Johnson'), (2,
'Harrop'\))`. This, of course, requires that your database supports this syntax.
2015-03-03 18:38:01 +08:00
[[jdbc-complex-types]]
2018-09-05 23:15:53 +08:00
==== Handling Complex Types for Stored Procedure Calls
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
When you call stored procedures, you can sometimes use complex types specific to the
2015-03-03 18:38:01 +08:00
database. To accommodate these types, Spring provides a `SqlReturnType` for handling
them when they are returned from the stored procedure call and `SqlTypeValue` when they
are passed in as a parameter to the stored procedure.
2020-03-17 21:53:15 +08:00
The `SqlReturnType` interface has a single method (named `getTypeValue`) that must be
implemented. This interface is used as part of the declaration of an `SqlOutParameter`.
The following example shows returning the value of an Oracle `STRUCT` object of the user
2018-09-05 23:15:53 +08:00
declared type `ITEM_TYPE`:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
2018-05-16 06:46:57 +08:00
public class TestItemStoredProcedure extends StoredProcedure {
2015-03-03 18:38:01 +08:00
2018-05-16 06:46:57 +08:00
public TestItemStoredProcedure(DataSource dataSource) {
2019-09-03 22:03:28 +08:00
// ...
2018-05-16 06:46:57 +08:00
declareParameter(new SqlOutParameter("item", OracleTypes.STRUCT, "ITEM_TYPE",
2020-01-20 08:48:52 +08:00
(CallableStatement cs, int colIndx, int sqlType, String typeName) -> {
STRUCT struct = (STRUCT) cs.getObject(colIndx);
Object[] attr = struct.getAttributes();
TestItem item = new TestItem();
item.setId(((Number) attr[0]).longValue());
item.setDescription((String) attr[1]);
item.setExpirationDate((java.util.Date) attr[2]);
return item;
2018-05-16 06:46:57 +08:00
}));
2019-09-03 22:03:28 +08:00
// ...
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class TestItemStoredProcedure(dataSource: DataSource) : StoredProcedure() {
init {
// ...
declareParameter(SqlOutParameter("item", OracleTypes.STRUCT, "ITEM_TYPE") { cs, colIndx, sqlType, typeName ->
val struct = cs.getObject(colIndx) as STRUCT
val attr = struct.getAttributes()
TestItem((attr[0] as Long, attr[1] as String, attr[2] as Date)
})
// ...
2018-05-16 06:46:57 +08:00
}
2019-09-03 22:03:28 +08:00
}
2015-03-03 18:38:01 +08:00
----
2018-09-05 23:15:53 +08:00
You can use `SqlTypeValue` to pass the value of a Java object (such as `TestItem`) to a
stored procedure. The `SqlTypeValue` interface has a single method (named
`createTypeValue`) that you must implement. The active connection is passed in, and you
can use it to create database-specific objects, such as `StructDescriptor` instances
or `ArrayDescriptor` instances. The following example creates a `StructDescriptor` instance:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
2018-05-16 06:46:57 +08:00
final TestItem testItem = new TestItem(123L, "A test item",
2015-03-03 18:38:01 +08:00
new SimpleDateFormat("yyyy-M-d").parse("2010-12-31"));
SqlTypeValue value = new AbstractSqlTypeValue() {
protected Object createTypeValue(Connection conn, int sqlType, String typeName) throws SQLException {
StructDescriptor itemDescriptor = new StructDescriptor(typeName, conn);
Struct item = new STRUCT(itemDescriptor, conn,
new Object[] {
testItem.getId(),
testItem.getDescription(),
new java.sql.Date(testItem.getExpirationDate().getTime())
});
return item;
}
};
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val (id, description, expirationDate) = TestItem(123L, "A test item",
SimpleDateFormat("yyyy-M-d").parse("2010-12-31"))
val value = object : AbstractSqlTypeValue() {
override fun createTypeValue(conn: Connection, sqlType: Int, typeName: String?): Any {
val itemDescriptor = StructDescriptor(typeName, conn)
return STRUCT(itemDescriptor, conn,
arrayOf(id, description, java.sql.Date(expirationDate.time)))
}
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
You can now add this `SqlTypeValue` to the `Map` that contains the input parameters for the
`execute` call of the stored procedure.
2015-03-03 18:38:01 +08:00
Another use for the `SqlTypeValue` is passing in an array of values to an Oracle stored
procedure. Oracle has its own internal `ARRAY` class that must be used in this case, and
you can use the `SqlTypeValue` to create an instance of the Oracle `ARRAY` and populate
2018-09-05 23:15:53 +08:00
it with values from the Java `ARRAY`, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
final Long[] ids = new Long[] {1L, 2L};
SqlTypeValue value = new AbstractSqlTypeValue() {
protected Object createTypeValue(Connection conn, int sqlType, String typeName) throws SQLException {
ArrayDescriptor arrayDescriptor = new ArrayDescriptor(typeName, conn);
ARRAY idArray = new ARRAY(arrayDescriptor, conn, ids);
return idArray;
}
};
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class TestItemStoredProcedure(dataSource: DataSource) : StoredProcedure() {
init {
val ids = arrayOf(1L, 2L)
val value = object : AbstractSqlTypeValue() {
override fun createTypeValue(conn: Connection, sqlType: Int, typeName: String?): Any {
val arrayDescriptor = ArrayDescriptor(typeName, conn)
return ARRAY(arrayDescriptor, conn, ids)
}
}
}
}
----
2015-03-03 18:38:01 +08:00
[[jdbc-embedded-database-support]]
2018-09-05 23:15:53 +08:00
=== Embedded Database Support
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
The `org.springframework.jdbc.datasource.embedded` package provides support for embedded
Java database engines. Support for http://www.hsqldb.org[HSQL],
2019-03-21 06:48:14 +08:00
https://www.h2database.com[H2], and https://db.apache.org/derby[Derby] is provided
2015-03-03 18:38:01 +08:00
natively. You can also use an extensible API to plug in new embedded database types and
`DataSource` implementations.
[[jdbc-why-embedded-database]]
2018-09-05 23:15:53 +08:00
==== Why Use an Embedded Database?
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
An embedded database can be useful during the development phase of a project because of its
2015-03-03 18:38:01 +08:00
lightweight nature. Benefits include ease of configuration, quick startup time,
2018-09-05 23:15:53 +08:00
testability, and the ability to rapidly evolve your SQL during development.
2015-03-03 18:38:01 +08:00
[[jdbc-embedded-database-xml]]
2018-09-05 23:15:53 +08:00
==== Creating an Embedded Database by Using Spring XML
2015-07-30 02:35:59 +08:00
2015-03-03 18:38:01 +08:00
If you want to expose an embedded database instance as a bean in a Spring
2018-09-05 23:15:53 +08:00
`ApplicationContext`, you can use the `embedded-database` tag in the `spring-jdbc` namespace:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
2015-07-30 03:15:39 +08:00
<jdbc:embedded-database id="dataSource" generate-name="true">
2015-03-03 18:38:01 +08:00
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
----
2018-09-05 23:15:53 +08:00
The preceding configuration creates an embedded HSQL database that is populated with SQL from
the `schema.sql` and `test-data.sql` resources in the root of the classpath. In addition, as
a best practice, the embedded database is assigned a uniquely generated name. The
2015-07-30 03:15:39 +08:00
embedded database is made available to the Spring container as a bean of type
2018-09-05 23:15:53 +08:00
`javax.sql.DataSource` that can then be injected into data access objects as needed.
2015-03-03 18:38:01 +08:00
[[jdbc-embedded-database-java]]
2018-09-05 23:15:53 +08:00
==== Creating an Embedded Database Programmatically
2015-07-30 02:35:59 +08:00
2015-03-03 18:38:01 +08:00
The `EmbeddedDatabaseBuilder` class provides a fluent API for constructing an embedded
2018-09-05 23:15:53 +08:00
database programmatically. You can use this when you need to create an embedded database in a
stand-alone environment or in a stand-alone integration test, as in the following example:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.setType(H2)
.setScriptEncoding("UTF-8")
.ignoreFailedDrops(true)
.addScript("schema.sql")
.addScripts("user_data.sql", "country_data.sql")
.build();
// perform actions against the db (EmbeddedDatabase extends javax.sql.DataSource)
db.shutdown()
2015-03-03 18:38:01 +08:00
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val db = EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.setType(H2)
.setScriptEncoding("UTF-8")
.ignoreFailedDrops(true)
.addScript("schema.sql")
.addScripts("user_data.sql", "country_data.sql")
.build()
2015-07-30 02:35:59 +08:00
2019-09-03 22:03:28 +08:00
// perform actions against the db (EmbeddedDatabase extends javax.sql.DataSource)
2015-07-30 02:35:59 +08:00
2019-09-03 22:03:28 +08:00
db.shutdown()
2015-03-03 18:38:01 +08:00
----
2018-10-25 21:15:58 +08:00
See the {api-spring-framework}/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.html[javadoc for `EmbeddedDatabaseBuilder`]
for further details on all supported options.
2015-07-30 03:15:39 +08:00
2018-09-05 23:15:53 +08:00
You can also use the `EmbeddedDatabaseBuilder` to create an embedded database by using Java
configuration, as the following example shows:
2015-07-30 02:35:59 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-07-30 02:35:59 +08:00
----
2019-09-03 22:03:28 +08:00
@Configuration
public class DataSourceConfig {
2015-07-30 02:35:59 +08:00
2019-09-03 22:03:28 +08:00
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.setType(H2)
.setScriptEncoding("UTF-8")
.ignoreFailedDrops(true)
.addScript("schema.sql")
.addScripts("user_data.sql", "country_data.sql")
.build();
}
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Configuration
class DataSourceConfig {
@Bean
fun dataSource(): DataSource {
return EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.setType(H2)
.setScriptEncoding("UTF-8")
.ignoreFailedDrops(true)
.addScript("schema.sql")
.addScripts("user_data.sql", "country_data.sql")
.build()
}
2017-11-21 05:28:00 +08:00
}
2015-07-30 02:35:59 +08:00
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
2015-07-30 02:43:30 +08:00
[[jdbc-embedded-database-types]]
2018-09-05 23:15:53 +08:00
==== Selecting the Embedded Database Type
This section covers how to select one of the three embedded databases that Spring
supports. It includes the following topics:
* <<jdbc-embedded-database-using-HSQL>>
* <<jdbc-embedded-database-using-H2>>
* <<jdbc-embedded-database-using-Derby>>
2015-03-03 18:38:01 +08:00
[[jdbc-embedded-database-using-HSQL]]
2015-07-30 02:43:30 +08:00
===== Using HSQL
2017-10-19 02:24:17 +08:00
2015-07-30 02:35:59 +08:00
Spring supports HSQL 1.8.0 and above. HSQL is the default embedded database if no type is
2018-09-05 23:15:53 +08:00
explicitly specified. To specify HSQL explicitly, set the `type` attribute of the
`embedded-database` tag to `HSQL`. If you use the builder API, call the
2015-03-03 18:38:01 +08:00
`setType(EmbeddedDatabaseType)` method with `EmbeddedDatabaseType.HSQL`.
[[jdbc-embedded-database-using-H2]]
2015-07-30 02:43:30 +08:00
===== Using H2
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Spring supports the H2 database. To enable H2, set the `type` attribute of the
`embedded-database` tag to `H2`. If you use the builder API, call the
2015-03-03 18:38:01 +08:00
`setType(EmbeddedDatabaseType)` method with `EmbeddedDatabaseType.H2`.
[[jdbc-embedded-database-using-Derby]]
2015-07-30 02:43:30 +08:00
===== Using Derby
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Spring supports Apache Derby 10.5 and above. To enable Derby, set the `type`
attribute of the `embedded-database` tag to `DERBY`. If you use the builder API,
2015-07-30 02:35:59 +08:00
call the `setType(EmbeddedDatabaseType)` method with `EmbeddedDatabaseType.DERBY`.
2015-03-03 18:38:01 +08:00
[[jdbc-embedded-database-dao-testing]]
2018-09-05 23:15:53 +08:00
==== Testing Data Access Logic with an Embedded Database
2015-07-30 02:35:59 +08:00
2018-09-05 23:15:53 +08:00
Embedded databases provide a lightweight way to test data access code. The next example is a
data access integration test template that uses an embedded database. Using such a template
2019-03-05 20:08:34 +08:00
can be useful for one-offs when the embedded database does not need to be reused across test
classes. However, if you wish to create an embedded database that is shared within a test suite,
consider using the <<testing.adoc#testcontext-framework, Spring TestContext Framework>> and
configuring the embedded database as a bean in the Spring `ApplicationContext` as described
in <<jdbc-embedded-database-xml>> and <<jdbc-embedded-database-java>>. The following listing
shows the test template:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
2015-07-30 02:35:59 +08:00
public class DataAccessIntegrationTestTemplate {
2015-03-03 18:38:01 +08:00
private EmbeddedDatabase db;
2019-09-03 22:03:28 +08:00
@BeforeEach
2015-03-03 18:38:01 +08:00
public void setUp() {
// creates an HSQL in-memory database populated from default scripts
// classpath:schema.sql and classpath:data.sql
2015-07-30 03:15:39 +08:00
db = new EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.addDefaultScripts()
.build();
2015-03-03 18:38:01 +08:00
}
@Test
public void testDataAccess() {
JdbcTemplate template = new JdbcTemplate(db);
2015-07-30 02:35:59 +08:00
template.query( /* ... */ );
2015-03-03 18:38:01 +08:00
}
2019-09-03 22:03:28 +08:00
@AfterEach
2015-03-03 18:38:01 +08:00
public void tearDown() {
db.shutdown();
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class DataAccessIntegrationTestTemplate {
private lateinit var db: EmbeddedDatabase
@BeforeEach
fun setUp() {
// creates an HSQL in-memory database populated from default scripts
// classpath:schema.sql and classpath:data.sql
db = EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.addDefaultScripts()
.build()
}
@Test
fun testDataAccess() {
val template = JdbcTemplate(db)
template.query( /* ... */)
}
@AfterEach
fun tearDown() {
db.shutdown()
}
}
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
2015-07-30 03:15:39 +08:00
[[jdbc-embedded-database-unique-names]]
2018-09-05 23:15:53 +08:00
==== Generating Unique Names for Embedded Databases
2015-07-30 03:15:39 +08:00
Development teams often encounter errors with embedded databases if their test suite
inadvertently attempts to recreate additional instances of the same database. This can
happen quite easily if an XML configuration file or `@Configuration` class is responsible
for creating an embedded database and the corresponding configuration is then reused
2018-09-05 23:15:53 +08:00
across multiple testing scenarios within the same test suite (that is, within the same JVM
2018-09-27 02:17:07 +08:00
process) -- for example, integration tests against embedded databases whose
2018-09-05 23:15:53 +08:00
`ApplicationContext` configuration differs only with regard to which bean definition
2015-07-30 03:15:39 +08:00
profiles are active.
The root cause of such errors is the fact that Spring's `EmbeddedDatabaseFactory` (used
internally by both the `<jdbc:embedded-database>` XML namespace element and the
2018-09-05 23:15:53 +08:00
`EmbeddedDatabaseBuilder` for Java configuration) sets the name of the embedded database to
`testdb` if not otherwise specified. For the case of `<jdbc:embedded-database>`, the
embedded database is typically assigned a name equal to the bean's `id` (often,
something like `dataSource`). Thus, subsequent attempts to create an embedded database
do not result in a new database. Instead, the same JDBC connection URL is reused,
and attempts to create a new embedded database actually point to an existing
2015-07-30 03:15:39 +08:00
embedded database created from the same configuration.
2018-09-05 23:15:53 +08:00
To address this common issue, Spring Framework 4.2 provides support for generating
unique names for embedded databases. To enable the use of generated names, use one of
2015-07-30 03:15:39 +08:00
the following options.
* `EmbeddedDatabaseFactory.setGenerateUniqueDatabaseName()`
* `EmbeddedDatabaseBuilder.generateUniqueName()`
* `<jdbc:embedded-database generate-name="true" ... >`
2015-07-30 02:43:30 +08:00
[[jdbc-embedded-database-extension]]
2018-09-05 23:15:53 +08:00
==== Extending the Embedded Database Support
2015-07-30 02:43:30 +08:00
2018-09-05 23:15:53 +08:00
You can extend Spring JDBC embedded database support in two ways:
2015-07-30 02:43:30 +08:00
* Implement `EmbeddedDatabaseConfigurer` to support a new embedded database type.
* Implement `DataSourceFactory` to support a new `DataSource` implementation, such as a
connection pool to manage embedded database connections.
2018-09-05 23:15:53 +08:00
We encourage you to contribute extensions to the Spring community at
2019-01-18 06:08:39 +08:00
https://github.com/spring-projects/spring-framework/issues[GitHub Issues].
2015-07-30 02:43:30 +08:00
2016-12-09 01:24:12 +08:00
[[jdbc-initializing-datasource]]
2018-09-05 23:15:53 +08:00
=== Initializing a `DataSource`
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
The `org.springframework.jdbc.datasource.init` package provides support for initializing
an existing `DataSource`. The embedded database support provides one option for creating
2018-09-05 23:15:53 +08:00
and initializing a `DataSource` for an application. However, you may sometimes need to initialize
an instance that runs on a server somewhere.
2015-03-03 18:38:01 +08:00
[[jdbc-initializing-datasource-xml]]
2018-09-05 23:15:53 +08:00
==== Initializing a Database by Using Spring XML
2017-10-19 02:24:17 +08:00
2015-07-30 02:35:59 +08:00
If you want to initialize a database and you can provide a reference to a `DataSource`
2018-09-05 23:15:53 +08:00
bean, you can use the `initialize-database` tag in the `spring-jdbc` namespace:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:com/foo/sql/db-schema.sql"/>
<jdbc:script location="classpath:com/foo/sql/db-test-data.sql"/>
</jdbc:initialize-database>
----
2018-09-05 23:15:53 +08:00
The preceding example runs the two specified scripts against the database. The first
2015-07-30 02:35:59 +08:00
script creates a schema, and the second populates tables with a test data set. The script
2018-09-05 23:15:53 +08:00
locations can also be patterns with wildcards in the usual Ant style used for resources
in Spring (for example,
`classpath{asterisk}:/com/foo/{asterisk}{asterisk}/sql/{asterisk}-data.sql`). If you use a
pattern, the scripts are run in the lexical order of their URL or filename.
The default behavior of the database initializer is to unconditionally run the provided
scripts. This may not always be what you want -- for instance, if you run
the scripts against a database that already has test data in it. The likelihood
of accidentally deleting data is reduced by following the common pattern (shown earlier)
of creating the tables first and then inserting the data. The first step fails if
2015-07-30 02:35:59 +08:00
the tables already exist.
2015-03-03 18:38:01 +08:00
2015-07-30 02:35:59 +08:00
However, to gain more control over the creation and deletion of existing data, the XML
namespace provides a few additional options. The first is a flag to switch the
2018-09-05 23:15:53 +08:00
initialization on and off. You can set this according to the environment (such as pulling a
boolean value from system properties or from an environment bean). The following example gets a value from a system property:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<jdbc:initialize-database data-source="dataSource"
2018-09-05 23:15:53 +08:00
enabled="#{systemProperties.INITIALIZE_DATABASE}"> <1>
2015-03-03 18:38:01 +08:00
<jdbc:script location="..."/>
</jdbc:initialize-database>
----
2018-09-05 23:15:53 +08:00
<1> Get the value for `enabled` from a system property called `INITIALIZE_DATABASE`.
2018-11-27 06:15:55 +08:00
2015-03-03 18:38:01 +08:00
The second option to control what happens with existing data is to be more tolerant of
2018-09-05 23:15:53 +08:00
failures. To this end, you can control the ability of the initializer to ignore certain
errors in the SQL it executes from the scripts, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
2019-03-13 22:12:51 +08:00
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS">
2015-03-03 18:38:01 +08:00
<jdbc:script location="..."/>
</jdbc:initialize-database>
----
2018-09-05 23:15:53 +08:00
In the preceding example, we are saying that we expect that, sometimes, the scripts are run
against an empty database, and there are some `DROP` statements in the scripts that
would, therefore, fail. So failed SQL `DROP` statements will be ignored, but other failures
2015-07-30 02:35:59 +08:00
will cause an exception. This is useful if your SQL dialect doesn't support `DROP ... IF
2015-03-03 18:38:01 +08:00
EXISTS` (or similar) but you want to unconditionally remove all test data before
2015-07-30 02:35:59 +08:00
re-creating it. In that case the first script is usually a set of `DROP` statements,
followed by a set of `CREATE` statements.
2015-03-03 18:38:01 +08:00
The `ignore-failures` option can be set to `NONE` (the default), `DROPS` (ignore failed
2015-07-30 02:35:59 +08:00
drops), or `ALL` (ignore all failures).
2015-03-03 18:38:01 +08:00
2016-01-18 23:09:07 +08:00
Each statement should be separated by `;` or a new line if the `;` character is not
2018-09-05 23:15:53 +08:00
present at all in the script. You can control that globally or script by script, as the
following example shows:
2016-01-18 23:09:07 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2016-01-18 23:09:07 +08:00
----
2018-09-05 23:15:53 +08:00
<jdbc:initialize-database data-source="dataSource" separator="@@"> <1>
<jdbc:script location="classpath:com/myapp/sql/db-schema.sql" separator=";"/> <2>
<jdbc:script location="classpath:com/myapp/sql/db-test-data-1.sql"/>
<jdbc:script location="classpath:com/myapp/sql/db-test-data-2.sql"/>
2016-01-18 23:09:07 +08:00
</jdbc:initialize-database>
----
2018-09-05 23:15:53 +08:00
<1> Set the separator scripts to `@@`.
<2> Set the separator for `db-schema.sql` to `;`.
2016-01-18 23:09:07 +08:00
In this example, the two `test-data` scripts use `@@` as statement separator and only
the `db-schema.sql` uses `;`. This configuration specifies that the default separator
2018-09-05 23:15:53 +08:00
is `@@` and overrides that default for the `db-schema` script.
2016-01-18 23:09:07 +08:00
2018-09-05 23:15:53 +08:00
If you need more control than you get from the XML namespace, you can use the
2015-07-30 02:35:59 +08:00
`DataSourceInitializer` directly and define it as a component in your application.
2015-03-03 18:38:01 +08:00
[[jdbc-client-component-initialization]]
2018-09-05 23:15:53 +08:00
===== Initialization of Other Components that Depend on the Database
2015-07-30 02:35:59 +08:00
2018-09-05 23:15:53 +08:00
A large class of applications (those that do not use the database until after the Spring context has
started) can use the database initializer with no further
complications. If your application is not one of those, you might need to read the rest
2015-07-30 02:35:59 +08:00
of this section.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The database initializer depends on a `DataSource` instance and runs the scripts
2015-07-30 02:35:59 +08:00
provided in its initialization callback (analogous to an `init-method` in an XML bean
definition, a `@PostConstruct` method in a component, or the `afterPropertiesSet()`
method in a component that implements `InitializingBean`). If other beans depend on the
2018-09-05 23:15:53 +08:00
same data source and use the data source in an initialization callback, there
2015-07-30 02:35:59 +08:00
might be a problem because the data has not yet been initialized. A common example of
this is a cache that initializes eagerly and loads data from the database on application
startup.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
To get around this issue, you have two options: change your cache initialization strategy
to a later phase or ensure that the database initializer is initialized first.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Changing your cache initialization strategy might be easy if the application is in your control and not otherwise.
2015-07-30 02:35:59 +08:00
Some suggestions for how to implement this include:
2015-03-03 18:38:01 +08:00
2015-07-30 02:35:59 +08:00
* Make the cache initialize lazily on first usage, which improves application startup
time.
2015-03-03 18:38:01 +08:00
* Have your cache or a separate component that initializes the cache implement
2018-09-05 23:15:53 +08:00
`Lifecycle` or `SmartLifecycle`. When the application context starts, you can
automatically start a `SmartLifecycle` by setting its `autoStartup` flag, and you can
2018-09-27 02:17:07 +08:00
manually start a `Lifecycle` by calling `ConfigurableApplicationContext.start()`
2015-07-30 02:35:59 +08:00
on the enclosing context.
2015-03-03 18:38:01 +08:00
* Use a Spring `ApplicationEvent` or similar custom observer mechanism to trigger the
cache initialization. `ContextRefreshedEvent` is always published by the context when
it is ready for use (after all beans have been initialized), so that is often a useful
hook (this is how the `SmartLifecycle` works by default).
2018-09-05 23:15:53 +08:00
Ensuring that the database initializer is initialized first can also be easy. Some suggestions on how to implement this include:
2015-07-30 02:35:59 +08:00
* Rely on the default behavior of the Spring `BeanFactory`, which is that beans are
initialized in registration order. You can easily arrange that by adopting the common
practice of a set of `<import/>` elements in XML configuration that order your
2018-09-05 23:15:53 +08:00
application modules and ensuring that the database and database initialization are
2015-07-30 02:35:59 +08:00
listed first.
2018-09-05 23:15:53 +08:00
* Separate the `DataSource` and the business components that use it and control their
startup order by putting them in separate `ApplicationContext` instances (for example, the
parent context contains the `DataSource`, and the child context contains the business
2015-07-30 02:35:59 +08:00
components). This structure is common in Spring web applications but can be more
generally applied.
2015-03-03 18:38:01 +08:00
2018-10-25 21:15:58 +08:00
2015-03-03 18:38:01 +08:00
[[orm]]
== Object Relational Mapping (ORM) Data Access
2018-09-05 23:15:53 +08:00
This section covers data access when you use Object Relational Mapping (ORM).
2015-03-03 18:38:01 +08:00
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
[[orm-introduction]]
=== Introduction to ORM with Spring
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The Spring Framework supports integration with the Java Persistence API (JPA) and
supports native Hibernate for resource management, data access object (DAO) implementations,
and transaction strategies. For example, for Hibernate, there is first-class support with
2016-07-05 05:34:48 +08:00
several convenient IoC features that address many typical Hibernate integration issues.
2018-09-05 23:15:53 +08:00
You can configure all of the supported features for OR (object relational) mapping
2016-07-05 05:34:48 +08:00
tools through Dependency Injection. They can participate in Spring's resource and
transaction management, and they comply with Spring's generic transaction and DAO
exception hierarchies. The recommended integration style is to code DAOs against plain
2017-10-05 19:23:18 +08:00
Hibernate or JPA APIs.
2015-03-03 18:38:01 +08:00
Spring adds significant enhancements to the ORM layer of your choice when you create
data access applications. You can leverage as much of the integration support as you
wish, and you should compare this integration effort with the cost and risk of building
a similar infrastructure in-house. You can use much of the ORM support as you would a
library, regardless of technology, because everything is designed as a set of reusable
2018-09-05 23:15:53 +08:00
JavaBeans. ORM in a Spring IoC container facilitates configuration and deployment. Thus,
2015-03-03 18:38:01 +08:00
most examples in this section show configuration inside a Spring container.
2018-09-05 23:15:53 +08:00
The benefits of using the Spring Framework to create your ORM DAOs include:
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
* *Easier testing.* Spring's IoC approach makes it easy to swap the implementations
2015-03-03 18:38:01 +08:00
and configuration locations of Hibernate `SessionFactory` instances, JDBC `DataSource`
instances, transaction managers, and mapped object implementations (if needed). This
in turn makes it much easier to test each piece of persistence-related code in
isolation.
2018-09-05 23:15:53 +08:00
* *Common data access exceptions.* Spring can wrap exceptions from your ORM tool,
2015-03-03 18:38:01 +08:00
converting them from proprietary (potentially checked) exceptions to a common runtime
2018-09-05 23:15:53 +08:00
`DataAccessException` hierarchy. This feature lets you handle most persistence
2015-03-03 18:38:01 +08:00
exceptions, which are non-recoverable, only in the appropriate layers, without
annoying boilerplate catches, throws, and exception declarations. You can still trap
and handle exceptions as necessary. Remember that JDBC exceptions (including
DB-specific dialects) are also converted to the same hierarchy, meaning that you can
perform some operations with JDBC within a consistent programming model.
2018-09-05 23:15:53 +08:00
* *General resource management.* Spring application contexts can handle the location
2015-03-03 18:38:01 +08:00
and configuration of Hibernate `SessionFactory` instances, JPA `EntityManagerFactory`
instances, JDBC `DataSource` instances, and other related resources. This makes these
values easy to manage and change. Spring offers efficient, easy, and safe handling of
persistence resources. For example, related code that uses Hibernate generally needs to
use the same Hibernate `Session` to ensure efficiency and proper transaction handling.
Spring makes it easy to create and bind a `Session` to the current thread transparently,
2018-09-05 23:15:53 +08:00
by exposing a current `Session` through the Hibernate `SessionFactory`. Thus, Spring
2015-03-03 18:38:01 +08:00
solves many chronic problems of typical Hibernate usage, for any local or JTA
transaction environment.
2018-09-05 23:15:53 +08:00
* *Integrated transaction management.* You can wrap your ORM code with a declarative,
2015-03-03 18:38:01 +08:00
aspect-oriented programming (AOP) style method interceptor either through the
`@Transactional` annotation or by explicitly configuring the transaction AOP advice in
an XML configuration file. In both cases, transaction semantics and exception handling
2019-03-05 20:08:34 +08:00
(rollback and so on) are handled for you. As discussed in <<orm-resource-mngmnt>>,
you can also swap various transaction managers, without affecting your ORM-related code.
For example, you can swap between local transactions and JTA, with the same full services
(such as declarative transactions) available in both scenarios. Additionally,
JDBC-related code can fully integrate transactionally with the code you use to do ORM.
This is useful for data access that is not suitable for ORM (such as batch processing and
BLOB streaming) but that still needs to share common transactions with ORM operations.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
TIP: For more comprehensive ORM support, including support for alternative database
2015-03-03 18:38:01 +08:00
technologies such as MongoDB, you might want to check out the
2019-03-21 06:48:14 +08:00
https://projects.spring.io/spring-data/[Spring Data] suite of projects. If you are
2015-03-03 18:38:01 +08:00
a JPA user, the https://spring.io/guides/gs/accessing-data-jpa/[Getting Started Accessing
Data with JPA] guide from https://spring.io provides a great introduction.
[[orm-general]]
2018-09-05 23:15:53 +08:00
=== General ORM Integration Considerations
2017-10-19 02:24:17 +08:00
2019-03-05 20:08:34 +08:00
This section highlights considerations that apply to all ORM technologies.
The <<orm-hibernate>> section provides more details and also show these features and
2015-03-03 18:38:01 +08:00
configurations in a concrete context.
2018-09-05 23:15:53 +08:00
The major goal of Spring's ORM integration is clear application layering (with any data
access and transaction technology) and for loose coupling of application objects -- no
2015-03-03 18:38:01 +08:00
more business service dependencies on the data access or transaction strategy, no more
hard-coded resource lookups, no more hard-to-replace singletons, no more custom service
2018-09-05 23:15:53 +08:00
registries. The goal is to have one simple and consistent approach to wiring up application objects, keeping
2015-03-03 18:38:01 +08:00
them as reusable and free from container dependencies as possible. All the individual
data access features are usable on their own but integrate nicely with Spring's
application context concept, providing XML-based configuration and cross-referencing of
plain JavaBean instances that need not be Spring-aware. In a typical Spring application,
many important objects are JavaBeans: data access templates, data access objects,
transaction managers, business services that use the data access objects and transaction
2018-09-05 23:15:53 +08:00
managers, web view resolvers, web controllers that use the business services, and so on.
2015-03-03 18:38:01 +08:00
[[orm-resource-mngmnt]]
2018-09-05 23:15:53 +08:00
==== Resource and Transaction Management
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Typical business applications are cluttered with repetitive resource management code.
Many projects try to invent their own solutions, sometimes sacrificing proper handling
of failures for programming convenience. Spring advocates simple solutions for proper
resource handling, namely IoC through templating in the case of JDBC and applying AOP
interceptors for the ORM technologies.
The infrastructure provides proper resource handling and appropriate conversion of
specific API exceptions to an unchecked infrastructure exception hierarchy. Spring
introduces a DAO exception hierarchy, applicable to any data access strategy. For direct
2019-03-05 20:08:34 +08:00
JDBC, the `JdbcTemplate` class mentioned in a <<jdbc-JdbcTemplate, previous section>>
provides connection handling and proper conversion of `SQLException` to the
`DataAccessException` hierarchy, including translation of database-specific SQL error
codes to meaningful exception classes. For ORM technologies, see the
<<orm-exception-translation, next section>> for how to get the same exception
2015-03-03 18:38:01 +08:00
translation benefits.
When it comes to transaction management, the `JdbcTemplate` class hooks in to the Spring
transaction support and supports both JTA and JDBC transactions, through respective
2018-09-05 23:15:53 +08:00
Spring transaction managers. For the supported ORM technologies, Spring offers Hibernate
2016-07-05 05:34:48 +08:00
and JPA support through the Hibernate and JPA transaction managers as well as JTA support.
For details on transaction support, see the <<transaction>> chapter.
2015-03-03 18:38:01 +08:00
[[orm-exception-translation]]
2018-09-05 23:15:53 +08:00
==== Exception Translation
2017-10-19 02:24:17 +08:00
2016-07-05 05:34:48 +08:00
When you use Hibernate or JPA in a DAO, you must decide how to handle the persistence
technology's native exception classes. The DAO throws a subclass of a `HibernateException`
2018-09-05 23:15:53 +08:00
or `PersistenceException`, depending on the technology. These exceptions are all runtime
2016-07-05 05:34:48 +08:00
exceptions and do not have to be declared or caught. You may also have to deal with
`IllegalArgumentException` and `IllegalStateException`. This means that callers can only
2018-09-05 23:15:53 +08:00
treat exceptions as being generally fatal, unless they want to depend on the persistence
technology's own exception structure. Catching specific causes (such as an optimistic
locking failure) is not possible without tying the caller to the implementation strategy.
This trade-off might be acceptable to applications that are strongly ORM-based or
do not need any special exception treatment (or both). However, Spring lets exception
translation be applied transparently through the `@Repository` annotation. The following
examples (one for Java configuration and one for XML configuration) show how to do so:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
@Repository
public class ProductDaoImpl implements ProductDao {
// class body here...
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Repository
class ProductDaoImpl : ProductDao {
// class body here...
}
----
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<!-- Exception translation bean post processor -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="myProductDao" class="product.ProductDaoImpl"/>
</beans>
----
The postprocessor automatically looks for all exception translators (implementations of
the `PersistenceExceptionTranslator` interface) and advises all beans marked with the
`@Repository` annotation so that the discovered translators can intercept and apply the
appropriate translation on the thrown exceptions.
2018-09-05 23:15:53 +08:00
In summary, you can implement DAOs based on the plain persistence technology's API and
annotations while still benefiting from Spring-managed transactions, dependency
2015-03-03 18:38:01 +08:00
injection, and transparent exception conversion (if desired) to Spring's custom
exception hierarchies.
[[orm-hibernate]]
=== Hibernate
2017-10-19 02:24:17 +08:00
2019-03-21 06:48:14 +08:00
We start with a coverage of https://hibernate.org/[Hibernate 5] in a Spring
2015-03-03 18:38:01 +08:00
environment, using it to demonstrate the approach that Spring takes towards integrating
2018-09-05 23:15:53 +08:00
OR mappers. This section covers many issues in detail and shows different variations
2015-03-03 18:38:01 +08:00
of DAO implementations and transaction demarcation. Most of these patterns can be
2018-09-05 23:15:53 +08:00
directly translated to all other supported ORM tools. The later sections in this
chapter then cover the other ORM technologies and show brief examples.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
NOTE: As of Spring Framework 5.0, Spring requires Hibernate ORM 4.3 or later for JPA support
2017-09-28 17:47:50 +08:00
and even Hibernate ORM 5.0+ for programming against the native Hibernate Session API.
2018-08-16 23:30:44 +08:00
Note that the Hibernate team does not maintain any versions prior to 5.1 anymore and
is likely to focus on 5.3+ exclusively soon.
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[orm-session-factory-setup]]
2018-09-05 23:15:53 +08:00
==== `SessionFactory` Setup in a Spring Container
2015-03-03 18:38:01 +08:00
To avoid tying application objects to hard-coded resource lookups, you can define
2018-09-05 23:15:53 +08:00
resources (such as a JDBC `DataSource` or a Hibernate `SessionFactory`) as beans in the
2015-03-03 18:38:01 +08:00
Spring container. Application objects that need to access resources receive references
to such predefined instances through bean references, as illustrated in the DAO
2019-03-05 20:08:34 +08:00
definition in the <<orm-hibernate-straight, next section>>.
2015-03-03 18:38:01 +08:00
The following excerpt from an XML application context definition shows how to set up a
JDBC `DataSource` and a Hibernate `SessionFactory` on top of it:
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
2015-12-18 07:27:19 +08:00
<bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
2015-03-03 18:38:01 +08:00
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>product.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>
</beans>
----
Switching from a local Jakarta Commons DBCP `BasicDataSource` to a JNDI-located
2018-09-05 23:15:53 +08:00
`DataSource` (usually managed by an application server) is only a matter of
configuration, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<jee:jndi-lookup id="myDataSource" jndi-name="java:comp/env/jdbc/myds"/>
</beans>
----
You can also access a JNDI-located `SessionFactory`, using Spring's
2018-08-16 23:30:44 +08:00
`JndiObjectFactoryBean` / `<jee:jndi-lookup>` to retrieve and expose it.
However, that is typically not common outside of an EJB context.
[NOTE]
====
Spring also provides a `LocalSessionFactoryBuilder` variant, seamlessly integrating
with `@Bean` style configuration and programmatic setup (no `FactoryBean` involved).
Both `LocalSessionFactoryBean` and `LocalSessionFactoryBuilder` support background
bootstrapping, with Hibernate initialization running in parallel to the application
2018-09-05 23:15:53 +08:00
bootstrap thread on a given bootstrap executor (such as a `SimpleAsyncTaskExecutor`).
On `LocalSessionFactoryBean`, this is available through the `bootstrapExecutor`
property. On the programmatic `LocalSessionFactoryBuilder`, there is an overloaded
`buildSessionFactory` method that takes a bootstrap executor argument.
2018-08-16 23:30:44 +08:00
As of Spring Framework 5.1, such a native Hibernate setup can also expose a JPA
`EntityManagerFactory` for standard JPA interaction next to native Hibernate access.
2019-03-05 20:08:34 +08:00
See <<orm-jpa-hibernate, Native Hibernate Setup for JPA>> for details.
2018-08-16 23:30:44 +08:00
====
2015-03-03 18:38:01 +08:00
[[orm-hibernate-straight]]
2018-09-05 23:15:53 +08:00
==== Implementing DAOs Based on the Plain Hibernate API
2017-10-19 02:24:17 +08:00
2015-12-18 07:27:19 +08:00
Hibernate has a feature called contextual sessions, wherein Hibernate itself manages
2015-03-03 18:38:01 +08:00
one current `Session` per transaction. This is roughly equivalent to Spring's
synchronization of one Hibernate `Session` per transaction. A corresponding DAO
implementation resembles the following example, based on the plain Hibernate API:
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class ProductDaoImpl implements ProductDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Collection loadProductsByCategory(String category) {
return this.sessionFactory.getCurrentSession()
.createQuery("from test.Product product where product.category=?")
.setParameter(0, category)
.list();
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class ProductDaoImpl(private val sessionFactory: SessionFactory) : ProductDao {
fun loadProductsByCategory(category: String): Collection<*> {
return sessionFactory.currentSession
.createQuery("from test.Product product where product.category=?")
.setParameter(0, category)
.list()
}
}
----
2015-03-03 18:38:01 +08:00
This style is similar to that of the Hibernate reference documentation and examples,
except for holding the `SessionFactory` in an instance variable. We strongly recommend
such an instance-based setup over the old-school `static` `HibernateUtil` class from
Hibernate's CaveatEmptor sample application. (In general, do not keep any resources in
2018-09-05 23:15:53 +08:00
`static` variables unless absolutely necessary.)
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The preceding DAO example follows the dependency injection pattern. It fits nicely into a Spring IoC
container, as it would if coded against Spring's `HibernateTemplate`.
You can also set up such a DAO in plain Java (for example, in unit tests). To do so,
2015-03-03 18:38:01 +08:00
instantiate it and call `setSessionFactory(..)` with the desired factory reference. As a
Spring bean definition, the DAO would resemble the following:
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<bean id="myProductDao" class="product.ProductDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
</beans>
----
2018-09-05 23:15:53 +08:00
The main advantage of this DAO style is that it depends on Hibernate API only. No import
of any Spring class is required. This is appealing from a non-invasiveness
perspective and may feel more natural to Hibernate developers.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
However, the DAO throws plain `HibernateException` (which is unchecked, so it does not have
to be declared or caught), which means that callers can treat exceptions only as being
generally fatal -- unless they want to depend on Hibernate's own exception hierarchy.
Catching specific causes (such as an optimistic locking failure) is not possible without
2015-03-03 18:38:01 +08:00
tying the caller to the implementation strategy. This trade off might be acceptable to
2018-09-05 23:15:53 +08:00
applications that are strongly Hibernate-based, do not need any special exception
treatment, or both.
2015-03-03 18:38:01 +08:00
Fortunately, Spring's `LocalSessionFactoryBean` supports Hibernate's
`SessionFactory.getCurrentSession()` method for any Spring transaction strategy,
2018-09-05 23:15:53 +08:00
returning the current Spring-managed transactional `Session`, even with
`HibernateTransactionManager`. The standard behavior of that method remains
to return the current `Session` associated with the ongoing JTA transaction, if any.
This behavior applies regardless of whether you use Spring's
2015-03-03 18:38:01 +08:00
`JtaTransactionManager`, EJB container managed transactions (CMTs), or JTA.
2018-09-05 23:15:53 +08:00
In summary, you can implement DAOs based on the plain Hibernate API, while still being
2015-03-03 18:38:01 +08:00
able to participate in Spring-managed transactions.
[[orm-hibernate-tx-declarative]]
2018-09-05 23:15:53 +08:00
==== Declarative Transaction Demarcation
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
We recommend that you use Spring's declarative transaction support, which lets you
2015-03-03 18:38:01 +08:00
replace explicit transaction demarcation API calls in your Java code with an AOP
2018-09-05 23:15:53 +08:00
transaction interceptor. You can configure this transaction interceptor in a Spring
container by using either Java annotations or XML. This declarative transaction capability
lets you keep business services free of repetitive transaction demarcation code and
focus on adding business logic, which is the real value of your application.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
NOTE: Before you continue, we are strongly encourage you to read <<transaction-declarative>>
if you have not already done so.
2015-03-03 18:38:01 +08:00
2018-09-27 02:17:07 +08:00
You can annotate the service layer with `@Transactional` annotations and instruct the
2016-12-09 01:24:12 +08:00
Spring container to find these annotations and provide transactional semantics for
2018-09-05 23:15:53 +08:00
these annotated methods. The following example shows how to do so:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class ProductServiceImpl implements ProductService {
private ProductDao productDao;
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
@Transactional
public void increasePriceOfAllProductsInCategory(final String category) {
List productsToChange = this.productDao.loadProductsByCategory(category);
// ...
}
@Transactional(readOnly = true)
public List<Product> findAllProducts() {
return this.productDao.findAllProducts();
}
2019-09-03 22:03:28 +08:00
}
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class ProductServiceImpl(private val productDao: ProductDao) : ProductService {
@Transactional
fun increasePriceOfAllProductsInCategory(category: String) {
val productsToChange = productDao.loadProductsByCategory(category)
// ...
}
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
@Transactional(readOnly = true)
fun findAllProducts() = productDao.findAllProducts()
2015-03-03 18:38:01 +08:00
}
----
2018-11-05 19:27:35 +08:00
In the container, you need to set up the `PlatformTransactionManager` implementation
(as a bean) and a `<tx:annotation-driven/>` entry, opting into `@Transactional`
processing at runtime. The following example shows how to do so:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/beans/spring-beans.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/tx
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/tx/spring-tx.xsd
2015-03-03 18:38:01 +08:00
http://www.springframework.org/schema/aop
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/aop/spring-aop.xsd">
2015-03-03 18:38:01 +08:00
<!-- SessionFactory, DataSource, etc. omitted -->
<bean id="transactionManager"
2015-12-18 07:27:19 +08:00
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
2015-03-03 18:38:01 +08:00
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven/>
<bean id="myProductService" class="product.SimpleProductService">
<property name="productDao" ref="myProductDao"/>
</bean>
</beans>
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[orm-hibernate-tx-programmatic]]
2018-09-05 23:15:53 +08:00
==== Programmatic Transaction Demarcation
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
You can demarcate transactions in a higher level of the application, on top of
lower-level data access services that span any number of operations. Nor do restrictions
exist on the implementation of the surrounding business service. It needs only a Spring
2015-03-03 18:38:01 +08:00
`PlatformTransactionManager`. Again, the latter can come from anywhere, but preferably
2018-09-05 23:15:53 +08:00
as a bean reference through a `setTransactionManager(..)` method. Also, the
`productDAO` should be set by a `setProductDao(..)` method. The following pair of snippets show
a transaction manager and a business service definition in a Spring application context
2015-03-03 18:38:01 +08:00
and an example for a business method implementation:
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
2015-12-18 07:27:19 +08:00
<bean id="myTxManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
2015-03-03 18:38:01 +08:00
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean id="myProductService" class="product.ProductServiceImpl">
<property name="transactionManager" ref="myTxManager"/>
<property name="productDao" ref="myProductDao"/>
</bean>
</beans>
----
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class ProductServiceImpl implements ProductService {
private TransactionTemplate transactionTemplate;
private ProductDao productDao;
public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionTemplate = new TransactionTemplate(transactionManager);
}
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
public void increasePriceOfAllProductsInCategory(final String category) {
this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(TransactionStatus status) {
List productsToChange = this.productDao.loadProductsByCategory(category);
// do the price increase...
}
});
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class ProductServiceImpl(transactionManager: PlatformTransactionManager,
private val productDao: ProductDao) : ProductService {
private val transactionTemplate = TransactionTemplate(transactionManager)
fun increasePriceOfAllProductsInCategory(category: String) {
transactionTemplate.execute {
val productsToChange = productDao.loadProductsByCategory(category)
// do the price increase...
}
}
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Spring's `TransactionInterceptor` lets any checked application exception be thrown
2015-03-03 18:38:01 +08:00
with the callback code, while `TransactionTemplate` is restricted to unchecked
2018-10-25 21:15:58 +08:00
exceptions within the callback. `TransactionTemplate` triggers a rollback in case of
an unchecked application exception or if the transaction is marked rollback-only by
the application (by setting `TransactionStatus`). By default, `TransactionInterceptor`
behaves the same way but allows configurable rollback policies per method.
2015-03-03 18:38:01 +08:00
[[orm-hibernate-tx-strategies]]
2018-09-05 23:15:53 +08:00
==== Transaction Management Strategies
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Both `TransactionTemplate` and `TransactionInterceptor` delegate the actual transaction
2018-09-05 23:15:53 +08:00
handling to a `PlatformTransactionManager` instance (which can be a
`HibernateTransactionManager` (for a single Hibernate `SessionFactory`) by using a
2015-03-03 18:38:01 +08:00
`ThreadLocal` `Session` under the hood) or a `JtaTransactionManager` (delegating to the
JTA subsystem of the container) for Hibernate applications. You can even use a custom
`PlatformTransactionManager` implementation. Switching from native Hibernate transaction
2018-09-05 23:15:53 +08:00
management to JTA (such as when facing distributed transaction requirements for certain
deployments of your application) is only a matter of configuration. You can replace
2015-03-03 18:38:01 +08:00
the Hibernate transaction manager with Spring's JTA transaction implementation. Both
2018-09-05 23:15:53 +08:00
transaction demarcation and data access code work without changes, because they
use the generic transaction management APIs.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
For distributed transactions across multiple Hibernate session factories, you can combine
2015-03-03 18:38:01 +08:00
`JtaTransactionManager` as a transaction strategy with multiple
`LocalSessionFactoryBean` definitions. Each DAO then gets one specific `SessionFactory`
reference passed into its corresponding bean property. If all underlying JDBC data
sources are transactional container ones, a business service can demarcate transactions
across any number of DAOs and any number of session factories without special regard, as
2018-09-05 23:15:53 +08:00
long as it uses `JtaTransactionManager` as the strategy.
2015-03-03 18:38:01 +08:00
Both `HibernateTransactionManager` and `JtaTransactionManager` allow for proper
JVM-level cache handling with Hibernate, without container-specific transaction manager
2018-09-05 23:15:53 +08:00
lookup or a JCA connector (if you do not use EJB to initiate transactions).
2015-03-03 18:38:01 +08:00
`HibernateTransactionManager` can export the Hibernate JDBC `Connection` to plain JDBC
2018-09-05 23:15:53 +08:00
access code for a specific `DataSource`. This ability allows for high-level
2015-03-03 18:38:01 +08:00
transaction demarcation with mixed Hibernate and JDBC data access completely without
2018-09-05 23:15:53 +08:00
JTA, provided you access only one database. `HibernateTransactionManager` automatically
2015-03-03 18:38:01 +08:00
exposes the Hibernate transaction as a JDBC transaction if you have set up the passed-in
`SessionFactory` with a `DataSource` through the `dataSource` property of the
`LocalSessionFactoryBean` class. Alternatively, you can specify explicitly the
`DataSource` for which the transactions are supposed to be exposed through the
`dataSource` property of the `HibernateTransactionManager` class.
[[orm-hibernate-resources]]
2018-09-05 23:15:53 +08:00
==== Comparing Container-managed and Locally Defined Resources
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
You can switch between a container-managed JNDI `SessionFactory` and a locally defined
2018-09-05 23:15:53 +08:00
one without having to change a single line of application code. Whether to keep
2015-03-03 18:38:01 +08:00
resource definitions in the container or locally within the application is mainly a
matter of the transaction strategy that you use. Compared to a Spring-defined local
`SessionFactory`, a manually registered JNDI `SessionFactory` does not provide any
benefits. Deploying a `SessionFactory` through Hibernate's JCA connector provides the
added value of participating in the Java EE server's management infrastructure, but does
not add actual value beyond that.
2018-09-05 23:15:53 +08:00
Spring's transaction support is not bound to a container. When configured with any strategy
2015-03-03 18:38:01 +08:00
other than JTA, transaction support also works in a stand-alone or test environment.
Especially in the typical case of single-database transactions, Spring's single-resource
local transaction support is a lightweight and powerful alternative to JTA. When you use
local EJB stateless session beans to drive transactions, you depend both on an EJB
2018-09-05 23:15:53 +08:00
container and on JTA, even if you access only a single database and use only stateless
2015-03-03 18:38:01 +08:00
session beans to provide declarative transactions through container-managed
2018-09-05 23:15:53 +08:00
transactions. Direct use of JTA programmatically also requires a Java EE environment.
JTA does not involve only container dependencies in terms of JTA itself and of
2015-03-03 18:38:01 +08:00
JNDI `DataSource` instances. For non-Spring, JTA-driven Hibernate transactions, you have
2018-09-05 23:15:53 +08:00
to use the Hibernate JCA connector or extra Hibernate transaction code with the
2015-03-03 18:38:01 +08:00
`TransactionManagerLookup` configured for proper JVM-level caching.
Spring-driven transactions can work as well with a locally defined Hibernate
2018-09-05 23:15:53 +08:00
`SessionFactory` as they do with a local JDBC `DataSource`, provided they access a
single database. Thus, you need only use Spring's JTA transaction strategy when you
2015-03-03 18:38:01 +08:00
have distributed transaction requirements. A JCA connector requires container-specific
2018-09-05 23:15:53 +08:00
deployment steps, and (obviously) JCA support in the first place. This configuration
2015-03-03 18:38:01 +08:00
requires more work than deploying a simple web application with local resource
definitions and Spring-driven transactions. Also, you often need the Enterprise Edition
2018-09-05 23:15:53 +08:00
of your container if you use, for example, WebLogic Express, which does not
provide JCA. A Spring application with local resources and transactions that span one
single database works in any Java EE web container (without JTA, JCA, or EJB), such as
2015-03-03 18:38:01 +08:00
Tomcat, Resin, or even plain Jetty. Additionally, you can easily reuse such a middle
tier in desktop applications or test suites.
All things considered, if you do not use EJBs, stick with local `SessionFactory` setup
and Spring's `HibernateTransactionManager` or `JtaTransactionManager`. You get all of
the benefits, including proper transactional JVM-level caching and distributed
transactions, without the inconvenience of container deployment. JNDI registration of a
2018-09-05 23:15:53 +08:00
Hibernate `SessionFactory` through the JCA connector adds value only when used in
2015-03-03 18:38:01 +08:00
conjunction with EJBs.
[[orm-hibernate-invalid-jdbc-access-error]]
2018-09-05 23:15:53 +08:00
==== Spurious Application Server Warnings with Hibernate
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
In some JTA environments with very strict `XADataSource` implementations (currently
only some WebLogic Server and WebSphere versions), when Hibernate is configured without
regard to the JTA `PlatformTransactionManager` object for that environment,
spurious warning or exceptions can show up in the application server log.
2015-03-03 18:38:01 +08:00
These warnings or exceptions indicate that the connection being accessed is no longer
2018-09-05 23:15:53 +08:00
valid or JDBC access is no longer valid, possibly because the transaction is no longer
2015-03-03 18:38:01 +08:00
active. As an example, here is an actual exception from WebLogic:
[literal]
[subs="verbatim,quotes"]
----
java.sql.SQLException: The transaction is no longer active - status: 'Committed'. No
further JDBC access is allowed within this transaction.
----
2018-09-05 23:15:53 +08:00
You can resolve this warning by making Hibernate aware of the JTA
`PlatformTransactionManager` instance, to which it synchronizes (along with Spring).
2015-03-03 18:38:01 +08:00
You have two options for doing this:
2018-09-05 23:15:53 +08:00
* If, in your application context, you already directly obtain the JTA
2015-03-03 18:38:01 +08:00
`PlatformTransactionManager` object (presumably from JNDI through
2018-09-05 23:15:53 +08:00
`JndiObjectFactoryBean` or `<jee:jndi-lookup>`) and feed it, for example, to
Spring's `JtaTransactionManager`, the easiest way is to specify a reference to
the bean that defines this JTA `PlatformTransactionManager` instance as the value of the
2015-03-03 18:38:01 +08:00
`jtaTransactionManager` property for `LocalSessionFactoryBean.` Spring then makes the
object available to Hibernate.
2018-09-05 23:15:53 +08:00
* More likely, you do not already have the JTA `PlatformTransactionManager` instance,
because Spring's `JtaTransactionManager` can find it itself. Thus, you need to
2015-03-03 18:38:01 +08:00
configure Hibernate to look up JTA `PlatformTransactionManager` directly. You do this
2018-09-05 23:15:53 +08:00
by configuring an application server-specific `TransactionManagerLookup` class in the
2015-03-03 18:38:01 +08:00
Hibernate configuration, as described in the Hibernate manual.
The remainder of this section describes the sequence of events that occur with and
without Hibernate's awareness of the JTA `PlatformTransactionManager`.
When Hibernate is not configured with any awareness of the JTA
`PlatformTransactionManager`, the following events occur when a JTA transaction commits:
* The JTA transaction commits.
* Spring's `JtaTransactionManager` is synchronized to the JTA transaction, so it is
2018-09-05 23:15:53 +08:00
called back through an `afterCompletion` callback by the JTA transaction manager.
2015-03-03 18:38:01 +08:00
* Among other activities, this synchronization can trigger a callback by Spring to
Hibernate, through Hibernate's `afterTransactionCompletion` callback (used to clear
2018-09-05 23:15:53 +08:00
the Hibernate cache), followed by an explicit `close()` call on the Hibernate session,
2015-03-03 18:38:01 +08:00
which causes Hibernate to attempt to `close()` the JDBC Connection.
* In some environments, this `Connection.close()` call then triggers the warning or
2018-09-05 23:15:53 +08:00
error, as the application server no longer considers the `Connection` to be usable,
2015-03-03 18:38:01 +08:00
because the transaction has already been committed.
When Hibernate is configured with awareness of the JTA `PlatformTransactionManager`, the
following events occur when a JTA transaction commits:
2018-09-05 23:15:53 +08:00
* The JTA transaction is ready to commit.
2015-03-03 18:38:01 +08:00
* Spring's `JtaTransactionManager` is synchronized to the JTA transaction, so the
2018-09-05 23:15:53 +08:00
transaction is called back through a `beforeCompletion` callback by the JTA
2015-03-03 18:38:01 +08:00
transaction manager.
2018-09-05 23:15:53 +08:00
* Spring is aware that Hibernate itself is synchronized to the JTA transaction and
2015-03-03 18:38:01 +08:00
behaves differently than in the previous scenario. Assuming the Hibernate `Session`
2018-09-05 23:15:53 +08:00
needs to be closed at all, Spring closes it now.
2015-03-03 18:38:01 +08:00
* The JTA transaction commits.
* Hibernate is synchronized to the JTA transaction, so the transaction is called back
2018-09-05 23:15:53 +08:00
through an `afterCompletion` callback by the JTA transaction manager and can
2015-03-03 18:38:01 +08:00
properly clear its cache.
[[orm-jpa]]
=== JPA
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
The Spring JPA, available under the `org.springframework.orm.jpa` package, offers
comprehensive support for the
2019-03-21 06:48:14 +08:00
https://www.oracle.com/technetwork/articles/javaee/jpa-137156.html[Java Persistence
2018-09-05 23:15:53 +08:00
API] in a manner similar to the integration with Hibernate while being aware of
2015-03-03 18:38:01 +08:00
the underlying implementation in order to provide additional features.
[[orm-jpa-setup]]
2018-09-05 23:15:53 +08:00
==== Three Options for JPA Setup in a Spring Environment
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
The Spring JPA support offers three ways of setting up the JPA `EntityManagerFactory`
2018-09-05 23:15:53 +08:00
that is used by the application to obtain an entity manager.
* <<orm-jpa-setup-lemfb>>
* <<orm-jpa-setup-jndi>>
* <<orm-jpa-setup-lcemfb>>
2015-03-03 18:38:01 +08:00
[[orm-jpa-setup-lemfb]]
2018-09-05 23:15:53 +08:00
===== Using `LocalEntityManagerFactoryBean`
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
You can use this option only in simple deployment environments such as stand-alone applications
2015-03-03 18:38:01 +08:00
and integration tests.
The `LocalEntityManagerFactoryBean` creates an `EntityManagerFactory` suitable for
simple deployment environments where the application uses only JPA for data access. The
2018-09-05 23:15:53 +08:00
factory bean uses the JPA `PersistenceProvider` auto-detection mechanism (according to
2015-03-03 18:38:01 +08:00
JPA's Java SE bootstrapping) and, in most cases, requires you to specify only the
2018-09-05 23:15:53 +08:00
persistence unit name. The following XML example configures such a bean:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<bean id="myEmf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myPersistenceUnit"/>
</bean>
</beans>
----
This form of JPA deployment is the simplest and the most limited. You cannot refer to an
2018-09-05 23:15:53 +08:00
existing JDBC `DataSource` bean definition, and no support for global transactions
2015-03-03 18:38:01 +08:00
exists. Furthermore, weaving (byte-code transformation) of persistent classes is
provider-specific, often requiring a specific JVM agent to specified on startup. This
option is sufficient only for stand-alone applications and test environments, for which
the JPA specification is designed.
[[orm-jpa-setup-jndi]]
===== Obtaining an EntityManagerFactory from JNDI
2018-09-05 23:15:53 +08:00
You can use this option when deploying to a Java EE server. Check your server's documentation
2015-03-03 18:38:01 +08:00
on how to deploy a custom JPA provider into your server, allowing for a different
provider than the server's default.
2016-12-09 01:24:12 +08:00
Obtaining an `EntityManagerFactory` from JNDI (for example in a Java EE environment),
2018-09-05 23:15:53 +08:00
is a matter of changing the XML configuration, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<jee:jndi-lookup id="myEmf" jndi-name="persistence/myPersistenceUnit"/>
</beans>
----
2018-09-05 23:15:53 +08:00
This action assumes standard Java EE bootstrapping. The Java EE server auto-detects
2015-03-03 18:38:01 +08:00
persistence units (in effect, `META-INF/persistence.xml` files in application jars) and
`persistence-unit-ref` entries in the Java EE deployment descriptor (for example,
`web.xml`) and defines environment naming context locations for those persistence units.
In such a scenario, the entire persistence unit deployment, including the weaving
(byte-code transformation) of persistent classes, is up to the Java EE server. The JDBC
2018-09-05 23:15:53 +08:00
`DataSource` is defined through a JNDI location in the `META-INF/persistence.xml` file.
`EntityManager` transactions are integrated with the server's JTA subsystem. Spring merely
2015-03-03 18:38:01 +08:00
uses the obtained `EntityManagerFactory`, passing it on to application objects through
2018-09-05 23:15:53 +08:00
dependency injection and managing transactions for the persistence unit (typically
through `JtaTransactionManager`).
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
If you use multiple persistence units in the same application, the bean names of such
2015-03-03 18:38:01 +08:00
JNDI-retrieved persistence units should match the persistence unit names that the
2018-09-05 23:15:53 +08:00
application uses to refer to them (for example, in `@PersistenceUnit` and
`@PersistenceContext` annotations).
2015-03-03 18:38:01 +08:00
[[orm-jpa-setup-lcemfb]]
2018-09-05 23:15:53 +08:00
===== Using `LocalContainerEntityManagerFactoryBean`
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
You can use this option for full JPA capabilities in a Spring-based application environment.
This includes web containers such as Tomcat, stand-alone applications, and
2015-03-03 18:38:01 +08:00
integration tests with sophisticated persistence requirements.
2018-07-04 21:07:09 +08:00
2018-09-05 23:15:53 +08:00
NOTE: If you want to specifically configure a Hibernate setup, an immediate alternative is
to go with Hibernate 5.2 or 5.3 and set up a native Hibernate `LocalSessionFactoryBean`
2018-07-04 21:07:09 +08:00
instead of a plain JPA `LocalContainerEntityManagerFactoryBean`, letting it interact
with JPA access code as well as native Hibernate access code.
2019-03-05 20:08:34 +08:00
See <<orm-jpa-hibernate, Native Hibernate setup for JPA interaction>> for details.
2015-03-03 18:38:01 +08:00
The `LocalContainerEntityManagerFactoryBean` gives full control over
`EntityManagerFactory` configuration and is appropriate for environments where
fine-grained customization is required. The `LocalContainerEntityManagerFactoryBean`
creates a `PersistenceUnitInfo` instance based on the `persistence.xml` file, the
2018-09-05 23:15:53 +08:00
supplied `dataSourceLookup` strategy, and the specified `loadTimeWeaver`. It is, thus,
2015-03-03 18:38:01 +08:00
possible to work with custom data sources outside of JNDI and to control the weaving
process. The following example shows a typical bean definition for a
`LocalContainerEntityManagerFactoryBean`:
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="someDataSource"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
</beans>
----
The following example shows a typical `persistence.xml` file:
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="myUnit" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/orm.xml</mapping-file>
<exclude-unlisted-classes/>
</persistence-unit>
</persistence>
----
2018-09-05 23:15:53 +08:00
NOTE: The `<exclude-unlisted-classes/>` shortcut indicates that no scanning for
annotated entity classes is supposed to occur. An explicit 'true' value
(`<exclude-unlisted-classes>true</exclude-unlisted-classes/>`) also means no scan.
`<exclude-unlisted-classes>false</exclude-unlisted-classes/>` does trigger a scan.
However, we recommend omitting the `exclude-unlisted-classes` element
2015-03-03 18:38:01 +08:00
if you want entity class scanning to occur.
Using the `LocalContainerEntityManagerFactoryBean` is the most powerful JPA setup
option, allowing for flexible local configuration within the application. It supports
links to an existing JDBC `DataSource`, supports both local and global transactions, and
so on. However, it also imposes requirements on the runtime environment, such as the
availability of a weaving-capable class loader if the persistence provider demands
byte-code transformation.
2016-12-09 01:24:12 +08:00
This option may conflict with the built-in JPA capabilities of a Java EE server. In a
full Java EE environment, consider obtaining your `EntityManagerFactory` from JNDI.
2015-03-03 18:38:01 +08:00
Alternatively, specify a custom `persistenceXmlLocation` on your
2018-09-05 23:15:53 +08:00
`LocalContainerEntityManagerFactoryBean` definition (for example,
META-INF/my-persistence.xml) and include only a descriptor with that name in your
application jar files. Because the Java EE server looks only for default
`META-INF/persistence.xml` files, it ignores such custom persistence units and, hence,
avoids conflicts with a Spring-driven JPA setup upfront. (This applies to Resin 3.1, for
2015-03-03 18:38:01 +08:00
example.)
.When is load-time weaving required?
****
2016-12-09 01:24:12 +08:00
Not all JPA providers require a JVM agent. Hibernate is an example of one that does not.
If your provider does not require an agent or you have other alternatives, such as
2018-09-05 23:15:53 +08:00
applying enhancements at build time through a custom compiler or an Ant task, you should not use the
load-time weaver.
2015-03-03 18:38:01 +08:00
****
2018-09-05 23:15:53 +08:00
The `LoadTimeWeaver` interface is a Spring-provided class that lets JPA
`ClassTransformer` instances be plugged in a specific manner, depending on whether the
2015-03-03 18:38:01 +08:00
environment is a web container or application server. Hooking `ClassTransformers`
through an
2019-03-21 06:48:14 +08:00
https://docs.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html[agent]
2018-09-05 23:15:53 +08:00
is typically not efficient. The agents work against the entire virtual machine and
inspect every class that is loaded, which is usually undesirable in a production
2015-03-03 18:38:01 +08:00
server environment.
Spring provides a number of `LoadTimeWeaver` implementations for various environments,
2018-09-05 23:15:53 +08:00
letting `ClassTransformer` instances be applied only for each class loader and not
for each VM.
2015-03-03 18:38:01 +08:00
2019-03-05 20:08:34 +08:00
See the <<core.adoc#aop-aj-ltw-spring, Spring configuration>> in the AOP chapter for
2017-03-29 20:20:12 +08:00
more insight regarding the `LoadTimeWeaver` implementations and their setup, either
2019-03-05 20:08:34 +08:00
generic or customized to various platforms (such as Tomcat, JBoss and WebSphere).
2018-09-05 23:15:53 +08:00
2019-03-05 20:08:34 +08:00
As described in <<core.adoc#aop-aj-ltw-spring, Spring configuration>>, you can configure
a context-wide `LoadTimeWeaver` by using the `@EnableLoadTimeWeaving` annotation of the
2019-08-12 17:21:54 +08:00
`context:load-time-weaver` XML element. Such a global weaver is automatically picked up
2019-03-05 20:08:34 +08:00
by all JPA `LocalContainerEntityManagerFactoryBean` instances. The following example
shows the preferred way of setting up a load-time weaver, delivering auto-detection
of the platform (e.g. Tomcat's weaving-capable class loader or Spring's JVM agent)
and automatic propagation of the weaver to all weaver-aware beans:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<context:load-time-weaver/>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
...
</bean>
----
2018-09-05 23:15:53 +08:00
However, you can, if needed, manually specify a dedicated weaver through the
`loadTimeWeaver` property, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
</property>
</bean>
----
2018-09-05 23:15:53 +08:00
No matter how the LTW is configured, by using this technique, JPA applications relying on
instrumentation can run in the target platform (for example, Tomcat) without needing an agent.
This is especially important when the hosting applications rely on different JPA
implementations, because the JPA transformers are applied only at the class-loader level and
are, thus, isolated from each other.
2018-08-16 23:30:44 +08:00
[[orm-jpa-setup-multiple]]
2018-09-05 23:15:53 +08:00
===== Dealing with Multiple Persistence Units
2017-10-24 07:01:42 +08:00
2018-09-05 23:15:53 +08:00
For applications that rely on multiple persistence units locations (stored in various
JARS in the classpath, for example), Spring offers the `PersistenceUnitManager` to act as
2015-03-03 18:38:01 +08:00
a central repository and to avoid the persistence units discovery process, which can be
2018-09-05 23:15:53 +08:00
expensive. The default implementation lets multiple locations be specified. These locations are
2015-03-03 18:38:01 +08:00
parsed and later retrieved through the persistence unit name. (By default, the classpath
2018-09-05 23:15:53 +08:00
is searched for `META-INF/persistence.xml` files.) The following example configures
multiple locations:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim"]
2015-03-03 18:38:01 +08:00
----
<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>org/springframework/orm/jpa/domain/persistence-multi.xml</value>
<value>classpath:/my/package/**/custom-persistence.xml</value>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="dataSources">
<map>
<entry key="localDataSource" value-ref="local-db"/>
<entry key="remoteDataSource" value-ref="remote-db"/>
</map>
</property>
<!-- if no datasource is specified, use this one -->
<property name="defaultDataSource" ref="remoteDataSource"/>
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="pum"/>
<property name="persistenceUnitName" value="myCustomUnit"/>
</bean>
----
2018-09-05 23:15:53 +08:00
The default implementation allows customization of the `PersistenceUnitInfo` instances
(before they are fed to the JPA provider) either declaratively (through its properties, which
affect all hosted units) or programmatically (through the
`PersistenceUnitPostProcessor`, which allows persistence unit selection). If no
2015-03-03 18:38:01 +08:00
`PersistenceUnitManager` is specified, one is created and used internally by
`LocalContainerEntityManagerFactoryBean`.
2018-08-16 23:30:44 +08:00
[[orm-jpa-setup-background]]
2018-09-05 23:15:53 +08:00
===== Background Bootstrapping
2018-08-16 23:30:44 +08:00
`LocalContainerEntityManagerFactoryBean` supports background bootstrapping through
2018-09-05 23:15:53 +08:00
the `bootstrapExecutor` property, as the following example shows:
2018-08-16 23:30:44 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2018-08-16 23:30:44 +08:00
----
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="bootstrapExecutor">
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
</property>
</bean>
----
2018-09-05 23:15:53 +08:00
The actual JPA provider bootstrapping is handed off to the specified executor and then,
running in parallel, to the application bootstrap thread. The exposed `EntityManagerFactory`
proxy can be injected into other application components and is even able to respond to
2018-08-16 23:30:44 +08:00
`EntityManagerFactoryInfo` configuration inspection. However, once the actual JPA provider
2018-09-05 23:15:53 +08:00
is being accessed by other components (for example, calling `createEntityManager`), those calls
block until the background bootstrapping has completed. In particular, when you use
2018-08-16 23:30:44 +08:00
Spring Data JPA, make sure to set up deferred bootstrapping for its repositories as well.
2015-03-03 18:38:01 +08:00
2016-12-09 01:24:12 +08:00
[[orm-jpa-dao]]
2018-09-05 23:15:53 +08:00
==== Implementing DAOs Based on JPA: `EntityManagerFactory` and `EntityManager`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
NOTE: Although `EntityManagerFactory` instances are thread-safe, `EntityManager` instances are
2015-03-03 18:38:01 +08:00
not. The injected JPA `EntityManager` behaves like an `EntityManager` fetched from an
application server's JNDI environment, as defined by the JPA specification. It delegates
2018-09-05 23:15:53 +08:00
all calls to the current transactional `EntityManager`, if any. Otherwise, it falls back
2015-03-03 18:38:01 +08:00
to a newly created `EntityManager` per operation, in effect making its usage thread-safe.
It is possible to write code against the plain JPA without any Spring dependencies, by
2018-09-05 23:15:53 +08:00
using an injected `EntityManagerFactory` or `EntityManager`. Spring can understand the
`@PersistenceUnit` and `@PersistenceContext` annotations both at the field and the method level
if a `PersistenceAnnotationBeanPostProcessor` is enabled. The following example shows a plain JPA DAO implementation
that uses the `@PersistenceUnit` annotation:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class ProductDaoImpl implements ProductDao {
private EntityManagerFactory emf;
@PersistenceUnit
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.emf = emf;
}
public Collection loadProductsByCategory(String category) {
2019-01-18 01:53:59 +08:00
try (EntityManager em = this.emf.createEntityManager()) {
2015-03-03 18:38:01 +08:00
Query query = em.createQuery("from Product as p where p.category = ?1");
query.setParameter(1, category);
return query.getResultList();
}
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class ProductDaoImpl : ProductDao {
private lateinit var emf: EntityManagerFactory
@PersistenceUnit
fun setEntityManagerFactory(emf: EntityManagerFactory) {
this.emf = emf
}
fun loadProductsByCategory(category: String): Collection<*> {
val em = this.emf.createEntityManager()
val query = em.createQuery("from Product as p where p.category = ?1");
query.setParameter(1, category);
return query.resultList;
}
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The preceding DAO has no dependency on Spring and still fits nicely into a Spring
2015-03-03 18:38:01 +08:00
application context. Moreover, the DAO takes advantage of annotations to require the
2018-09-05 23:15:53 +08:00
injection of the default `EntityManagerFactory`, as the following example bean definition shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<!-- bean post-processor for JPA annotations -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="myProductDao" class="product.ProductDaoImpl"/>
</beans>
----
2018-09-05 23:15:53 +08:00
As an alternative to explicitly defining a `PersistenceAnnotationBeanPostProcessor`,
2015-03-03 18:38:01 +08:00
consider using the Spring `context:annotation-config` XML element in your application
context configuration. Doing so automatically registers all Spring standard
post-processors for annotation-based configuration, including
`CommonAnnotationBeanPostProcessor` and so on.
2018-09-05 23:15:53 +08:00
Consider the following example:
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<!-- post-processors for all standard config annotations -->
<context:annotation-config/>
<bean id="myProductDao" class="product.ProductDaoImpl"/>
</beans>
----
The main problem with such a DAO is that it always creates a new `EntityManager` through
the factory. You can avoid this by requesting a transactional `EntityManager` (also
2018-09-05 23:15:53 +08:00
called a "`shared EntityManager`" because it is a shared, thread-safe proxy for the actual
transactional EntityManager) to be injected instead of the factory. The following example shows how to do so:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class ProductDaoImpl implements ProductDao {
@PersistenceContext
private EntityManager em;
public Collection loadProductsByCategory(String category) {
Query query = em.createQuery("from Product as p where p.category = :category");
query.setParameter("category", category);
return query.getResultList();
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class ProductDaoImpl : ProductDao {
@PersistenceContext
private lateinit var em: EntityManager
fun loadProductsByCategory(category: String): Collection<*> {
val query = em.createQuery("from Product as p where p.category = :category")
query.setParameter("category", category)
return query.resultList
}
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The `@PersistenceContext` annotation has an optional attribute called `type`, which defaults to
`PersistenceContextType.TRANSACTION`. You can use this default to receive a shared
`EntityManager` proxy. The alternative, `PersistenceContextType.EXTENDED`, is a completely
different affair. This results in a so-called extended `EntityManager`, which is not
thread-safe and, hence, must not be used in a concurrently accessed component, such as a
Spring-managed singleton bean. Extended `EntityManager` instances are only supposed to be used in
2015-03-03 18:38:01 +08:00
stateful components that, for example, reside in a session, with the lifecycle of the
2018-09-05 23:15:53 +08:00
`EntityManager` not tied to a current transaction but rather being completely up to the
2015-03-03 18:38:01 +08:00
application.
.Method- and field-level Injection
****
2018-09-05 23:15:53 +08:00
You can apply annotations that indicate dependency injections (such as `@PersistenceUnit` and
`@PersistenceContext`) on field or methods inside a class -- hence the
expressions "`method-level injection`" and "`field-level injection`". Field-level
annotations are concise and easier to use while method-level annotations allow for further
processing of the injected dependency. In both cases, the member visibility (public,
protected, or private) does not matter.
2015-03-03 18:38:01 +08:00
What about class-level annotations?
2016-12-09 01:24:12 +08:00
On the Java EE platform, they are used for dependency declaration and not for resource
2015-03-03 18:38:01 +08:00
injection.
****
2018-09-05 23:15:53 +08:00
The injected `EntityManager` is Spring-managed (aware of the ongoing transaction).
Even though the new DAO implementation uses method-level
2015-03-03 18:38:01 +08:00
injection of an `EntityManager` instead of an `EntityManagerFactory`, no change is
2018-09-05 23:15:53 +08:00
required in the application context XML, due to annotation usage.
2015-03-03 18:38:01 +08:00
2019-03-05 20:08:34 +08:00
The main advantage of this DAO style is that it depends only on the Java Persistence API.
No import of any Spring class is required. Moreover, as the JPA annotations are understood,
2015-03-03 18:38:01 +08:00
the injections are applied automatically by the Spring container. This is appealing from
2018-09-05 23:15:53 +08:00
a non-invasiveness perspective and can feel more natural to JPA developers.
2015-03-03 18:38:01 +08:00
[[orm-jpa-tx]]
2016-12-09 01:24:12 +08:00
==== Spring-driven JPA transactions
2019-03-05 20:08:34 +08:00
NOTE: We strongly encourage you to read <<transaction-declarative>>, if you have not
already done so, to get more detailed coverage of Spring's declarative transaction support.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The recommended strategy for JPA is local transactions through JPA's native transaction
2016-12-09 01:24:12 +08:00
support. Spring's `JpaTransactionManager` provides many capabilities known from local
2018-09-05 23:15:53 +08:00
JDBC transactions (such as transaction-specific isolation levels and resource-level
read-only optimizations) against any regular JDBC connection pool (no XA requirement).
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
Spring JPA also lets a configured `JpaTransactionManager` expose a JPA transaction
2016-12-09 01:24:12 +08:00
to JDBC access code that accesses the same `DataSource`, provided that the registered
2018-09-05 23:15:53 +08:00
`JpaDialect` supports retrieval of the underlying JDBC `Connection`.
2016-07-05 05:34:48 +08:00
Spring provides dialects for the EclipseLink and Hibernate JPA implementations.
2019-03-05 20:08:34 +08:00
See the <<orm-jpa-dialect, next section>> for details on the `JpaDialect` mechanism.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
NOTE: As an immediate alternative, Spring's native `HibernateTransactionManager` is capable
2018-07-04 21:07:09 +08:00
of interacting with JPA access code as of Spring Framework 5.1 and Hibernate 5.2/5.3,
2018-09-05 23:15:53 +08:00
adapting to several Hibernate specifics and providing JDBC interaction.
2018-07-04 21:07:09 +08:00
This makes particular sense in combination with `LocalSessionFactoryBean` setup.
2019-03-05 20:08:34 +08:00
See <<orm-jpa-hibernate, Native Hibernate Setup for JPA Interaction>> for details.
2018-09-05 23:15:53 +08:00
2018-07-04 21:07:09 +08:00
2015-03-03 18:38:01 +08:00
[[orm-jpa-dialect]]
2018-09-05 23:15:53 +08:00
==== Understanding `JpaDialect` and `JpaVendorAdapter`
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
As an advanced feature, `JpaTransactionManager` and subclasses of
`AbstractEntityManagerFactoryBean` allow a custom `JpaDialect` to be passed into the
`jpaDialect` bean property. A `JpaDialect` implementation can enable the following advanced
2015-05-06 02:18:51 +08:00
features supported by Spring, usually in a vendor-specific manner:
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
* Applying specific transaction semantics (such as custom isolation level or transaction
2015-03-03 18:38:01 +08:00
timeout)
2018-09-05 23:15:53 +08:00
* Retrieving the transactional JDBC `Connection` (for exposure to JDBC-based DAOs)
2015-03-03 18:38:01 +08:00
* Advanced translation of `PersistenceExceptions` to Spring `DataAccessExceptions`
This is particularly valuable for special transaction semantics and for advanced
2018-09-05 23:15:53 +08:00
translation of exception. The default implementation (`DefaultJpaDialect`) does
not provide any special abilities and, if the features listed earlier are required, you have
2016-12-09 01:24:12 +08:00
to specify the appropriate dialect.
2018-09-05 23:15:53 +08:00
TIP: As an even broader provider adaptation facility primarily for Spring's full-featured
2016-12-09 01:24:12 +08:00
`LocalContainerEntityManagerFactoryBean` setup, `JpaVendorAdapter` combines the
capabilities of `JpaDialect` with other provider-specific defaults. Specifying a
`HibernateJpaVendorAdapter` or `EclipseLinkJpaVendorAdapter` is the most convenient
way of auto-configuring an `EntityManagerFactory` setup for Hibernate or EclipseLink,
respectively. Note that those provider adapters are primarily designed for use with
2018-09-05 23:15:53 +08:00
Spring-driven transaction management (that is, for use with `JpaTransactionManager`).
2016-12-09 01:24:12 +08:00
2018-10-25 21:15:58 +08:00
See the {api-spring-framework}/orm/jpa/JpaDialect.html[`JpaDialect`] and
{api-spring-framework}/orm/jpa/JpaVendorAdapter.html[`JpaVendorAdapter`] javadoc for
more details of its operations and how they are used within Spring's JPA support.
2016-12-09 01:24:12 +08:00
2018-09-05 23:15:53 +08:00
2016-12-09 01:24:12 +08:00
[[orm-jpa-jta]]
2018-09-05 23:15:53 +08:00
==== Setting up JPA with JTA Transaction Management
2016-12-09 01:24:12 +08:00
As an alternative to `JpaTransactionManager`, Spring also allows for multi-resource
2018-09-05 23:15:53 +08:00
transaction coordination through JTA, either in a Java EE environment or with a
stand-alone transaction coordinator, such as Atomikos. Aside from choosing Spring's
`JtaTransactionManager` instead of `JpaTransactionManager`, you need to take few further
steps:
2016-12-09 01:24:12 +08:00
2018-09-05 23:15:53 +08:00
* The underlying JDBC connection pools need to be XA-capable and be integrated with
2016-12-09 01:24:12 +08:00
your transaction coordinator. This is usually straightforward in a Java EE environment,
2018-09-05 23:15:53 +08:00
exposing a different kind of `DataSource` through JNDI. See your application server
2016-12-09 01:24:12 +08:00
documentation for details. Analogously, a standalone transaction coordinator usually
2018-09-05 23:15:53 +08:00
comes with special XA-integrated `DataSource` implementations. Again, check its documentation.
2016-12-09 01:24:12 +08:00
* The JPA `EntityManagerFactory` setup needs to be configured for JTA. This is
2018-09-05 23:15:53 +08:00
provider-specific, typically through special properties to be specified as `jpaProperties`
2016-12-09 01:24:12 +08:00
on `LocalContainerEntityManagerFactoryBean`. In the case of Hibernate, these properties
2018-09-05 23:15:53 +08:00
are even version-specific. See your Hibernate documentation for details.
* Spring's `HibernateJpaVendorAdapter` enforces certain Spring-oriented defaults, such
as the connection release mode, `on-close`, which matches Hibernate's own default in
Hibernate 5.0 but not any more in 5.1/5.2. For a JTA setup, either do not declare
`HibernateJpaVendorAdapter` to begin with or turn off its `prepareConnection` flag.
Alternatively, set Hibernate 5.2's `hibernate.connection.handling_mode` property to
`DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT` to restore Hibernate's own default.
2016-12-09 01:24:12 +08:00
See <<orm-hibernate-invalid-jdbc-access-error>> for a related note about WebLogic.
* Alternatively, consider obtaining the `EntityManagerFactory` from your application
2018-09-05 23:15:53 +08:00
server itself (that is, through a JNDI lookup instead of a locally declared
`LocalContainerEntityManagerFactoryBean`). A server-provided `EntityManagerFactory`
might require special definitions in your server configuration (making the deployment
less portable) but is set up for the server's JTA environment.
2016-12-09 01:24:12 +08:00
2018-07-04 21:07:09 +08:00
[[orm-jpa-hibernate]]
2018-09-05 23:15:53 +08:00
==== Native Hibernate Setup and Native Hibernate Transactions for JPA Interaction
2018-07-04 21:07:09 +08:00
As of Spring Framework 5.1 and Hibernate 5.2/5.3, a native `LocalSessionFactoryBean`
setup in combination with `HibernateTransactionManager` allows for interaction with
2018-09-05 23:15:53 +08:00
`@PersistenceContext` and other JPA access code. A Hibernate
`SessionFactory` natively implements JPA's `EntityManagerFactory` interface now
and a Hibernate `Session` handle natively is a JPA `EntityManager`.
Spring's JPA support facilities automatically detect native Hibernate sessions.
2018-07-04 21:07:09 +08:00
2018-09-05 23:15:53 +08:00
Such native Hibernate setup can, therefore, serve as a replacement for a standard JPA
2018-07-04 21:07:09 +08:00
`LocalContainerEntityManagerFactoryBean` and `JpaTransactionManager` combination
in many scenarios, allowing for interaction with `SessionFactory.getCurrentSession()`
(and also `HibernateTemplate`) next to `@PersistenceContext EntityManager` within
the same local transaction. Such a setup also provides stronger Hibernate integration
2018-09-05 23:15:53 +08:00
and more configuration flexibility, because it is not constrained by JPA bootstrap contracts.
2018-07-04 21:07:09 +08:00
2018-09-05 23:15:53 +08:00
You do not need `HibernateJpaVendorAdapter` configuration in such a scenario,
since Spring's native Hibernate setup provides even more features
(for example, custom Hibernate Integrator setup, Hibernate 5.3 bean container integration,
and stronger optimizations for read-only transactions). Last but not least, you can also
express native Hibernate setup through `LocalSessionFactoryBuilder`,
2018-07-04 21:07:09 +08:00
seamlessly integrating with `@Bean` style configuration (no `FactoryBean` involved).
2018-08-16 23:30:44 +08:00
[NOTE]
====
`LocalSessionFactoryBean` and `LocalSessionFactoryBuilder` support background
2018-09-05 23:15:53 +08:00
bootstrapping, just as the JPA `LocalContainerEntityManagerFactoryBean` does.
2019-03-05 20:08:34 +08:00
See <<orm-jpa-setup-background, Background Bootstrapping>> for an introduction.
2018-08-16 23:30:44 +08:00
2018-09-05 23:15:53 +08:00
On `LocalSessionFactoryBean`, this is available through the `bootstrapExecutor`
property. On the programmatic `LocalSessionFactoryBuilder`, an overloaded
`buildSessionFactory` method takes a bootstrap executor argument.
2018-08-16 23:30:44 +08:00
====
2018-07-04 21:07:09 +08:00
2015-03-03 18:38:01 +08:00
2018-10-25 21:15:58 +08:00
2015-03-03 18:38:01 +08:00
[[oxm]]
2018-09-05 23:15:53 +08:00
== Marshalling XML by Using Object-XML Mappers
2015-03-03 18:38:01 +08:00
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
[[oxm-introduction]]
=== Introduction
2017-10-24 07:01:42 +08:00
2018-09-05 23:15:53 +08:00
This chapter, describes Spring's Object-XML Mapping support. Object-XML
Mapping (O-X mapping for short) is the act of converting an XML document to and from
2015-03-03 18:38:01 +08:00
an object. This conversion process is also known as XML Marshalling, or XML
Serialization. This chapter uses these terms interchangeably.
2018-09-05 23:15:53 +08:00
Within the field of O-X mapping, a marshaller is responsible for serializing an
object (graph) to XML. In similar fashion, an unmarshaller deserializes the XML to
2015-03-03 18:38:01 +08:00
an object graph. This XML can take the form of a DOM document, an input or output
stream, or a SAX handler.
Some of the benefits of using Spring for your O/X mapping needs are:
2018-09-05 23:15:53 +08:00
* <<oxm-ease-of-configuration>>
* <<oxm-consistent-interfaces>>
* <<oxm-consistent-exception-hierarchy>>
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
[[oxm-ease-of-configuration]]
2015-03-03 18:38:01 +08:00
==== Ease of configuration
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Spring's bean factory makes it easy to configure marshallers, without needing to
2018-09-05 23:15:53 +08:00
construct JAXB context, JiBX binding factories, and so on. You can configure the marshallers
as you would any other bean in your application context. Additionally, XML namespace-based
2015-03-03 18:38:01 +08:00
configuration is available for a number of marshallers, making the configuration even
simpler.
2018-09-05 23:15:53 +08:00
[[oxm-consistent-interfaces]]
==== Consistent Interfaces
Spring's O-X mapping operates through two global interfaces: {api-spring-framework}/oxm/Marshaller.html[`Marshaller`] and
{api-spring-framework}/oxm/Unmarshaller.html[`Unmarshaller`]. These abstractions let you switch O-X mapping frameworks
with relative ease, with little or no change required on the classes that do the
2015-03-03 18:38:01 +08:00
marshalling. This approach has the additional benefit of making it possible to do XML
2018-09-05 23:15:53 +08:00
marshalling with a mix-and-match approach (for example, some marshalling performed using JAXB
2019-01-14 18:25:27 +08:00
and some by XStream) in a non-intrusive fashion, letting you use the strength of each
2015-03-03 18:38:01 +08:00
technology.
2018-09-05 23:15:53 +08:00
[[oxm-consistent-exception-hierarchy]]
==== Consistent Exception Hierarchy
Spring provides a conversion from exceptions from the underlying O-X mapping tool to its
own exception hierarchy with the `XmlMappingException` as the root exception.
These runtime exceptions wrap the original exception so that no information is lost.
2015-03-03 18:38:01 +08:00
[[oxm-marshaller-unmarshaller]]
2018-09-05 23:15:53 +08:00
=== `Marshaller` and `Unmarshaller`
2017-10-19 02:24:17 +08:00
2019-03-05 20:08:34 +08:00
As stated in the <<oxm-introduction, introduction>>, a marshaller serializes an object
to XML, and an unmarshaller deserializes XML stream to an object. This section describes
2015-03-03 18:38:01 +08:00
the two Spring interfaces used for this purpose.
[[oxm-marshaller]]
2018-09-05 23:15:53 +08:00
==== Understanding `Marshaller`
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
Spring abstracts all marshalling operations behind the
2018-09-05 23:15:53 +08:00
`org.springframework.oxm.Marshaller` interface, the main method of which follows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public interface Marshaller {
/**
2016-12-09 01:24:12 +08:00
* Marshal the object graph with the given root into the provided Result.
2015-03-03 18:38:01 +08:00
*/
void marshal(Object graph, Result result) throws XmlMappingException, IOException;
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
interface Marshaller {
/**
* Marshal the object graph with the given root into the provided Result.
*/
@Throws(XmlMappingException::class, IOException::class)
fun marshal(
graph: Any,
result: Result
)
}
----
2015-03-03 18:38:01 +08:00
The `Marshaller` interface has one main method, which marshals the given object to a
2018-09-05 23:15:53 +08:00
given `javax.xml.transform.Result`. The result is a tagging interface that basically
represents an XML output abstraction. Concrete implementations wrap various XML
representations, as the following table indicates:
2015-03-03 18:38:01 +08:00
[[oxm-marshller-tbl]]
|===
| Result implementation| Wraps XML representation
| `DOMResult`
| `org.w3c.dom.Node`
| `SAXResult`
| `org.xml.sax.ContentHandler`
| `StreamResult`
| `java.io.File`, `java.io.OutputStream`, or `java.io.Writer`
|===
2018-09-05 23:15:53 +08:00
NOTE: Although the `marshal()` method accepts a plain object as its first parameter, most
2015-03-03 18:38:01 +08:00
`Marshaller` implementations cannot handle arbitrary objects. Instead, an object class
2018-09-05 23:15:53 +08:00
must be mapped in a mapping file, be marked with an annotation, be registered with the
marshaller, or have a common base class. Refer to the later sections in this chapter
to determine how your O-X technology manages this.
2015-03-03 18:38:01 +08:00
[[oxm-unmarshaller]]
2018-09-05 23:15:53 +08:00
==== Understanding `Unmarshaller`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Similar to the `Marshaller`, we have the `org.springframework.oxm.Unmarshaller`
interface, which the following listing shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public interface Unmarshaller {
/**
2016-12-09 01:24:12 +08:00
* Unmarshal the given provided Source into an object graph.
2015-03-03 18:38:01 +08:00
*/
Object unmarshal(Source source) throws XmlMappingException, IOException;
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
interface Unmarshaller {
/**
* Unmarshal the given provided Source into an object graph.
*/
@Throws(XmlMappingException::class, IOException::class)
fun unmarshal(source: Source): Any
}
----
2015-03-03 18:38:01 +08:00
This interface also has one method, which reads from the given
2018-09-05 23:15:53 +08:00
`javax.xml.transform.Source` (an XML input abstraction) and returns the object read. As
with `Result`, `Source` is a tagging interface that has three concrete implementations. Each
wraps a different XML representation, as the following table indicates:
2015-03-03 18:38:01 +08:00
[[oxm-unmarshller-tbl]]
|===
| Source implementation| Wraps XML representation
| `DOMSource`
| `org.w3c.dom.Node`
| `SAXSource`
| `org.xml.sax.InputSource`, and `org.xml.sax.XMLReader`
| `StreamSource`
| `java.io.File`, `java.io.InputStream`, or `java.io.Reader`
|===
2018-09-27 02:17:07 +08:00
Even though there are two separate marshalling interfaces (`Marshaller` and
2018-09-05 23:15:53 +08:00
`Unmarshaller`), all implementations in Spring-WS implement both in one class.
2015-03-03 18:38:01 +08:00
This means that you can wire up one marshaller class and refer to it both as a
2018-09-05 23:15:53 +08:00
marshaller and as an unmarshaller in your `applicationContext.xml`.
2015-03-03 18:38:01 +08:00
[[oxm-xmlmappingexception]]
2018-09-05 23:15:53 +08:00
==== Understanding `XmlMappingException`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
Spring converts exceptions from the underlying O-X mapping tool to its own exception
hierarchy with the `XmlMappingException` as the root exception.
These runtime exceptions wrap the original exception so that no information will be lost.
2015-03-03 18:38:01 +08:00
Additionally, the `MarshallingFailureException` and `UnmarshallingFailureException`
provide a distinction between marshalling and unmarshalling operations, even though the
2018-09-05 23:15:53 +08:00
underlying O-X mapping tool does not do so.
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The O-X Mapping exception hierarchy is shown in the following figure:
2015-05-06 14:42:30 +08:00
2017-01-05 00:51:58 +08:00
image::images/oxm-exceptions.png[]
2015-03-03 18:38:01 +08:00
[[oxm-usage]]
2018-09-05 23:15:53 +08:00
=== Using `Marshaller` and `Unmarshaller`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
You can use Spring's OXM for a wide variety of situations. In the following example, we
use it to marshal the settings of a Spring-managed application as an XML file. In the following example, we
use a simple JavaBean to represent the settings:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
public class Settings {
private boolean fooEnabled;
public boolean isFooEnabled() {
return fooEnabled;
}
public void setFooEnabled(boolean fooEnabled) {
this.fooEnabled = fooEnabled;
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class Settings {
var isFooEnabled: Boolean = false
}
----
2015-03-03 18:38:01 +08:00
The application class uses this bean to store its settings. Besides a main method, the
class has two methods: `saveSettings()` saves the settings bean to a file named
2018-09-05 23:15:53 +08:00
`settings.xml`, and `loadSettings()` loads these settings again. The following `main()` method
constructs a Spring application context and calls these two methods:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
2015-03-03 18:38:01 +08:00
----
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
public class Application {
private static final String FILE_NAME = "settings.xml";
private Settings settings = new Settings();
private Marshaller marshaller;
private Unmarshaller unmarshaller;
public void setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
}
public void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
public void saveSettings() throws IOException {
2019-01-18 01:53:59 +08:00
try (FileOutputStream os = new FileOutputStream(FILE_NAME)) {
2015-03-03 18:38:01 +08:00
this.marshaller.marshal(settings, new StreamResult(os));
}
}
public void loadSettings() throws IOException {
2019-01-18 01:53:59 +08:00
try (FileInputStream is = new FileInputStream(FILE_NAME)) {
2015-03-03 18:38:01 +08:00
this.settings = (Settings) this.unmarshaller.unmarshal(new StreamSource(is));
}
}
public static void main(String[] args) throws IOException {
ApplicationContext appContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
Application application = (Application) appContext.getBean("application");
application.saveSettings();
application.loadSettings();
}
}
----
2019-09-03 22:03:28 +08:00
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class Application {
lateinit var marshaller: Marshaller
lateinit var unmarshaller: Unmarshaller
fun saveSettings() {
FileOutputStream(FILE_NAME).use { outputStream -> marshaller.marshal(settings, StreamResult(outputStream)) }
}
fun loadSettings() {
FileInputStream(FILE_NAME).use { inputStream -> settings = unmarshaller.unmarshal(StreamSource(inputStream)) as Settings }
}
}
private const val FILE_NAME = "settings.xml"
fun main(args: Array<String>) {
val appContext = ClassPathXmlApplicationContext("applicationContext.xml")
val application = appContext.getBean("application") as Application
application.saveSettings()
application.loadSettings()
}
----
2015-03-03 18:38:01 +08:00
2018-09-05 23:15:53 +08:00
The `Application` requires both a `marshaller` and an `unmarshaller` property to be set. We
can do so by using the following `applicationContext.xml`:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<bean id="application" class="Application">
2019-01-14 18:25:27 +08:00
<property name="marshaller" ref="xstreamMarshaller" />
<property name="unmarshaller" ref="xstreamMarshaller" />
2015-03-03 18:38:01 +08:00
</bean>
2019-01-14 18:25:27 +08:00
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/>
2015-03-03 18:38:01 +08:00
</beans>
----
2019-01-14 18:25:27 +08:00
This application context uses XStream, but we could have used any of the other marshaller
instances described later in this chapter. Note that, by default, XStream does not require any further
2018-09-05 23:15:53 +08:00
configuration, so the bean definition is rather simple. Also note that the
2019-01-14 18:25:27 +08:00
`XStreamMarshaller` implements both `Marshaller` and `Unmarshaller`, so we can refer to the
`xstreamMarshaller` bean in both the `marshaller` and `unmarshaller` property of the
2015-03-03 18:38:01 +08:00
application.
This sample application produces the following `settings.xml` file:
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<?xml version="1.0" encoding="UTF-8"?>
<settings foo-enabled="false"/>
----
[[oxm-schema-based-config]]
2018-09-05 23:15:53 +08:00
=== XML Configuration Namespace
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
You can configure marshallers more concisely by using tags from the OXM namespace. To
make these tags available, you must first reference the appropriate schema in the
preamble of the XML configuration file. The following example shows how to do so:
2015-03-03 18:38:01 +08:00
[source,xml,indent=0]
[subs="verbatim,quotes"]
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2018-09-22 05:06:09 +08:00
xmlns:oxm="http://www.springframework.org/schema/oxm" <1>
xsi:schemaLocation="http://www.springframework.org/schema/beans
2019-03-21 06:48:14 +08:00
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/oxm https://www.springframework.org/schema/oxm/spring-oxm.xsd"> <2>
2015-03-03 18:38:01 +08:00
----
2018-09-05 23:15:53 +08:00
<1> Reference the `oxm` schema.
2018-09-22 05:06:09 +08:00
<2> Specify the `oxm` schema location.
2018-11-27 06:15:55 +08:00
2015-03-03 18:38:01 +08:00
2019-01-14 18:25:27 +08:00
The schema makes the following elements available:
2015-03-03 18:38:01 +08:00
* <<oxm-jaxb2-xsd, `jaxb2-marshaller`>>
* <<oxm-jibx-xsd, `jibx-marshaller`>>
2018-09-05 23:15:53 +08:00
Each tag is explained in its respective marshaller's section. As an example, though,
the configuration of a JAXB2 marshaller might resemble the following:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>
----
[[oxm-jaxb]]
=== JAXB
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
The JAXB binding compiler translates a W3C XML Schema into one or more Java classes, a
`jaxb.properties` file, and possibly some resource files. JAXB also offers a way to
generate a schema from annotated Java classes.
Spring supports the JAXB 2.0 API as XML marshalling strategies, following the
`Marshaller` and `Unmarshaller` interfaces described in <<oxm-marshaller-unmarshaller>>.
The corresponding integration classes reside in the `org.springframework.oxm.jaxb`
package.
[[oxm-jaxb2]]
2018-09-05 23:15:53 +08:00
==== Using `Jaxb2Marshaller`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The `Jaxb2Marshaller` class implements both of Spring's `Marshaller` and `Unmarshaller`
interfaces. It requires a context path to operate. You can set the context path by setting the
`contextPath` property. The context path is a list of colon-separated Java package
2015-03-03 18:38:01 +08:00
names that contain schema derived classes. It also offers a `classesToBeBound` property,
which allows you to set an array of classes to be supported by the marshaller. Schema
2018-09-05 23:15:53 +08:00
validation is performed by specifying one or more schema resources to the bean, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>org.springframework.oxm.jaxb.Flight</value>
<value>org.springframework.oxm.jaxb.Flights</value>
</list>
</property>
<property name="schema" value="classpath:org/springframework/oxm/schema.xsd"/>
</bean>
...
</beans>
----
2018-09-05 23:15:53 +08:00
2015-03-03 18:38:01 +08:00
[[oxm-jaxb2-xsd]]
2018-09-05 23:15:53 +08:00
===== XML Configuration Namespace
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The `jaxb2-marshaller` element configures a `org.springframework.oxm.jaxb.Jaxb2Marshaller`,
as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>
----
2018-09-05 23:15:53 +08:00
Alternatively, you can provide the list of classes to bind to the marshaller by using the
`class-to-be-bound` child element:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<oxm:jaxb2-marshaller id="marshaller">
<oxm:class-to-be-bound name="org.springframework.ws.samples.airline.schema.Airport"/>
<oxm:class-to-be-bound name="org.springframework.ws.samples.airline.schema.Flight"/>
...
</oxm:jaxb2-marshaller>
----
2018-09-05 23:15:53 +08:00
The following table describes the available attributes:
2015-03-03 18:38:01 +08:00
|===
| Attribute| Description| Required
| `id`
2018-09-05 23:15:53 +08:00
| The ID of the marshaller
| No
2015-03-03 18:38:01 +08:00
| `contextPath`
2018-09-05 23:15:53 +08:00
| The JAXB Context path
| No
2015-03-03 18:38:01 +08:00
|===
[[oxm-jibx]]
=== JiBX
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The JiBX framework offers a solution similar to that which Hibernate provides for ORM: A
2015-03-03 18:38:01 +08:00
binding definition defines the rules for how your Java objects are converted to or from
XML. After preparing the binding and compiling the classes, a JiBX binding compiler
2018-09-05 23:15:53 +08:00
enhances the class files and adds code to handle converting instances of the classes
2015-03-03 18:38:01 +08:00
from or to XML.
2018-09-05 23:15:53 +08:00
For more information on JiBX, see the http://jibx.sourceforge.net/[JiBX web
site]. The Spring integration classes reside in the `org.springframework.oxm.jibx`
2015-03-03 18:38:01 +08:00
package.
[[oxm-jibx-marshaller]]
2018-09-05 23:15:53 +08:00
==== Using `JibxMarshaller`
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
The `JibxMarshaller` class implements both the `Marshaller` and `Unmarshaller`
interface. To operate, it requires the name of the class to marshal in, which you can
2018-09-05 23:15:53 +08:00
set using the `targetClass` property. Optionally, you can set the binding name by setting the
`bindingName` property. In the following example, we bind the `Flights` class:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<bean id="jibxFlightsMarshaller" class="org.springframework.oxm.jibx.JibxMarshaller">
<property name="targetClass">org.springframework.oxm.jibx.Flights</property>
</bean>
...
</beans>
----
A `JibxMarshaller` is configured for a single class. If you want to marshal multiple
2018-09-05 23:15:53 +08:00
classes, you have to configure multiple `JibxMarshaller` instances with different `targetClass`
2015-03-03 18:38:01 +08:00
property values.
[[oxm-jibx-xsd]]
2018-09-05 23:15:53 +08:00
===== XML Configuration Namespace
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The `jibx-marshaller` tag configures a `org.springframework.oxm.jibx.JibxMarshaller`,
as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<oxm:jibx-marshaller id="marshaller" target-class="org.springframework.ws.samples.airline.schema.Flight"/>
----
2018-09-05 23:15:53 +08:00
The following table describes the available attributes:
2015-03-03 18:38:01 +08:00
|===
| Attribute| Description| Required
| `id`
2018-09-05 23:15:53 +08:00
| The ID of the marshaller
| No
2015-03-03 18:38:01 +08:00
| `target-class`
2018-09-05 23:15:53 +08:00
| The target class for this marshaller
| Yes
2015-03-03 18:38:01 +08:00
| `bindingName`
2018-09-05 23:15:53 +08:00
| The binding name used by this marshaller
| No
2015-03-03 18:38:01 +08:00
|===
[[oxm-xstream]]
=== XStream
2017-10-19 02:24:17 +08:00
2015-03-03 18:38:01 +08:00
XStream is a simple library to serialize objects to XML and back again. It does not
2018-09-05 23:15:53 +08:00
require any mapping and generates clean XML.
2015-03-03 18:38:01 +08:00
2019-03-21 06:48:14 +08:00
For more information on XStream, see the https://x-stream.github.io/[XStream
2018-09-05 23:15:53 +08:00
web site]. The Spring integration classes reside in the
2015-03-03 18:38:01 +08:00
`org.springframework.oxm.xstream` package.
[[oxm-xstream-marshaller]]
2018-09-05 23:15:53 +08:00
==== Using `XStreamMarshaller`
2017-10-19 02:24:17 +08:00
2018-09-05 23:15:53 +08:00
The `XStreamMarshaller` does not require any configuration and can be configured in an
application context directly. To further customize the XML, you can set an alias map,
which consists of string aliases mapped to classes, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<beans>
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<props>
<prop key="Flight">org.springframework.oxm.xstream.Flight</prop>
</props>
</property>
</bean>
...
</beans>
----
[WARNING]
2018-09-05 23:15:53 +08:00
=====
By default, XStream lets arbitrary classes be unmarshalled, which can lead to
unsafe Java serialization effects. As such, we do not recommend using the
`XStreamMarshaller` to unmarshal XML from external sources (that is, the Web), as this can
result in security vulnerabilities.
2017-03-24 18:09:57 +08:00
If you choose to use the `XStreamMarshaller` to unmarshal XML from an external source,
2018-09-05 23:15:53 +08:00
set the `supportedClasses` property on the `XStreamMarshaller`, as the following example shows:
2015-03-03 18:38:01 +08:00
2019-09-03 22:03:28 +08:00
[source,xml,indent=0,subs="verbatim,quotes"]
2015-03-03 18:38:01 +08:00
----
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="supportedClasses" value="org.springframework.oxm.xstream.Flight"/>
...
</bean>
----
2018-09-05 23:15:53 +08:00
Doing so ensures that only the registered classes are eligible for unmarshalling.
2015-03-03 18:38:01 +08:00
Additionally, you can register
2015-10-27 21:31:00 +08:00
{api-spring-framework}/oxm/xstream/XStreamMarshaller.html#setConverters(com.thoughtworks.xstream.converters.ConverterMatcher...)[custom
2015-03-03 18:38:01 +08:00
converters] to make sure that only your supported classes can be unmarshalled. You might
want to add a `CatchAllConverter` as the last converter in the list, in addition to
converters that explicitly support the domain classes that should be supported. As a
result, default XStream converters with lower priorities and possible security
vulnerabilities do not get invoked.
2018-09-05 23:15:53 +08:00
=====
2015-03-03 18:38:01 +08:00
NOTE: Note that XStream is an XML serialization library, not a data binding library.
2018-09-05 23:15:53 +08:00
Therefore, it has limited namespace support. As a result, it is rather unsuitable for usage
2015-03-03 18:38:01 +08:00
within Web services.
2017-10-05 19:23:18 +08:00
2017-10-19 02:24:17 +08:00
2018-10-25 21:15:58 +08:00
2017-10-05 19:23:18 +08:00
include::data-access-appendix.adoc[leveloffset=+1]