Author Archives: Amit

Labeling Connections in UCP – Adjusting with the API

After decompiling the ucp jar file, I was able to understand the labeling connection callback better and got to know that the implementation didn’t completely match with the jdbc developer guide. This post will detail out on a couple of mismatch’d points from the developer guide.

Read the rest of this entry

Java Decompiler – JD-GUI Features

Recently I came across some really nice features of the jd-gui java decompiler. This decompiler seems to behave as an IDE for reading class files. I am using the 0.3.3 version of the library.

Read the rest of this entry

JMX Monitoring Support For Local Process On Windows

If you are monitoring a local jvm process using jconsole or jvisualvm and aren’t able to view the Threads tab on jvisualvm or connect to jconsole, this post should provide some troubleshooting information.

Any application that is started on the Java SE 6 platform will support the Attach API, and so will automatically be made available for local monitoring and management when needed. This out of box jmx support doesn’t work on Windows though. On Windows platforms, for security reasons, local monitoring and management is only supported if your default temporary directory is on a file system that allows the setting of permissions on files and directories (for example, on a New Technology File System (NTFS) file system). It is not supported on a File Allocation Table (FAT) file system, which provides insufficient access controls.

Read the rest of this entry

Oracle Case Insensitivity and JDBC Drivers

Natively Oracle is case sensitive which means if you executed a query for e.g.

select emp_id, emp_salary from employee where emp_name = 'amit';

it would search for the string ‘amit’ in lowercase. We can switch this to make the search behave case insensitively by setting the NLS_COMP and NLS_SORT parameters from Oracle’s globalization support which was previously called National Language Support (NLS).

Read the rest of this entry

Generating Delegate Methods In Java Using Intellij

If you are implementing a decorator pattern, you would have to implement one or more interfaces. If the interface has large number of methods, delegating each of them might be troublesome and error prone. Since there is no good way in java to implement delegates (implementing a proxy with an invocation handler is a workaround :)), a better option would be to take help of the IDE to be sure that the implementation is not error prone. Here is how to do it using Intellij IDE

Read the rest of this entry

Function Arguments – Clean Code

As an extension to the last post on functions, below are some points about function arguments.

Function Arguments

  • Function arguments take a lot of conceptual power while reading and understanding code.
  • They should be at the same level of abstraction as the function name. For e.g. String createTestableHtml(PageData pageData). Reading this function signature forces you to know the detail which isn’t particular important at that point in time.
  • The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification—and then shouldn’t be used anyway.
  • Output arguments

    • Arguments are most naturally interpreted as inputs to a function.
    • Output arguments should be avoided. If your function must change the state of something, have it change the state of its owning object

    Read the rest of this entry

    Configuring Ehcache with JPA, Hibernate & Spring

    This post details on the steps required to configure ehcache as the second level hibernate cache when jpa, hibernate and spring are used in combination

    Configuring the cache

    Add the below properties to the persistence.xml configuration file

    <property name="hibernate.cache.use_second_level_cache" value="true"/>
    <property name="hibernate.cache.use_query_cache" value="true"/>
    <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
    <property name="hibernate.generate_statistics" value="true"/>

    Read the rest of this entry

    Email Alerts With Logback

    Logback provides a support to send logged messages in an email through an SMTPAppender. The SMTPAppender accumulates logging events in one or more fixed-size buffers and sends the contents of the appropriate buffer in an email after a user-specified event occurs. By default, the email transmission is triggered by a logging event of level ERROR or higher. Moreover, by default, a single buffer is used for all events.

    A sample smtp appender configuration would look like below

    Read the rest of this entry

    Functions – Clean Code Video

    While watching the episode 3 of the clean coders video I noted down some important points.

    Functions are the first line of code organization in your application.

    Small Functions

    • The first rule to having clean functions is that they should be small.
    • Typically classes hide in large functions.
    • If you find functions that can be divided into several functional areas and you have variables that are used by all those areas then what you really have is a class. After all, a class is a group of functions that use a common set of variables.
  • Having lot’s of small well named functions is good. They act as sign post which help the reader to navigate through your code easily.
  • We should not be worrying about the function call overhead since the current computers are so fast that this overhead might be less then a nanosecond. Instead we should take the advantage of such high speed hardware to improve our code for readability first.
  • Unloading of static fields in java

    I came across this interesting article about loading and unloading of static fields which details a couple of facts I was not aware of
    1. Loading – You can get a reference to a class, before it has been initialized (i.e its static fields being initialized or its static initializers being called), only when it is used does it get initialized. The link contains an example code which illustrates this fact.
    2. Unloading – Static fields are unloaded when the Class’ classloader is unloaded. This is unloaded when a GC is performed and there are no strong references from the threads’ stacks.

    This post adds more detail to the second point. Below are few reasons why classes are unloaded only when the classloader unloads.

    Read the rest of this entry