Quickfix: Hibernate integration for ZK applications

If you’re using the awesome ZK framework together with Hibernate you might run into some issues where Hibernate behaves a little funky while the application runs.

The trouble’s cause

According to the developers of ZK, the reason for this is ZK’s multi-threaded event model. Whenever an event gets fired, a new event thread is created. This will lead to multiple instances of the Hibernate session class (one per thread) and thus lead to unpredictable behavior.

The quick fix

If you don’t want to read the whole blah-blah in the linked article, here is the quick fix. Just insert the following lines into your zk.xml file and you should be fine:

<!-- Hibernate SessionFactory life cycle -->
<description>Hibernate SessionFactory life cycle</description>

<!-- Hibernate OpenSessionInView life cycle -->
<description>Hibernate Open Session In View life cycle</description>

Vector vs. ArrayList in multi-threaded Java applications

I lately had to play around a lot with collections in multi-threaded environments and learned several new stuff that I’d like to share:

Vector vs. ArrayList

Where is the difference between both? When should you use ArrayList and when is the time to use Vector?
This is a very common question and often leads to huge discussions.
For me, the following two points are sufficient in order to decide when to use what:

  1. Vector is threadsafe. ArrayList is not.
    That means that you can have two threads accessing (and manipulating) a vector without having to worry about mutual exclusion.
  2. ArrayList is a little faster than Vector but not threadsafe.

Lists in multi-threaded environments

Now, if you have multiple threads in your application which both access collections at the same time, you might run into issues because this is simply not allowed. Think of the list as a piece of cake. Only one guy can eat it.

As mentioned above, Vector is threadsafe, so in theory you could assume that it’s perfectly alright if you only use Vectors through the whole application. That’s wrong. See the example below, which will cause a ConcurrentModificationException.

So, if you have thread 1 doing this:

List<Person> persons = new Vector<Person>();

for (Person person : persons){
	if (person.getName().equals("timo")){

.. and thread 2 does the same thing at the same time, you’ll most likely face a ConcurrentModificationException.

That’s because

  1. The “for (Person person : persons)” syntax creates an implicit iterator which is not thread safe.
  2. You cannot iterate over a collection and modify it at the same time.

Solution: Use the synchronized keyword and iterate over a copy but modify the original

List<Person> persons = new Vector<Person>();

	List<Person> copy = new Vector<Person>(persons);
	for (Person person : copy){
		if (person.getName().equals("timo")){

Wrapping the for loop into a synchronized block will synchronize access between multiple threads.
This must be done in all threads which could possibly manipulate the vector. Adding the synchronized keyword in just thread 1 will not work unless thread 2 also uses this keyword.
Also, the above code creates a copy of the original list and iterates over it. Note that the call of persons.remove(person) happens on the orignal list.

Flash – The new Java?

Update: After hearing about some misunderstandings regarding this post, I want to make sure, that the title “Flash – The new Java?” is of course targeted at front-ent software engineering, not backend. Although it’s obvious that noone would ever try to build a web application backend using Flash, I just wanted to make sure that you understand what I am talking about :-)

Flash/AIR seems to really become a serious alternative development environment if it comes up to multi-platform requirements for desktop- and mobile applications.

With one codebase (in ActionScript3), it’s possible to target:

  • Windows
  • Mac OS
  • Linux
  • iPhone, iPad
  • Android
  • Web OS

Don’t missunderstand me. If I say “desktop application”, I am not talking about ones which run inside a browser. Instead, they are being installed on the local file system and run inside a runtime environment. Think of them like Java applications running inside the Java Virtual Machine.

Check out this youtube video where Flash’s multi-platform capabilities are being demonstrated.

Note: Since both, the iPhone as well as iPad, don’t support the Flash runtime environment, it is possible to compile Flash into native iPhone applications, which behave 100% as their originals (I, personally find this unbelievable!).
On all other platforms, there is a runtime environment, called AIR (Adobe Integrated Runtime), available. On Android phones, there is even a Flash Player installed which makes it possible to run Flash apps inside the browser. – Just one of the reasons why I am gonna buy the new HTC Legend in the next days. Sorry Apple :-P

For me, Flash is the new Java, which always claimed to be the platform-independent runtime environment for front-end applications. As good as Java is for backend-development on the web, the more it failed on the desktop and mobile phones.

Flash is not an animation-tool for designers anymore. It’s a serious SDK for writing great front-end applications in half of the time one would need using Java. ActionScript3 is a great programming language based on the object-oriented principles of java with some syntax similarities with JavaScript. In AS3, the developer has everything to code robust applications in OO-style: Classes, interfaces, polymorphism, events and more. Using Eclipse as the IDE of choice, one gets all the benefits of it like great debugging tools, for example.
Check this link out for a comparison between Java and AS3 syntax. You won’t see that much of a difference.

Flash is better than Java

Stop! Before you send me a letter-bomb, read this article first :-)

Update: After hearing about some misunderstandings regarding this post, I want to make sure, that the title “Flash is better than Java” is of course targeted at front-ent software engineering, not backend. Although it’s obvious that noone would ever try to build a web application backend using Flash, I just wanted to make sure that you understand what I am talking about :-)

If I talk to people and tell them that I love to use Flex, most say: “Eewwhh! Flex? Isn’t that… Flash? This animation thingy?”.

That’s what often makes me go nuts.

Don’t get me wrong. The Flash Player itself is shitty. It’s slow, unstable and has some serious issues regarding security. On the other side: The developer-side for Flex-coders is absolutely great and professional. ActionScript3 is an awesome object-oriented programming language and the nice features of MXML, like easy data binding, make it possible to create good-looking applications in no time.

If combined with the AIR runtime environment, which makes it possible to compile Flash applications into installable multi-platform desktop programs, I’d even go so far to say that it’s better than Java (on both, Desktop and Web).

Big IT-companies like IBM or SAP currently run experiments in order to verify if it is possible to replace Java (desktop-)frontends using Flex. This would shorten development time (since using Flex is so easy) and thus reduce costs. On top of that, their customers achieve great-looking and modern graphic user interfaces.
This replacement strategy does totally make sense, because Flex works great with common Java-based SOA architectures. Technically seen, it’s no problem to throw away an existing Java-frontend and replace it with a Flex version by simply connecting it to an existing J2EE backend. The reason: Using RPC techniques like class/object-sharing between Java and Flex over distributed networks is no problem since both languages are so similar regarding their object-oriented philosophy, as well as syntax. If you don’t believe me, read this.

Further, with the new Flex 4 platform, it is possible to improve the workflow between UI-designers and programmers using Flash Builder 4 and Adobe Catalyst. The idea is as follows: Before Flex 4, designers created user-interfaces and sent these to the developers, who then started transforming these drafts into actual programs. Now, if the designer wants any changes to the layout, he needs to ask the the developer to implement these changes. This is not very efficient. With Flex 4 and Catalyst the whole workflow has changed: Designers now create the user-interface using Illustrator. Then, they export their drafts into a *.fxb file using Catalyst, which needs to be sent to the developer, who just has to import this file into the project and can thus focus on the actual application logic instead of bothering with the UI. If the designer now wants any changes to the UI, he simply updates his *.fxb file and the developer re-imports it. Done. Can Java do this? No.

I don’t want to start a flame war, this is just my personal opinion from my experience with both worlds of Java and Flash.

There is no Flex Developer!

In case you have ever been asking yourself how to call yourself if you develop Flash-based applications, here is the ultimate guide:

  • If you use the Eclipse-based IDE called “Flash Builder” in order to create Rich Internet Applications using AS3 and MXML, you are a Flash Developer (with Focus on Flex).
  • If you use the design- and animation tool Flash CS in order to create interactive movies, you are a Flash Designer.

There is no Flex Developer!