Monday, May 31, 2010

Transaction isolation level

Recently I have read very good explanation of transaction isolation levels in "Real World Java EE Patterns. Rethinking Best Practices." by Adam Bien.

Transactions were introduced for two reasons. To make complex operations atomic (all or nothing) and to give possibility to concurrently use resources. Without transactions it is possible that two users (or more specifically actors, this can be some parts of the system, not necessarly live users) read/write the same resource simultaneously, and changes made by one of them are lost and overwriten by the other one.

Consider following example document:

Cheesecake bottom
250g crunchy bisquits
100g butter

Cheesecake top
900g cottage cheese
250g mascarpone cheese
1 lemon

Imagine one user gets the document and the other user also is getting the same document. Both users have the same data. Now both of them are editing it. One likes sweeter cake bottom, so adds "2 spoons of sugar" to the recipe, and writes changed recipe back to the database. In database now it looks like this:

Cheesecake bottom
250g crunchy bisquits
100g butter
2 spoons of sugar

Cheesecake top
900g cottage cheese
250g mascarpone cheese
1 lemon

Meanwhile second user adds "2 eggs" to cheescake top recipe and saves his version:

Cheesecake bottom
250g crunchy bisquits
100g butter
2 spoons of sugar

Cheesecake top
900g cottage cheese
250g mascarpone cheese
1 lemon
2 eggs
Change made by the first user is lost. In some cases it can lead to inconsistent data.

So that's why transactions were introduced. Transaction isolates changes made by one user from changes made by the other. There are 4 levels of transaction isolation.

Serializable

Transaction locks all necessary resources. If user wants to read or write some resource, it is locked and no one else can read or write it. This level of isolation can easly lead to deadlocks:
  1. User Ann opens transaction. Ann needs resource A, transaction locks it.
  2. User John opens another transaction. John needs resource B, transaction locks it.
  3. Ann needs resource B, but it is locked, so Ann's transaction needs to wait until it is released
  4. John needs resource A, but it is locked. John's transaction waits until it is released. Both users wait endlessly.

If John didn't need A, he finishes his transaction, B is released and Ann can finish her transaction too.

Repeatable reads

Guarantees that the same query will return the same results if executed in one transaction. Even if other transaction modifies resource meanwhile. Exception is adding - new rows can appear in query result. If another transaction deletes existing rows or modifies them, this changes are not visible.
  1. Ann opens transaction. She reads names of java4people organizers: "Stawicki" and "Gruchała".
  2. Bob opens transaction and deletes "Stawicki". Bob commits changes and closes transaction.
  3. Again Ann reads names. She gets "Stawicki" and "Gruchała".

If Bob added some name, Ann would read it too.

Read commited

The same query can return different result even in one transaction if another transaction makes changes and commits them.
  1. Ann opens transaction and reads names of java4people organizers: "Stawicki" and "Gruchała".
  2. Bob opens transaction and deletes "Stawicki".
  3. Ann reads names again. She gets "Stawicki" and "Gruchała".
  4. Bob commits his transaction
  5. Ann reads names again (still in one transaction). She gets "Gruchała".

Read uncommited

Like no transactions at all. It only gives us atomic operations. User can rollback all the changes in transaction. Changes are visible to other transactions even before commit. Of course, if transaction that made changes is rolled back, changes are not visible any more.

Saturday, May 15, 2010

After GeeCON 2010

This year GeeCON took place in Poznań which is much closer to Szczecin than Cracow. I expected it to be as good as a year before, maybe I expected too much. I don't mean it was bad, but previous year it was so great that maybe it was hard to keep this level. Or maybe I was not choosing right presentations. There were three tracks this year so choice was sometimes difficult.

Like a previous year there was "University Day" before the conference. University Day is day of workshops on various topics. I have chosen Gradle, like many other people. Too many unfortunately. Hans said there are too many attendants to make exercises for everyone, so only he coded live. I missed coding myself.

First interesting concept I heard about was Object Teams. It is a new idea of modularizing our programs, based on entities, collaborations and roles. All three look pretty much like java classes. In OOP there are objects which are data and methods. We can use objects to modularize our applications, but often connections between modules are becoming complicated. Creators of Object Teams concluded that modularization and OOP is not about modules/objects itself but about connections between them. So there are entities, which represent data. There are collaborations, that represent operations on data, and roles. In a collaboration, each entity plays some role. Everything looks pretty and neat, and I wonder if it is going to become popular in coming years. I must admit I haven't heard about it before.

Quite interesting presentation was about JSF 2.0 by Ed Burns. Nothing quite new for me, but if someone thinks JSF didn't change much since 1.2 version he should attend this presentation (or watch on GeeCON's channel on parleys.com when it becomes available ;)). Guys who created version 2.0 addressed all the problems developers were complaining about with previous versions. I remember Seam was framework which was built on JSF and addressed such problems. Now, when it is solved in JSF itself, I wonder what are differences between Seam and JSF 2.0.

On the second day Jonas Bonér was talking about actors, agents, STM and other solutions making concurrent programming much easier than threads with shared state and locks. Next time I'll need to do some concurrent programming in Java I'll definitely use something from Akka.

Vaclav Pech's presentation was quite similar. He also talked about concurrent programming with actors, but in Groovy. As usual Vaclav gave very good presentation.

Vaadin is the next thing I want to use. At work I use GWT and I don't really like it. GWT compilation is very long and asynchronous callbacks are making code more complicated. Besides most of work is done on the server anyway. Programming in Vaadin is very similar to programming in GWT, but everything is server side. Vaadin uses GWT for presentation, so it is possible to write custom components for Vaadin in GWT, but then compilation is needed only once. Damn, this could save many hours of my life.

Bruno Bossola's presentation was very good and funny. No suprise here :) Bruno talks about things that many developers really need. I agree somewhere we lost real Object Oriented programming. Bruno also mentioned a book which I feel is unfairly forgotten. "Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development" by Craig Larman. When I started working for NCDC more than few years ago, my boss put this book to company's library and recommended it to us. I read it and I feel it made me a better developer. If you put all your code into one huge class or even method, buy or borrow this book and read it. Even if it's old it's worth it. Some things don't change even in IT ;)

If I can have some hint for the organizers I'd like to suggest to make presentations longer. Many presenters didn't manage to show everything they wanted. I think 1.5 hour is minimum.

I heard food was also a problem on the first day. It didn't suffice for everybody, but I managed to grab my plate so I don't complain :)

Saturday, April 24, 2010

SzJUG member, Filip "Filus" Pająk presentation on TestNG

Recently (last Thursday) I had pleasure to attend Szczecin JUG meeting at which Filip presented TestNG. Presentation was great. I knew Filip is a perfectionist, and when he does something it is just perfect :), but I didn't know he has such great speaker talent! I felt he really cares about being understood.

Filip was very well prepared. A lot of knowledge and a lot of great examples. Examples were prepared before and were short and simple. Just what you need to understand some issue, no less and no more. I think it is better than coding "live" when something can blow up. Show example, run it and show results.

Filip claimed he is no "guru" and doesn't know TestNG very well, but as far as I remember, he knew answer to every question :). I hope it was not his last presentation at Szczecin JUG. Or maybe he'll present somewhere else now? Who knows?

Saturday, April 17, 2010

Szczecin JUG Meeting. Filip "Filus" Pająk on TestNG.

On 22nd of April 2010 Filip "Filus" Pająk will present TestNG. If you are around Szczecin, feel invited. It's free :)

Filus is experienced Java programmer and tester. When he does something, you can be sure it's going to be good. I think he'll make great presentation.

I am very glad Szczecin JUG is organising another meeting. It's been some time since the last one.

Maybe I should also present something? JavaFX? Scala?

Monday, March 22, 2010

JavaScript training

This weekend I was able to take part in JavaScript training by Damian Wielgosik . As I was not very experienced JavaScript developer, I have learned a lot. I also got convinced that in JavaScript everything is possible :)

I am impressed by the trainer. I was quite surprised when I found out that it was the third training he conducted. I thought he is much more experienced. He had great "contact with the audience", atmosphere was very friendly, nobody was afraid to ask questions or discuss. Thank you Damian!

Two days full of training is quite a much time, so there was also place for exercises. This was very good, nothing consolidates knowledge like some exercises and because Damian was always there to help if we were stuck, we were not wasting time. Doing exercises in groups was also very good idea. We worked as a team and helped each other. I never doubted in efficiency of pair programming :)

Scope of topics was impressive. I learned a lot about prototypes and constructors, which was a bit strange for Java programmer (but not so much if he was also learning Ruby ;)) I think we covered pretty much everything in JS, from basics (which we run through quite fast) to optimization.

I would also like to thank other participants for great atmosphere, discussions, questions, help and info about "pierogarnia" :) I wish I could attend more such good trainings.

Friday, March 19, 2010

Review: Programming Scala, by Venkat Subramaniam

Programming Scala: Tackle Multi-Core Complexity on the Java Virtual Machine (Pragmatic Programmers)Courtesy of The Pragmatic Bookshelf I was able to read "Programming Scala" by Venkat Subramaniam. If you are a Java programmer, this book is a great introduction to Scala for you. It is packed with knowledge. There was not even one chapter after reading which I could say "well, this was quite obvious, I could expect this". Book is targeted for experienced Java programmers, so author does not cover things we already know. This was great for me, my time was not wasted learning what I don't need. Instead, I was led from easier topics in Scala (like classes, typing, closures) to more difficult ones (pattern matching, actors and concurrency) at a nice pace. Most topics are explained quite clearly and understandable. There are few which I had difficulties understanding and I needed to fiddle with some code to get it. But fiddling with code is fun for programmer, isn't it? One of such topics were actors, which is a great feature in Scala and something completely new for Java programmer (unless he knows Erlang too ;)). It took me some time with IDE to understand actors, but it was also fun.

The book not only teaches how to program in Scala, but also shows tools to run these programs. Knowing that Scala doesn't necessarly need to be compiled to run and that it can be run as a script can be very useful if you need to do some simple job on your system. Author shows how to use it as a scripting language. I also liked the last chapter in which author creates Scala application using various features covered in the book. It was a good way to sum up.

The only weak point of this book are examples. Especially at the end of the book they become more and more difficult and not always clearly explained. Another disadvantage of examples is their little connection with reality. Have you ever need to count letters in blog URL? In some cases they even show bad practice like throwing general Exception. I know it's only example, but educational book should not promote bad practice even there.

Said all this, I would recommend this book to every Java programmer who wants to learn Scala. If you are not afraid to fiddle a little with code (and you should not be, you are a programmer after all;) ) it is a very good book for you and you are it's perfect target.

Last but not least, not only author, but also editors deserve praise. I have PDF edition and I can login on pragprog.com page anytime and download it. I don't have to worry that if I loose it - it's lost for good. Moreover there are .mobi and .epub formats available. I could read the book on my phone on a bus or a tram which was very convenient. And there is even more - the books are DRM free, which shows publishers not only care for readers comfort, but also trust us. I appreciate that.

Tuesday, March 2, 2010

Simple AJAX with JSF 2.0

In JSF 2.0, there are tags added to make AJAX calls easy. Without further ado:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core">
<h:head>
   <h:outputScript name="jsf.js" library="javax.faces" target="body"></h:outputScript>
   <title>Ajax</title>
</h:head>
<h:body>
   <f:view contentType="text/html; charset=UTF-8">
       <h1>Hello Ajax World!</h1>
       <h:form id="form">
           <h:inputText id="name" value="#{nameMBean.name}"/>

           <h:commandButton value="Reverse name via Ajax!">
               <f:ajax execute="name" render="reverseName"/>
           </h:commandButton>
           <h:outputText id="reverseName" value="#{nameMBean.reverseName}"/>
       </h:form>
   </f:view>
</h:body>
</html>

First things first. To use AJAX in JSF 2.0, we have to attach jsf.js file (line 8). Then we are ready.

Most interesting is line 18. <f:ajax> added to command button makes AJAX action fire when button is pressed. We could as well put it into <h:inputText> element, then it would fire on change of text inside (onchange event).

Parameter execute tells JSF which parts it should send to server for model update. This could be also form, for all the parameters in the form, or list of element ids separated by space. Parameter render denotes which parts of view should be rerendered after receiving AJAX response. Here we can also put more than one.

We can put more parameters to <f:ajax>, like event, where we can specify on which event AJAX action should occur (not necessarly click event), or listener, where we can call some method on managed bean.