XEO – EboContext when to create and how to use

Hi everyone,

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?

Summary

  • 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!

 

 

7 essential wordpress plugins for technical blogs

Hi everyone

Today I’m going to move from my usually Java/XEO related posts and blog about wordpress plugins. The point is to share the plugins I have installed in this blog. So lets get started!

Akismet (bundled with wordpress)

Akistmet protects you from spam, and it does a great job at it. It’s the first in the list because it really is so important. If not for Akismet I would literally have to go through dozens of comments and check their content (and I have a very small blog with a low number of visits), this saves me hours!
You’ll need to register with Akismet for an API Key to activate the plugin.

Syntax Highlighter Compress 

Since I’m running a technical blog, one of the things I need is a code highlighter. This plugin highlights the most common languages like C#, Java, XML, SQL, Javascript, Ruby Phyton, etc.. as well as less common ones ( e.g. Erlang ). It has its own settings page where you can choose a stylesheet such as Eclipse/Emacs, among other things.

Social Sharing Toolkit

You must have a social sharing plugin these days, right? This plugin allows to share to virtually every social network you can think of (you can configure which social networks are displayed in each post you make). I personally only use Facebook, Twitter, LinkedIn, Google+ and Reddit. It’s easy to install, works well and it looks good.

Smart YoutTube Pro

I don’t usually post youtube videos, but when you need to do it, it becomes very handy. It has tons of options, but you basically just need to paste the youtube link and it will embed the video in the post, very nice!

Limit Login Attempts

This is one is here for security. Until I installed it, I had no idea about the amount of (automated) hacking going around these days. By default wordpress makes no attempt to limit login attempts which makes it a very nice friend to brute-force attacks. Even with a strong password, I feel much more secure by having it installed.

NK Google Analytics

Well, if you want to track your visits and all, you’ll want a Google Analytics account, as such, you’ll need a plugin. Installing Google Analytics on your page is basically coping a few lines of javascript to the main template, but if you don’t want to get your hands dirty, use this plugin. You’ll need to register an API Key with Google first!

WordPress SEO

Well, I’m far from being an SEO Expert, but this plugin really gives a certain amount of information about each of your posts regarding search engine optimization which I find useful. It has TONS of settings, but in the end, it’s really useful by showing little green/yellow/red balls next to the items you choose like keywords, description, etc…

Well, I hope you find this list useful.

Happy coding blogging!

 

 

 

Query SQL / BOQL with Dates in where clause using Java

Hi everyone,
One of the things that I struggled in the beginning (a long time ago in a galaxy far away, cof cof) with SQL queries (or BOQL queries if you’re using the XEO framework) was how to make queries using dates in the where clause, something like this:

select * from table where column_date > SOMETHING

Not knowing how to do date comparisons I usually did stuff like this ( evil code ahead, beware ) :

select * from some_table where to_char(column_data,'DD-MON-YYYY') > to_char('23-05-2013','DD-MON-YYYY');

Usually in Java you have java.util.Date instances which I heroically used like the following, in order to be able to create the previous query:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date date = loadDateFromSomewhere();
String sql = "select * from some_table where to_char(column_data,'DD-MON-YYYY') > ('"+sdf.format(date)+"','DD-MON-YYYY')";

Well now I don’t do this kind of thing, eh eh… it’s way easier to do such queries using proper data types. Check in a Java SQL Prepared statement:

Connection conn = getConnection();
PreparedStatement statement = conn.prepareStatement("select * from some_table where column_date > ?");
statement.setDate(1, new java.sql.Date(loadDateFromSomewhere().getTime()));

Notice how you pass a java.sql.Date instance (created using the information from the java.util.Date instance) to the parameter. This has the added benefit that no data conversions are done (meaning extra performance). And if you have an index on a date column that index will be used ( I also learned that when you apply a function to an indexed column, that index won’t be used properly as the function has to be applied to every row in the table – check this )

In BOQL by the way it would be like this:

boObjectList.list(getEboContext(), "select Object where column_data > ?", new Object[]{ new java.sql.Date(date.getTime()) });

Important notes:

  • In SQL/BOQL queries you use java.sql.Date and java.sql.Timestamp instances (you don’t use java.util.Date instances)
  • Use java.sql.Date instances when you want to compare Dates only (ignoring time)
  • Use java.sql.Timestamp instances when you want to compare Dates including time information

And voilá… Dates in SQL/BOQL queries (using Java) without much effort. Hope it was useful,  Happy coding!