Skip to content

Object Lifetime

Philippe Marschall edited this page Oct 12, 2016 · 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 with puts pressure on Metaspace. Second we cache meta information about a call so the second time we do no longer need perform reflection and 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);
  }
  

}

CDI

and give it the ApplicationScoped scope.

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

Singleton

Clone this wiki locally