Categories

Tag Cloud

Archives

Optimistic Locking in Hibernate and JPA

When writing applications that are going to be accessed by more than one user at a time, you run the risk of the underlying data being corrupted. One solution to this problem is to implement row locks on the data being modified. However, should a process or a user be modifying several rows, this lock can lead to performance degradation as the DB begins to wait for locks to be released.

A better solution is to use Optimistic Locking. This is where we monitor the row being modified and allow other users to access it. No lock is being applied here. However, once the row has been saved back to the database, all the rows that are still being viewed become out-of-date, or stale.

The simplest method by which the row can be monitored is to employ a “version” column. This is a simple counter that increments every time an update is performed on the row.

In JPA annotations this is represented by the @Version annotation.

@Version
@Column(name = "version", nullable = false, length = 5)
public int getVersion() { ... }

Now, when this object is saved the version number is automatically incremented. Hibernate will take care of this for us. When a save is attempted on a stale object, Hibernate will throw a StaleObjectStateException (wrapped in a HibernateOptimisticLockingFailureException).

RSS Feed Add to Technorati Favorites Add to Del.icio.us Stumble It! Submit to Slashdot Submit to Buzz! Digg It!

All About AIR

Smashing Magazine has compiled a rather great article about Air, filled with linkes to tutorials, forums, official articles, and other lumps of great useful stuff.

You can read it here.

RSS Feed Add to Technorati Favorites Add to Del.icio.us Stumble It! Submit to Slashdot Submit to Buzz! Digg It!

The Guardian of Family Values

I thought I’d publish a quick follow-up to my story on Open Media Content of a couple of weeks ago.

The first major piece of work has been done with the Guardian’s back catalogue and categorically it can be said that, year on year, f**ks are stable.

Can it all be due to Charlie Brooker’s Potty Mouth? Warning, the following link may offend some people.

RSS Feed Add to Technorati Favorites Add to Del.icio.us Stumble It! Submit to Slashdot Submit to Buzz! Digg It!

How to be Agile

A few years ago I was lucky enough to be involved in a meeting with Scott Ambler, in which he opened my eyes to the Agile way. Since then I have been an ardent supporter of Agile processes.

From that point I have striven to run all my projects in an Agile manner, and have actively sought to work at companies which have an Agile development approach. However, I have started to see a disturbing trend amongst recruitment consultants about what Agile really is.

Wikipedia defines Agile as:

“..a group of software development methodologies that are based on similar principles.”

These principles include: frequent review and refactoring; teamwork; self-organisation; accountability; rapid delivery; and aligning the customer needs to the development goals.

The various Agile practices are: Test Driven Development (TDD); Business Collaboration, sometimes called Business Driven Development (BDD); Continuous Integration; Pair Programming; Task Management.

In the above I have not mentioned 2-week iterations, or daily stand-ups because these are both aspects of Scrum, and I think this is where the confusion happens.

There are various methodologies out there that can be considered Agile - Scrum and XP for example (note that RUP is not an Agile process) - and it seems to be that recruiters are given a checklist of what it is to be “Agile”. Most commonly the list seems aligned to Scrum, and all too commonly it appears that if you don’t check every box, then you haven’t been in an Agile environment.

I don’t think the blame lies wholly with the recruiters, we shouldn’t expect them to understand the minutia of every Agile methodology. However, we should try to moved away from the idea that Agile is a rigid set of all or nothing processes, it’s called Agile for a reason.

RSS Feed Add to Technorati Favorites Add to Del.icio.us Stumble It! Submit to Slashdot Submit to Buzz! Digg It!

When is it right to Autowire beans?

Short answer: Never.

Long answer: Never, no, uh-uh, no.

Longer answer: Well OK maybe sometimes.

Automatic wiring is a great time saver for small applications, such as “Hello, World”. Anything more than that and you are heading down a path paved with bad practices and at best it can lead to an inflexible architecture in larger applications.

The various ways of autowiring can be set-up as follows:

// Spring Context XML
<bean id="foo" class="com.springcode.example.Foo"/>

<bean id="useByName" autowire="byName" class="..."/>
<bean id="useByType" autowire="byType" class="..."/>
<bean id="useConstructor" autowire="constructor" class="..."/>
<bean id="useAutodetect" autowire="autodetect" class="..."/>

// Java Class
private Foo foo;
private Foo foo2;

public MyClass() { ... }
public MyClass(Foo foo) { ... }

public setFoo(Foo foo) { ... }
public setFoo2(Foo foo2) { ... }

Use of byName will call the no-args constructor and set a value for foo, but not foo2; use of byType will set values for foo and foo2; use of constructor will call the single arg constructor; and use of autodetect will use the byType autowiring as there is a no-args constructor.

Note that we could have used the @Autowired annotation in MyClass and avoided use of the single arg constructor and the setters.

@Autowired
private Foo foo;
@Autowired
private Foo foo2;

Each method of autowiring leads to its own distinctive issues.

  • byName - This sounds like it could be the best option if you want to avoid ambiguity, but you can easily find yourself giving your beans really obtuse “unique” names. This ends up defeating the benefit of Spring doing the work for you.
  • byType - This encounters issues in larger applications. By using byType you are forced to only have one bean of each type in your BeanFactory, this then causes issues if you are attempting to have beans with different configurations for the same type.
  • constructor - Encounters the same issues as byType.
  • autodetect - The functionality behind autodetect is just the same as in byType and in constructor. Thus, you encounter the same issues.

So is there anywhere you can use autowiring, where it wont break things? Well I have been told that in unit testing it is sometimes advisable to use byType autowiring. However, even here you can just use manual wiring.

RSS Feed Add to Technorati Favorites Add to Del.icio.us Stumble It! Submit to Slashdot Submit to Buzz! Digg It!

Readable code is as important as good code.

I am somewhat of a code fascist, I like my curly braces to be in just the right place, and don’t get me started on multiple return statements.

I’m just as bad when it comes to Flex. I’ve seen too many messy mxml and ActionScript files where nothing is where you’d expect it. However, there are infinitely more strict individuals than I, some going so far as to insist on the order of attributes in tags (”id” first, always “id” first).

Luckily this responsibility can now be removed from the developer to some extent, and and put in to the hands of someone like me.

An Eclipse plug-in has been released that formats the mxml and ActionScript for you. it’ll make everything neat and tidy but more importantly, consistent, across developers.

Download it here. then use it.

RSS Feed Add to Technorati Favorites Add to Del.icio.us Stumble It! Submit to Slashdot Submit to Buzz! Digg It!

Magento Catalog Bug

I’ve just been doing some work on a Magento e-commerce site and had a problem editing product information. In case you’re unaware Magento is a very good open-source e-commerce platform, you can find out more about it here.

The problem arises once you’ve set up your basic store, and if you’ve followed some of the helpful guides, you’ll arrive at a position with a website displayed in what is called a “store view”. A store view can be used for things such as language demarcation, seasonal look-and-feel, etc.

e.g.

  • Default
    • MyStore
      • MyApplication
        • English
        • Spanish
        • German

I’d added a couple of products in to the system via the admin console, exported them as CSV to add some more and then imported them all again in to my default store view (set as “English”). Then I entered the admin console, catalog tab, selected a product and edited it. However, none of the changes appeared on site. Everything was there on the site as I had imported it and everything I had changed was in the admin screens.

The issue seemed to revolve around having a single “store view” when you have a single store.

Magento makes the assumption that when you only have a single store, with a single store view, then all the content for any products will be the same as the default content. This makes perfect sense, you would want the only view to have the default information.

What makes it tricky is that this isn’t explained anywhere. What makes it more tricky is that to be helpful, the product editing page removes the drop-down allowing you to switch between stores and views.

The problem comes when you export from, or import into your view. The export will create a file tagged with the ID of your view. When you import this file (in to your view) if these are new products they will also create a new default copy (ID:0) else they will just update the store view copy.

As there is no drop down any edits you make are made to the default information, not the newly imported store view information. This means that if your website is showing your store view, no changes from the admin console will be seen.

The solution, if you only have a single view on a single store, is to upload in to the default store, with no view specific ID on the import. Internally Magento will “replicate” to your view so that changes made in the Admin console will be seen on site.

If you then add a new view, say for example for a new language, then on creating that, Magento will do the same replication from the default. To get language specific values you then need to import those changes, with the correct store view id, back in to the system. The edit product page will then display the drop down you are expecting.

In the end I had to wander through the database, and make the connections. Please someone, write some good docs for Magento.

RSS Feed Add to Technorati Favorites Add to Del.icio.us Stumble It! Submit to Slashdot Submit to Buzz! Digg It!

Open Media Content

It’s been a while since the BBC opened their back catalogue for members of Jo Public to play with and Backstage has been very successful. So you wonder why no other members of the “old” media world have decided to do the same thing?

Well this week the Guardian announced Open Platform at an event at King’s Place, London.

Their intention with Open Platform is to allow external partners to build applications based around the Guardian’s content. Much the same way the BBC has done. Requests are sent to the Guardian servers and the response returned in either XML, JSON or ATOM format.

They have also developed a set of client libraries so that you can build up your requests using Ruby, Python, PHP or Java.

The Open Platform is composed of two distinct areas, the Content API and the Data Store. The Content API is the mechanism to retrieve the data from the servers. The Data Store is their repository of, journalist vetted, data. Articles for you to pull.

I’m not sure how useful this will be, at the end of the day you’re only pulling an XML feed from the Guardian, but hopefully this will expand over coming month and I expect we’ll see some clever and innovative uses for the data being produced by the community.

RSS Feed Add to Technorati Favorites Add to Del.icio.us Stumble It! Submit to Slashdot Submit to Buzz! Digg It!

Tween

Wikipedia states that Tweening:

“…is the process of generating intermediate frames between two images to give the appearance that the first image evolves smoothly into the second image.”

There does exist a Tweener library for Flex (well, for ActionScript actually but it can be used in Flex) that can be found on the Google Code site here.

Andrew Trice has written an excellent article for Inside RIA on the subject here.

RSS Feed Add to Technorati Favorites Add to Del.icio.us Stumble It! Submit to Slashdot Submit to Buzz! Digg It!

Silverlight with Java

Soyatec, a French technology company, have just released Eclipse4SL, an open source plugin for Eclipse to handle Silverlight applications in Java projects. The purpose being to provide both a Silverlight development environment and greater interoperability between Silverlight and Java.

Eclipse4SL works with the Eclipse IDE and Rich Client Platform, and it can be downloaded from here.

Microsoft has been working with Soyatec to develop this, providing funds and technical guidance. Despite those who negatively respond to anything by Microsoft, this can only be a good thing. Anything that enables Silverlight to mature as a platform will mean that Adobe will have to respond and push Flex further.

RSS Feed Add to Technorati Favorites Add to Del.icio.us Stumble It! Submit to Slashdot Submit to Buzz! Digg It!