From facd2401cd9d02f1aa5d7b9e46b8daa4cc0782f9 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 20 Feb 2015 14:54:33 +0100 Subject: [PATCH] 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 --- src/asciidoc/index.adoc | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/asciidoc/index.adoc b/src/asciidoc/index.adoc index f8be201867a..c27bae7acf6 100644 --- a/src/asciidoc/index.adoc +++ b/src/asciidoc/index.adoc @@ -7269,6 +7269,20 @@ method are automatically enlisted with a destruction callback. If you have a pub `close` or `shutdown` method and you do not wish for it to be called when the container shuts down, simply add `@Bean(destroyMethod="")` to your bean definition to disable the default `(inferred)` mode. + +You may want to do that by default for a resource that you acquire via JNDI as its lifecycle +is managed outside the application. In particular, make sure to always do it for a +`DataSource` as it is known to be problematic. + +[source,java,indent=0] +[subs="verbatim,quotes"] +---- + @Bean(destroyMethod="") + public DataSource dataSource() throws NamingException { + return (DataSource) jndiTemplate.lookup("MyDS"); + } +---- + ==== 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: [source,java,indent=0] [subs="verbatim,quotes"] ---- - @Bean + @Bean(destroyMethod="") public DataSource dataSource() throws Exception { Context ctx = new InitialContext(); return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource"); @@ -8143,7 +8157,7 @@ can rewrite the _dataSource_ configuration as follows: **@Profile("production")** public class JndiDataConfig { - @Bean + @Bean(destroyMethod="") public DataSource dataSource() throws Exception { Context ctx = new InitialContext(); return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource"); @@ -20413,7 +20427,7 @@ integration test but using `@Configuration` classes instead of XML. @Profile("production") public class JndiDataConfig { - @Bean + @Bean(destroyMethod="") public DataSource dataSource() throws Exception { Context ctx = new InitialContext(); return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");