Skip to content

Object Lifetime

Philippe Marschall edited this page Mar 2, 2017 · 9 revisions

We recommend you create one instance of the stored procedure interface and use it for the entire lifetime of your application. We do not recommend creating an instance of the stored procedure interface interface every time you call a stored procedure.

There are two reasons for this. First creating a new instance involves creating a proxy which puts pressure on Metaspace. Second we cache meta information about a call so the second time we no longer need perform reflection and can allocate fewer objects.

Spring

If you use Spring we recommend you make the stored procedure interface instance a bean with the singleton scope which is the default scope.

@Configuration
public class TaxProceduresConfiguration {

  @Autowired
  private DataSource dataSource;

  @Bean
  public TaxProcedures taxProcedures() {
    return ProcedureCallerFactory.build(TaxProcedures.class, dataSource);
  }

}

If you're using Spring XML configuration you'll probably have to use a factory method or even factory bean.

CDI

If you use CDI we recommend you make the stored procedure interface instance ApplicationScoped.

public class TaxProceduresProducer {

  @Resource(name="jndi/name/of/the/DataSource")
  private DataSource dataSource;

  @Produces
  @ApplicationScoped
  public TaxProcedures taxProcedures() {
    return ProcedureCallerFactory.build(TaxProcedures.class, dataSource);
  }

}

Guice

If you use Guice we recommend you make the stored procedure interface instance Singleton scoped.

public class TaxProceduresModule extends AbstractModule  {

  @Inject
  private DataSource dataSource;

  @Provides
  @Singleton
  public TaxProcedures taxProcedures() {
    return ProcedureCallerFactory.build(TaxProcedures.class, dataSource);
  }

}