Friday, February 27, 2009

Database Connection Pooling in Hibernate

I was looking for a way to control connection pooling in Hibernate inside a Spring environment. God bless Apache foundation for their amazing code helps, as I found the answers in DBCP. Which-ever hibernate session factory you use, use the following data source bean in your configuration xml to control connection pooling:

<bean id="internalDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://127.0.0.1/myDatabase"/>
<property name="username" value="root"/>
<property name="password" value="abc123"/>
<property name="maxIdle" value="5"/>
<property name="maxActive" value="25"/>
<property name="minIdle" value="5"/>
<property name="initialSize" value="5"/>
</bean>


For all options and details, refer the API for BasicDataSource. And, if you are a maven user, don't forget to include the dependency:

<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>



 

Wednesday, February 25, 2009

Which IDE?

IDEs can be critical to productivity. As a developer, which is the one that I prefer?

There are three IDEs that I have used for development:  Idea, Eclipse and NetBeans.

Idea costs money, but it is worth it. Developing enterprise applications with it is easier than with other IDEs. Nevertheless it is not cheap and other IDEs are fast catching up with it.

Eclipse is too much customizable for its own good, making the "setting up of eclipse" so as it works with a plethora of commonly used enterprise technologies almost an art. Hence the learning curve can be sharp. If you are in a hurry to get the project running and don't have much experience with Eclipse , I would not recommend Eclipse. Eclipse is a good (and free) IDE and can be a developer's joy. Nevertheless it can be a pain for somebody new to it and not willing to spend so much effort customizing it.

I would have completely ruled out NetBeans before its version 6. In the past I saw it as a good Java GUI builder and thats it. But the recent release is impressive. It takes into account a lot of problems that are commonly faced using disparate technologies in enterprise development. It is configurable but comes with a lot of intelligent defaults that can get you up-to-speed pretty quickly. Still not as good as Idea, but is a good contender if you are not willing to spend. 

Thursday, January 15, 2009

Parsing CSV File

I just wanted to read a simple CSV file in the resources folder and parse it in my spring application. A simple class to do the same. csvFileName is a string property injected at runtime.

CSV Reader

Errata: My good friend and colleague Ben has rightly pointed out availability of generic csv parsers that can make life easier. I am exploring Java CSV and Flatpack as suggested by Ben.

Java CSV API
Java CSV Sample



Monday, December 15, 2008

Using Log4J in Spring: Effective Logging

Spring supports logging in its usual plug and use format. To use Apache's log4j in a Spring-Maven environments:

  1. Add the log4j dependency to the pom
  2. In web.xml of the application define spring listener for log4j and its config location
  3. In the config location put in the appropriate properties file defining your loggers



The log4j.properties at the above link is fully commented to help you(and me) understand how to use a properties file to split your logs at a finer level.

I have realized that in order to utilize the full power of log4j it is important to understand and use log4j.properties. This can help you to isolate logs from different part of of your application- something that can be really handy when you are trying to pin-point the source of trouble in a large system. The bigger and more distributed your system becomes, more important and useful logging appears.
As usual, I have focussed only on the practical aspects. For knowing more about log4j, see:





Tuesday, December 9, 2008

PostgreSQL Vs MySQL

My team decided to use PostgreSQL instead of MySQL. This got me interested in finding out why. Found these articles that helped me understand:

(a recent comparison, subtly votes in favor of PostgreSQL)
(this is the most concise comparison but is a little old, very nicely tabulated)
(a very old comparison to give some prespective)

In a nutshell, both are equally good in the current scenario; though before the 8th release of PostgreSQL,  MySQL held a distinct advantage. Currently PostgreSQL offers a wider range of features and on the whole MySQL is a bit faster. However with every release MySQL is inching towards more features and PostGreSQL towards more speed. MySQL is currently more widespread, perhaps because it is older and is preferred by the widespread PHP community. Another difference is the licensing. Both are open-source but MySQL is under GNU while PostgreSQL is under BSD.  

Monday, December 8, 2008

Using JAXB in a Spring-Maven Environment for WebServices

In a Spring application you can consume a webservice using JaxRpcPortProxyFactorybean or XFireClientFactoryBean (in conjunction with XFire). However, there is another way which can prove to be extremely useful and convenient if you are using Spring and Maven: by using Spring and Maven in conjunction with JAXB. As usual, I will focus on using JAXB and not about JAXB. To learn more about JAXB see:


In this article we will cover
1. Setting up JAXB in Spring environment to generate stubs for the webservice
2. Using it in an external project or the same project.

As per the default maven build structure, you need to make a package called "wsdl" under src/main and place a copy of wsdl of the webservice you wish to use. Then configure your pom file with the right dependencies:



Two things that you should notice in the pom besides JAXB dependencies are the build plugins maven-source-plugin and maven-jaxb2-plugin. They will generate stubs for your webservice in the "target" directory under "generated-sources" when you compile the project/ generate resources using maven. After that your webservice should be ready to used. Almost :D.

The generated source will contain stubs that correspond to exposed functions, responses and the various objects used. The name of the stubs will correspond to name in wsdl. In the same project you can use it by importing stubs from the package specified in com.saveenkumar.myApp.someWebService.types For an external project, you just need to import the project as a dependency.

Let us now see how to use these stubs. You will be able to use the webservice in your Spring app with the help of org.springframework.ws.client.core.WebServiceTemplate. You can see the bean config that you can use to inject it here:


Note that the "contextPaths" property of the JAXB marshaller is same as the  property above. If you want to dig still deeper, have a look about using WebServiceTemplate  at:


and



It may be a good idea to hide the WebServiceTemplate behind a nice interface to abstract away the user from the generated JAXB classes.

The generated classes will always have a class called "ObjectFactory". There will also be objects corresponding to every operation, every response and every object used in these transactions. Let us say our webservice exposes the following operations:

List getAllStockInfo();
List getStocksByVolatilityRange(float low, float high);
StockInfo getStockInfo(String ticker);

May generate following objects:

ObjectFactory
ArrayOfStockInfo
StockInfo
GetAllStockInfo
GetAllStockInfoResponse
GetStockInfo
GetStockInfoResponse
GetStocksByVolatilityRange
GetStocksByVolatilityRangeResponse

Let us say we want to use GetStockInfo. The code would look something like:



I would also recommend using SoapUI to test your webservices. 

Tuesday, December 2, 2008

@Resource and @Autowired

In September I wrote an article on using @Autowired for testing RMI services in Spring. Recently when trying to test a webservice consumer, I ran into trouble with @Autowired. Instead using @Resource (javax.annotation.Resource in Java 1.6) seemed to work fine. Digging I found this article in Spring forum:


which pointed to this in spring reference:

Seems the two are pretty similar. Using 
@Qualifier (org.springframework.beans.factory.annotation.Qualifier) with @Autowired solved the problem and in the end I could use either.