Skip to content

Commit facd240

Browse files
committed
Document lifecycle issue with JNDI-based beans
When a bean is retrieved via JNDI using Java config, we apply the same inferred more for destruction callbacks as for any other bean. If an object from the JNDI tree has a `close` or `shutdown` method, the context calls it when it shutdowns. Unfortunately, we have no way to know that the bean was retrieved via JNDI and that its lifecycle is managed outside the application. The documentation has been updated to reflect that problem explicitly. Issue: SPR-12551
1 parent 152a7b6 commit facd240

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/asciidoc/index.adoc

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7269,6 +7269,20 @@ method are automatically enlisted with a destruction callback. If you have a pub
72697269
`close` or `shutdown` method and you do not wish for it to be called when the container
72707270
shuts down, simply add `@Bean(destroyMethod="")` to your bean definition to disable the
72717271
default `(inferred)` mode.
7272+
7273+
You may want to do that by default for a resource that you acquire via JNDI as its lifecycle
7274+
is managed outside the application. In particular, make sure to always do it for a
7275+
`DataSource` as it is known to be problematic.
7276+
7277+
[source,java,indent=0]
7278+
[subs="verbatim,quotes"]
7279+
----
7280+
@Bean(destroyMethod="")
7281+
public DataSource dataSource() throws NamingException {
7282+
return (DataSource) jndiTemplate.lookup("MyDS");
7283+
}
7284+
----
7285+
72727286
====
72737287

72747288
Of course, in the case of `Foo` above, it would be equally as valid to call the `init()`
@@ -8088,7 +8102,7 @@ now looks like this:
80888102
[source,java,indent=0]
80898103
[subs="verbatim,quotes"]
80908104
----
8091-
@Bean
8105+
@Bean(destroyMethod="")
80928106
public DataSource dataSource() throws Exception {
80938107
Context ctx = new InitialContext();
80948108
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
@@ -8143,7 +8157,7 @@ can rewrite the _dataSource_ configuration as follows:
81438157
**@Profile("production")**
81448158
public class JndiDataConfig {
81458159

8146-
@Bean
8160+
@Bean(destroyMethod="")
81478161
public DataSource dataSource() throws Exception {
81488162
Context ctx = new InitialContext();
81498163
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
@@ -20413,7 +20427,7 @@ integration test but using `@Configuration` classes instead of XML.
2041320427
@Profile("production")
2041420428
public class JndiDataConfig {
2041520429

20416-
@Bean
20430+
@Bean(destroyMethod="")
2041720431
public DataSource dataSource() throws Exception {
2041820432
Context ctx = new InitialContext();
2041920433
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");

0 commit comments

Comments
 (0)