Developer How-To Use the OpenMRS API


Demarcating start and end of API work

  • When coding against the database API, domain objects (Patient, User, etc.) provided from the API are only guaranteed to be valid between calls to context.openSession() and context.closeSession().
    • You needn't worry about this within a web application environment as long as your are using a filter such as org.openmrs.web.OpenmrsFilter (since the filter marks the boundaries around each HTTP request)
    • You cannot use domain objects across transactions (or requests) (e.g., if you loaded a Patient object on one web page, you must reload that object, before using it on subsequent pages)
  • It is the developer's responsibility to ensure that context.closeSession() is called (if not within webapp environment) to release precious resources (even in the event of an exception)
  • These transaction boundary calls are lightweight -- i.e., there is little penalty for calling them