Category Archives: J2EE

While I was searching google for some helpful information about the many [Exception I've tend to get these days][1], I stumble apon this [helpful blogentry][2] about [JBoss][3] and Performance. It said, that by adding a single line in a configuration fil, the performance would increase dramtically. “Yeah right” I thought, and tried it out anyway:

JBOSS_HOME/server/CONFIGURATION/conf/log4j.xml:

into:

JBOSS_HOME/server/CONFIGURATION/conf/log4j.xml:

And the effect was there right after a shutdown/start of the JBoss server. Is was very much faster! Very nice.

After thinking about it, it also made sence. JBoss enables debug logging as default. All the IO required to save this log information uses up A LOT of the performance.

This actually fixes one of my tasks for [FriFinans 0.2][4] :D

[1]: http://billen.dk/2005/08/04/new-jboss-ejb3-release/
[2]: http://fishbowl.pastiche.org/2005/04/19/fun_with_jboss_performance
[3]: http://www.jboss.org/
[4]: http://wiki.frifinans.dk/Roadmap#Version_0.2

I have upgraded my development environment to [JBoss EJB3 RC1][1]. This is apparently more strict and correct according to the [EJB3 spec][2]. This is great, because it helps fixing all that “ugly code ™”. In the next couply of posts I’ll be addressing some of the funny little Exceptions, which I receive while converting the existing [FriFinans][3] code into shiny pebbles ;-) .

The first one is

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session

at org.frifinans.server.stateless.UserSettingsBean.get()Lorg.frifinans.server.entity.LedgerUserSetting;(Unknown Source)

This one has a simple solution. I was trying to cascade persist an object, which was already persisted. This is a little piece of the [UserSettingsBean][4] SessionBean, which tried to persist:

public LedgerUserSetting get () {
    LedgerUserSetting lus = new LedgerUserSetting ();

    CommonUser user = ur.getCurrent ();
    if (user != null) {
        // Find user setting
        LedgerUserSetting l = null;
        try {
             l = (LedgerUserSetting) manager.
                    createQuery (
                    "FROM " +
                    "   LedgerUserSetting lus " +
                    "WHERE " +
                    "   lus.user = :user ").
                    setParameter ("user", user).
                    getSingleResult ();
        }
        catch (EntityNotFoundException e) {
            // Ok
        }

        if (l != null) {
            lus = l;
        }
        else {
            lus.setUser (user);
            manager.persist(lus);     // < --- Try to persist (this is ok)
        }
    }

    return lus;
}

The problem was in the [LedgerUserSetting][5] EntityBean, where this following code caused the Exception:

@OneToOne (cascade = CascadeType.PERSIST)   // We shouldn't cascade
@JoinColumn (name = "username")
public CommonUser getUser ()  {
    return user;
}

Remove the cascade option and we end up with:

@OneToOne  // Thats more like it.
@JoinColumn (name = "username")
public CommonUser getUser ()  {
    return user;
}

[1]: https://sourceforge.net/project/showfiles.php?group_id=22866&package_id=132063&release_id=346762
[2]: http://www.jcp.org/en/jsr/detail?id=220
[3]: http://cvs.frifinans.dk:8080/viewrep/FriFinans/frifinans
[4]: http://cvs.frifinans.dk:8080/viewrep/FriFinans/frifinans/src/org/frifinans/server/stateless/UserSettingsBean.java?r=1.6
[5]: http://cvs.frifinans.dk:8080/viewrep/FriFinans/frifinans/src/org/frifinans/server/entity/LedgerUserSetting.java?r=1.5

This guide helps you learn the new EJB3 through a series of examples. It also helps you install JBoss with EJB3, so you are ready to try the examples.

* [JBoss EJB 3.0 and Extensions: TrailBlazer][1]

[1]: http://trailblazer.demo.jboss.com/EJB3Trail/

It is done! FriFinans has been rewritten, using the new EJB3. I guess that from now on, it will be much easier to add/change functionality. It will also be a lot easier for new developers to start coding, because of the new, slick model.

Just take a look at this little example:

* [Account Entity in EJB2 (old)][1]
* [Account Stateless Bean in EJB2 (old)][2]
* [Account Entity in EJB3 (new)][3]
* [Account Stateless Bean in EJB3 (new)][4]

[1]: http://cvs.frifinans.dk/frifinans/src/org/frifinans/server/model/Attic/LedgerAccountBean.java?rev=1.9&view=auto
[2]: http://cvs.frifinans.dk/frifinans/src/org/frifinans/server/function/Attic/AccountBean.java?rev=1.20&view=auto
[3]: http://cvs.frifinans.dk/frifinans/src/org/frifinans/server/entity/LedgerAccount.java?view=markup
[4]: http://cvs.frifinans.dk/frifinans/src/org/frifinans/server/stateless/AccountBean.java?view=markup

After having tried EJB3, I’m convinced that this is the right thing for the FriFinans project.
I think there are many problems with the old EJB2 model.

Some of the bad things about the EJB2 model:

* Not really JavaBeans we are working with. IMO: Not really OO!
* “FileHell”. To create an entity you’d have to create up to 7 files!!!
* XDoclet solved the “FileHell”, but created a more vendorlocked model,
where EJB Container-bindings and datasource-bindings are “inside!!!” the bean.

These issues are solved in EJB3:

* We are working with real POJO’s (Plain Old Java Objects).
* To create an entity, you create One entity bean class. Thats it! No more, no less.
* Vendor independent. Very easy to change DB or EJB Container.

The only minus about EJB3, which I have found so far, is that when you are creating SessionBeans (stateless/stateful), you have to create up to 2 interfaces. One for Remote and one for Local (if needed). I don’t understand why this couldn’t be just one file, which you could choose to expose to Remote or Local. But then again: I’m not completely in to this new EJB3 model, so maybe I’ve not just seen the purpose of this design…

Lately I have considered using EJB3 for FriFinans. EJB3 gives a much more clean server code. Take a look at [this article][EJB3], which discusses the new EJB3 model.

I think I will give it a try and post again when I have concluded what to do.

* [JBoss impementation of EJB3][JBossEJB3]
* [EJB3 specification][EJBSpec]

[EJB3]: http://www.jroller.com/comments/raghukodali/Weblog/does_ejb_3_0_really
[JBossEJB3]: http://www.jboss.org/products/ejb3
[EJBSpec]: http://java.sun.com/products/ejb/docs.html