Eclipse or SUNrise...

Eclipse or SUNrise...
...JAVA for sure

Thursday, June 28, 2007

EJB cacheing old data from database

Lately, I was working with postgres 8.x database and EJB3.0 from JDK5. I had problem of refreshing data that was saved in the DB. From some reason the Enterprise Entity Beans had been cacheing the previous values from the tables. The issue happend when there was couple of opperations one after another using the modified data. All the updates on the tables were transactional so it shouldn't happen - in theory.

So, how to solve the problem? First I tried the flush() method from the EntityManager after the persist() method, but it didn't help (actualy I don't know where it could come in handy...), the one that helped was the refresh() method. It takes one parameter, an object - Entity Bean representing the record from the database and checks it's latest version.

The example code is simple (EJB3.0), please note that exception handling is primitive and for example purposes only:

@PersistenceContext private EntityManager manager;

//..

int id = 1;
Query q = manager.createQuery("SELECT OBJECT(o) FROM TestTableBean o WHERE o.id = :id");
q.setParameter("id", id);

try
{
TestTableBean testTableBean = (TestTableBean)q.getSingleResult();
}
catch (NoResultException nre)
{
testTableBean = null;
}
catch (Exception e)
{
testTableBean = null;
System.err.println("ERROR: " + e.toString() + ":");
e.printStackTrace();
}

if (testTableBean != null)
{
int cachedValue = testTableBean.getValue(); // old one
manager.refresh(testTableBean);
int goodValue = testTableBean.getValue(); // correct one
}


So, if you need the data to be 100% up to date - simply use the refresh() method. I can't tell you how much impacts it on the performance yet, but in my opinion it looks like a good solution.

If you have any comments - please share it, I'm open for discusion.

Sunday, June 24, 2007

LabelValueBean from Struts

Yesterday I was modyfing a simple struts form. The modification was to add a pulldown box with status names stored in database. The status list had to be obtained from a dictrionary table and consisted from Integer type index and String type status name. On this occasion I have found a nice LabelValueBean object which comes with standard struts utilites. You can find it under org.apache.struts.util package.

So what is special about it? I guess there is nothing special, but it enables us to produce a nice, readable code, plus you don't have to make a special pojo objects or make magic tricks after which you need extra time to understand what is going on in the code each time you look at it. So, how does the bean help. The LabelValueBean consists of two Strings, one for the Label (status name) and one for the Id (ie. status id in the dictionary). To make it running you need two things on the struts form, and that is:

private Integer statusId; // for selected id
private List statusList; // for all the status values and names

// plus proper geters and seters of course...


In the action, in the initialization section you just need to fill the List of statusList with values from the database or whatever you need

And finally in the frontend layer - the jsp level, you code like that:

<html:select property="statusId">
<html:optionsCollection property="statusList" value="value" label="label"/>
</html:select>


As simple as that - I especially like the code in jsp because it is nice and clear and it is really easy to understand for someone who didn't wrote it himself. The String id stored in the value will be automaticaly casted to Integer type of statusId property. This one shows once again the beauty of struts - it just makes the worst work for you. I love the fact that struts is object oriented and that it is cappable of using Collections.

Thursday, June 21, 2007

Problems with codepage on NetBeans 5.5

Some time ago, I was working in a project developed on Linux Ubuntu systems. The project was based on NetBeans 5.5 platform and everything went well, until I had to port the application to my private laptop, with Windows XP on it. After installing and configuring all the stuff, I tried to compile and deploy the app, but I got tons of errors. I quickly recalled that on Linux there is a standard unicode UTF-8 format codeing and on Windows I got the magic CP1250-whatever format which is Microsoft's equivalent of ISO8859-2 (middle-eastern European language type used in my country).

Normaly advanced editors (even the EditPlus) are cappable to switch the code-page and there is no problem with such matters. So to solve the problem I tried the NetBeans options. It was quite tricky to find the right one, especialy because you need to type it by yourself... Guys from Sun hidden it under

tools > options > advanced options > Editing > Java Sources


To make it clear, let me just show you. You have to type exactly "UTF-8"



Ok, so was it it? Nope... it wasn't. It fixed the characters so they looked fine but still the code wouldn't compile. After a while, I came up with something else to fix this. Because I didn't found any other options conected with java compiler on NetBeans I wrote a script which started NetBeans with proper compiler encoding. The script is very simple and looks like this:

"C:\Program Files\netbeans-5.5\bin\netbeans.exe" -J-Dfile.encoding=UTF-8


I made a shortcut on the desktop, and finally it worked just fine.

If it happens that you know a better sollution - please let me know. I have to check out if they fixed this on NetBeans6.

Wednesday, June 20, 2007

Let the show begin...

Ok, officialy registered on the blogger! And now what? Well, let me tell you why am I here in the first place. I'm working with Java technology for quite a while and during this time, when I had some problems to solve or some quastions to answer and there was anybody to help - google often showed the way. One of those 'times' I found a java programmer blog - John Yeary's Blog. It was and IS a great help for me and all I can tell you is to check it out for yourself. I am not even in 10% as experienced as John but I think that still, I have something to say. Till now, I just used help from others by reading their words, now I would like to write something of my own experience and give something back. I really hope it may be some help for others programmers.

Ok, to work then! Hope to write something usefull soon ;-)
Wishing the best to all you readers (if any),
Sebastian Kapciak