Skip to content

Commit 50b6f9d

Browse files
committed
Reference documentation covers async Hibernate/JPA bootstrap options
Issue: SPR-17189
1 parent 31c7807 commit 50b6f9d

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

src/docs/asciidoc/data-access.adoc

+59-7
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ The above definition of the `dataSource` bean uses the `<jndi-lookup/>` tag from
311311
====
312312

313313
You can also use Hibernate local transactions easily, as shown in the following
314-
examples. In this case, you need to define a Hibernate `LocalSessionFactoryBean`, which
315-
your application code will use to obtain Hibernate `Session` instances.
314+
examples. In this case, you need to define a Hibernate `LocalSessionFactoryBean`,
315+
which your application code will use to obtain Hibernate `Session` instances.
316316

317317
The `DataSource` bean definition will be similar to the local JDBC example shown
318318
previously and thus is not shown in the following example.
@@ -5116,8 +5116,8 @@ chapter will then cover the other ORM technologies, showing briefer examples the
51165116
====
51175117
As of Spring Framework 5.0, Spring requires Hibernate ORM 4.3 or later for JPA support
51185118
and even Hibernate ORM 5.0+ for programming against the native Hibernate Session API.
5119-
Note that the Hibernate team does not maintain any versions prior to 5.0 anymore and
5120-
is likely to focus on 5.2+ exclusively soon.
5119+
Note that the Hibernate team does not maintain any versions prior to 5.1 anymore and
5120+
is likely to focus on 5.3+ exclusively soon.
51215121
====
51225122

51235123

@@ -5175,8 +5175,25 @@ configuration:
51755175
----
51765176

51775177
You can also access a JNDI-located `SessionFactory`, using Spring's
5178-
`JndiObjectFactoryBean` / `<jee:jndi-lookup>` to retrieve and expose it. However, that
5179-
is typically not common outside of an EJB context.
5178+
`JndiObjectFactoryBean` / `<jee:jndi-lookup>` to retrieve and expose it.
5179+
However, that is typically not common outside of an EJB context.
5180+
5181+
[NOTE]
5182+
====
5183+
Spring also provides a `LocalSessionFactoryBuilder` variant, seamlessly integrating
5184+
with `@Bean` style configuration and programmatic setup (no `FactoryBean` involved).
5185+
5186+
Both `LocalSessionFactoryBean` and `LocalSessionFactoryBuilder` support background
5187+
bootstrapping, with Hibernate initialization running in parallel to the application
5188+
bootstrap thread on a given bootstrap executor (e.g. a `SimpleAsyncTaskExecutor`).
5189+
On `LocalSessionFactoryBean`, this is available through the "bootstrapExecutor"
5190+
property; on the programmatic `LocalSessionFactoryBuilder`, there is an overloaded
5191+
`buildSessionFactory` method which takes a bootstrap executor argument.
5192+
5193+
As of Spring Framework 5.1, such a native Hibernate setup can also expose a JPA
5194+
`EntityManagerFactory` for standard JPA interaction next to native Hibernate access.
5195+
See <<orm-jpa-hibernate,Native Hibernate setup for JPA>> for details.
5196+
====
51805197

51815198

51825199
[[orm-hibernate-straight]]
@@ -5781,7 +5798,7 @@ This is important especially when the hosting applications rely on different JPA
57815798
implementations because the JPA transformers are applied only at class loader level and
57825799
thus are isolated from each other.
57835800

5784-
[[orm-jpa-multiple]]
5801+
[[orm-jpa-setup-multiple]]
57855802
===== Dealing with multiple persistence units
57865803

57875804
For applications that rely on multiple persistence units locations, stored in various
@@ -5825,6 +5842,30 @@ affect __all__ hosted units, or programmatically, through the
58255842
`PersistenceUnitManager` is specified, one is created and used internally by
58265843
`LocalContainerEntityManagerFactoryBean`.
58275844

5845+
[[orm-jpa-setup-background]]
5846+
===== Background bootstrapping
5847+
5848+
`LocalContainerEntityManagerFactoryBean` supports background bootstrapping through
5849+
the `bootstrapExecutor` property:
5850+
5851+
[source,xml,indent=0]
5852+
[subs="verbatim,quotes"]
5853+
----
5854+
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
5855+
<property name="bootstrapExecutor">
5856+
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
5857+
</property>
5858+
</bean>
5859+
----
5860+
5861+
The actual JPA provider bootstrapping will be handed off to the specified executor then,
5862+
running in parallel to the application bootstrap thread. The exposed `EntityManagerFactory`
5863+
proxy can be injected into other application components and is even able to respond
5864+
`EntityManagerFactoryInfo` configuration inspection. However, once the actual JPA provider
5865+
is being accessed by other components, e.g. calling `createEntityManager`, those calls
5866+
will block until the background bootstrapping has completed. In particular, when using
5867+
Spring Data JPA, make sure to set up deferred bootstrapping for its repositories as well.
5868+
58285869

58295870
[[orm-jpa-dao]]
58305871
==== Implementing DAOs based on JPA: EntityManagerFactory and EntityManager
@@ -6087,6 +6128,17 @@ as well as stronger optimizations for read-only transactions. Last but not least
60876128
native Hibernate setup can also be expressed through `LocalSessionFactoryBuilder`,
60886129
seamlessly integrating with `@Bean` style configuration (no `FactoryBean` involved).
60896130

6131+
[NOTE]
6132+
====
6133+
`LocalSessionFactoryBean` and `LocalSessionFactoryBuilder` support background
6134+
bootstrapping, just like the JPA `LocalContainerEntityManagerFactoryBean`.
6135+
See <<orm-jpa-setup-background,Background bootstrapping>> for an introduction.
6136+
6137+
On `LocalSessionFactoryBean`, this is available through the "bootstrapExecutor"
6138+
property; on the programmatic `LocalSessionFactoryBuilder`, there is an overloaded
6139+
`buildSessionFactory` method which takes a bootstrap executor argument.
6140+
====
6141+
60906142

60916143

60926144

0 commit comments

Comments
 (0)