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
This commit is contained in:
parent
152a7b645f
commit
facd2401cd
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Reference in New Issue