Today I’ll talk about an important concept in the XEO Framework. The EboContext. Specifically how you should use it, and how you shouldn’t use it.
If you’ve been using the framework, by now you know that whenever you need to load a model instance or create a list of model instances, you need to pass an instance of EboContext to the respective methods, like the following:
boObjectList.list(ctx, “select Model where something=otherthing”);
Most of the times, you can just do the following to get your hands on an instance of EboContext:
EboContext ctx = boApplication.currentContext().getEboContext();
But depending on the things you’ve been doing, you might find that the EboContext instance is null, why is that?
When a request is issued to the XWC Servlet (the Servlet that configured in your web.xml to process any request for files with a .xvw extension), that servlet is responsible for creating an EboContext and associating it to the current thread context and by the end of the request closing it. So each request has the following structure:
- Browser makes request
- XWC Servlet creates EboContext instance and associates it to the current context
- Request is processed
- XWC Servlet disposes of the EboContext instance
The step that created and associated the EboContext instance is the one responsible for you being able to issue a statement like boApplication.currentContext().getEboContext().
But what if you’re not using the XWC servlet and have some JSP page where you need to deal with XEO Objects? Well in that case you’re responsible for dealing with the EboContext instances. So what do you need to do to create an EboContext instance? See the example:
What’s really important to know about creating EboContext instances?
You MUST ABSOLUTELY CLOSE every instance you EXPLICITLY create
If you create instances of EboContext and don’t close them, pretty soon you’ll run out of database connections 🙂 Talk about resource leakage heh?
- In you’re in the context of a .xvw file, no need to create EboContext instances
- If you’re out of that context, you’re responsible for creating the EboContext instance (and for closing it)
- If you explicitly create an EboContext instance, you must explicitly close it in a finally statement
- If you need the EboContext you created, to be accessible via boApplication.currentContext().getEboContext() you need to associated the instance via boApplication.currentContext.addEboContext(ctx).
Hope it helps when dealing with EboContexts!