Wednesday, November 21, 2012

To the hashcode() and equals() problem

Oracle connect by prior with order siblings

To not forget:

with hier as (
  select 'Foo' as name, 1 as id, null as pid from dual union all
  select 'FooBar', 2, 1 from dual union all
  select 'Bar', 3, null from dual union all
  select 'BarFoo', 4, 3 from dual union all
  select 'FooZo', 5, 1 from dual union all
  select 'FooBarFoo', 6, 2 from dual union all
  select 'FooZoo', 7, 1 from dual union all
  select 'FooZooo', 8, 1 from dual
select LPAD(' ', 2*(LEVEL-1))||name
from hier
start with pid is null
connect by prior id=pid
order siblings by id;

Monday, July 9, 2012

Guice a bit more convenient than Spring in injecting circular dependencies in session beans

It is not very easy to find, but Spring does not support circular dependencies for scopes other than Singleton (default scope for Spring). That may be very frustrating. There are some workarounds, but since I met this limitation right at the very beginning of the project, I just moved to Guice, that supports session scoped beans with circular references out-of-the-box.

If you want to try it yourself, or try some workarounds for Spring - I have created a small simple github project as an illustration.

EDIT: Actually now there is an easy workaround to this intentional Spring behavior. You may use javax.inject.Provider of session-scoped beans inside other session-scoped beans and introduce circular dependencies. This will not work in Spring 3.1.0-3.1.2 due to a regression, but this bug is fixed in 3.1.3 Spring. I have updated the example. For session-scoped beans this workaround is really very easy since you may call provider.get() any number of times in one session and you will always recieve one and the same bean. This may be a bit trickier with prototype-scoped beans or any custom scope but this idea may in some form apply to them too.

So Spring is not really owned and now allow you to do the same thing. But still you need to remember that Spring guys do not like circular dependencies in beans and they have reasons for that.

Thrift exposed via webapp servlet

Thrift services can be exposed via servlets. Now the TServlet class is part of the core thrift package in java. It is relatively easy to use, but there are some nuances.

First - it requires some Processor and Protocol to be used. Example of such usage maybe found here. Though it is about OSGI Servlets, not web applications - this example is easy and small. However it is stated there that due to violation of Servlet specification (no default constructor) it is not possible to use this servlet in web application. Actually it can be used.

  • We can either use it directly with some covering code, like in this stackoverflow answer.
  • We can use any standard way of delegating servlets like Spring, or Guice and maybe some other frameworks that most likely are already used in your application.
I have created some small test project to illustrate these options (hand-written delegating code, Spring and Guice) and placed it on github. Maybe it will be useful to someone except me ;-)

Saturday, February 18, 2012

Fedora 16x64 and JavaFX 2.1.0-beta

JavaFx is currently unavailable for Linux x86_64. So we will use 32bit one. The steps are:
1. Download jdk-7u3-linux-i586.tar.gz from here (I have  jdk-7u3-linux-x64.rpm already installed and I don't want to mix 32 bit jvm to my package system)
2. Unpack it to /opt
3. Add to your profile (e.g. /etc/profile)

export JAVA_HOME="/opt/jdk1.7.0_03"1
4. Download something like from here and unzip it to /opt
5. Install packages to support 32 bit java (maybe not needed if you have installed rpm):
sudo yum install libgtk-java.i686 gtk2-engines.i686 libxslt.i686

6.You also need to fully support HTML5 in JavaFX. It is not shipped nor in the main Fedora repo, nor in Rpmfusion. I have found it in the However this repo may come in conflict with Rpmfusion. Anyway, with this repo installed you may do
sudo yum install libavcodec52.i686

That's it! Now you may install Netbeans 7.1 and try javaFX samples. Or try them in Eclipse with this plugin. I recommend to look through these examples (code for this book - have not read it and I'm seriously doubt whether it is worth reading it). They are not very good, some of them do not compile (but are easily fixable), some of them do not work on Fedora x86_64, but I have not found anything better yet.

Thursday, February 9, 2012

Order of enums in hashset and hashmap

Recently I have found myself writing a complex sql-builder for two weeks already. Of cause I used TDD technique and this was much less like a pain comparing to the times when I didn't wrote any tests. In some moment I decided that just operating with strings in some thousand or more lines of code is not very cool and started to write some OOD for the problem. In the resulting type hierarchy there were some enums. And I used these enums as keys in HashMap. Maybe some curious reader have already guessed what this post is about ;-) If not or if you just want some detailed illustration, read the rest:

Thursday, January 19, 2012

Synchronized vs ThreadLocal SimpleDateFormat

EDIT: This benchmark is not really reliable, as any other benchmark that is done just for fun - but here is the new one that pretends to have more sense.
It is widely known that SimpleDateFormat on the Java platform is not thread-safe. There are basically three approaches to deal with this unsafety. First - each time create new instance. Should be not very good, since it is not very simple and lightweight object. Synchronizing - I think should be better. And the third approach, that is used mostly in real world, I think - using ThreadLocal. I decided to test all these approaches. The result of my test is on the graph:
On the vertical axis here is the time, so the less - the better. On the horizontal axis is number of threads concurrently formatting dates. So what can be concluded from this graph?

  • Either SimpleDateFormat is not so heavyweight object, either synchronization is so heavy operation (I incline to the second option), but there is very little difference in synchronizing on one and the same instance of creating new one each time it is needed. However synchronizing is a bit better.
  • Widely used approach among the naive is the best.
EDIT: Doing this test I used Java6 from Sun. I have retested it with Java7 from Oracle and get quite different picture. The hardware is different too, so these pictures can not be compared with each other in absolute numbers. But difference in the relations between "new instance each invocation" approach and one synchronized instance is very interesting:

Anyway, here is the code I used for testing:

Monday, January 16, 2012

Oracle 11g denormalizing hierarchy with recursive subquery factoring and LISTAGG function

In Oracle 11g there are many new features. From the developer's point of view there are a bit less such features, but still there are some very impressive. Among these features there are recursive subquery factoring and LISTAGG grouping function. Here is how they can be combined together: