Skip to content

Commit fbb3f3a

Browse files
committed
#199 - Add documentation for Connection Factory Initialization.
1 parent 53121b4 commit fbb3f3a

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
[[r2dbc.init]]
2+
= Initializing a `ConnectionFactory`
3+
4+
The `org.springframework.data.r2dbc.connectionfactory.init` package provides support for initializing an existing `ConnectionFactory`.
5+
You may sometimes need to initialize an instance that runs on a server somewhere or an embedded database.
6+
7+
== Initializing a Database by Using @Bean methods
8+
9+
If you want to initialize a database and you can provide a reference to a `ConnectionFactory` bean, you can use the
10+
11+
.Using `ConnectionFactoryInitializer` to initialize a `ConnectionFactory`
12+
====
13+
[source,java]
14+
----
15+
@Configuration
16+
public class InitializerConfiguration {
17+
18+
@Bean
19+
public ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {
20+
21+
ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
22+
initializer.setConnectionFactory(connectionFactory);
23+
24+
CompositeDatabasePopulator populator = new CompositeDatabasePopulator();
25+
populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("com/foo/sql/db-schema.sql")));
26+
populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("com/foo/sql/test-data1.sql")));
27+
initializer.setDatabasePopulator(populator);
28+
29+
return initializer;
30+
}
31+
}
32+
----
33+
====
34+
35+
The preceding example runs the two specified scripts against the database.
36+
The first script creates a schema, and the second populates tables with a test data set.
37+
38+
The default behavior of the database initializer is to unconditionally run the provided scripts.
39+
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.
40+
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.
41+
The first step fails if the tables already exist.
42+
43+
However, to gain more control over the creation and deletion of existing data, `ConnectionFactoryInitializer` and `ResourceDatabasePopulator` support various switches such as switching the initialization on and off.
44+
45+
Each statement should be separated by `;` or a new line if the `;` character is not present at all in the script. You can control that globally or script by script, as the following example shows:
46+
47+
.Customizing statement separators
48+
====
49+
[source,java]
50+
----
51+
@Configuration
52+
public class InitializerConfiguration {
53+
54+
@Bean
55+
public ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {
56+
57+
ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
58+
59+
initializer.setConnectionFactory(connectionFactory);
60+
61+
ResourceDatabasePopulator populator = new ResourceDatabasePopulator(new ClassPathResource("com/foo/sql/db-schema.sql"));
62+
populator.setSeparator("@@"); <1>
63+
initializer.setDatabasePopulator(populator);
64+
65+
return initializer;
66+
}
67+
}
68+
----
69+
<1> Set the separator scripts to `@@`.
70+
====
71+
72+
In this example, the schema scripts uses `@@` as statement separator.
73+
74+
=== Initialization of Other Components that Depend on the Database
75+
76+
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.
77+
If your application is not one of those, you might need to read the rest of this section.
78+
79+
The database initializer depends on a `ConnectionFactory` instance and runs the scripts 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`).
80+
If other beans depend on the same data source and use the data source in an initialization callback, there might be a problem because the data has not yet been initialized.
81+
A common example of this is a cache that initializes eagerly and loads data from the database on application startup.
82+
83+
To get around this issue, you have two options:
84+
85+
1. change your cache initialization strategy to a later phase or
86+
2. ensure that the database initializer is initialized first
87+
88+
Changing your cache initialization strategy might be easy if the application is in your control and not otherwise. Some suggestions for how to implement this include:
89+
90+
* Make the cache initialize lazily on first usage, which improves application startup time.
91+
* Have your cache or a separate component that initializes the cache implement Lifecycle or SmartLifecycle.
92+
When the application context starts, you can automatically start a `SmartLifecycle` by setting its `autoStartup` flag, and you can manually start a Lifecycle by calling `ConfigurableApplicationContext.start()` on the enclosing context.
93+
* Use a Spring `ApplicationEvent` or similar custom observer mechanism to trigger the cache initialization.
94+
`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).
95+
96+
Ensuring that the database initializer is initialized first can also be easy.
97+
Some suggestions on how to implement this include:
98+
99+
* Rely on the default behavior of the Spring `BeanFactory`, which is that beans are initialized in registration order.
100+
You can easily arrange that by adopting the common practice of a set of `@Import` configuration that order your application modules and ensuring that the database and database initialization are listed first.
101+
* Separate the `ConnectionFactory` 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 `ConnectionFactory`, and the child context contains the business components).
102+

0 commit comments

Comments
 (0)