@PooledBean
As we’re working on passing the EJB TCK for JavaEE 6 WebProfile, we’ve also been talking about how to improve EJB further for JavaEE 7. One idea that we’ve discussed is creating a @PooledBean lifecycle.
The @PooledBean would streamline, simplify, and improve the robustness of the current @Stateless bean, which fits nicely into our goals of improving performance and robustness of the WebProfile.
The purpose of the @PooledBean is to clean up the boilerplace try finally code used for long-lived resources like JDBC, JPA, sockets,
and JMS. The @PooledBean treats the allocation and freeing of the resources as cross-cutting concerns, handled by the @PostConstruct and @PreDestroy methods.
As code, the @PooledBean is a class annotation that would tell the application server to pool instances of the bean on all interface method invocations. In all other ways, the class would remain a plain CDI managed bean: no special context object, no special classloader behavior, no jndi, and no extra transaction defaults. Just the pooling capability. The code might look like:
package com.example;
import com.caucho.inject.PooledBean;
@PooledBean
public class MyPooledBean implements PooledApi {
private ExpensiveResource = _resource;
String hello()
{
return _resource.doHelloMethod();
}
@PostConstruct
void init()
{
// allocate expensive resource
}
@PreDestroy
void destroy()
{
// free expensive resource
}
}
Since the destroy() code is guaranteed to be called when the instance is destroyed, your finally code can be shared by all the business methods. Even better, you can put the init()/destroy() code in a common superclass. From a testing point of view, this change makes the application more reliable, because you can fully test the resource close for every method without creating separate tests for separate finally blocks.

May 17th, 2010 at 4:26 pm
This is a great great great idea… especially with some way to control the max pool size.
May 17th, 2010 at 7:07 pm
Good point. Tomorrow, I can put up the pool size attributes we’ve talked about internally.
May 21st, 2010 at 11:18 am
Just a thought but… why not call it @Pooled instead of @PooledBean? It would fit better into the conventions of other EE annotations, @Stateless, @Stateful, @MessageDriven, etc.
May 25th, 2010 at 10:39 am
Good point. @Pooled is a better name.