While using C3P0 as your connection pool (either with Spring or otherwise), sometimes you might get an exception like this:
** BEGIN NESTED EXCEPTION **
com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **
java.io.EOFException

STACKTRACE:
java.io.EOFException at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1956)
This is quite a common problem which shows up when C3P0 is configured incorrectly or by using the default settings. The problem occurs when C3P0 is trying to use a connection already established by the pool but was closed by the database server. When C3P0 tries to use this already-closed connection, the error is thrown.
In c3p0.properties, try experimenting with the following values:
c3p0.preferredTestQuery=SELECT 1 
c3p0.testConnectionOnCheckout=true
This will force C3P0 to test each connection before passing it to the application.
If you are configuring using spring/hibernate, do note that *only* these properties are configurable in the applicationContext.xml file:
hibernate.c3p0.acquire_increment
hibernate.c3p0.idle_test_period
hibernate.c3p0.timeout
hibernate.c3p0.max_size
hibernate.c3p0.max_statements
hibernate.c3p0.min_size
hibernate.c3p0.validate
The other properties should go to the c3p0.properties file located in the webapp class root folder.

If you get an exception such as:
java.sql.SQLException: com.mchange.v2.resourcepool.ResourcePoolException: Attempted to use a closed or broken resource pool 
Ensure that the timeout value configured for c3p0 does not not exceed the database server's wait timeout.
The MasterCardCDF3Lib is a library to assist with developing with MasterCard's Smart Data CDF XML Files in Java.

I've included source codes, Castor XML Marshaller/Unmarshaller and Ant build as well as a pre-built jar as well.

To use:
// Unmarshall XML to JavaBean
CDF3Util cdf3Util = new CDF3Util ();
CDFTransmissionFile file = cdf3Util.unmarshall (inputXml);

// Marshall JavaBean to XML
String outputXml = cdf3Util.marshall (file);
Download: [mastercard-cdf3-lib.zip @ ~4.5MB]
Eclipse is a pretty powerful IDE. It has been in use in Java, Ruby on Rails, Python and even C++. It can, however, be quite the RAM hog. This does not mean that Eclipse needs to be slow, however. There are ways make your development life with Eclipse just that bit more bearable at the cost of some minor conveniences.

Here's a couple of tips on how to squeeze that extra speed out of Eclipse. (Do note that for some of these tips you might need to restart Eclipse in order to effect the changes.)

  • Close unused projects
    Close those projects which you don't need to work on. If I have projects which I don't need to work on more than a few days, I'd close them, keeping opened projects to the basic minimum. Fewer open projects mean fewer files Eclipse need to manage.

  • Disable folding
    Disable Automatic Folding in Windows > Preferences > Java > Editor > Folding. Untick the 'Enable Folding' check box.

  • Disable Content Assist
    Those cool widgets which pop up and assist your coding can be real nice, but they can slow Eclipse down by magnitudes. To disable Content Assist, go to Windows > Preferences > Java > Editor > Content Assist > Untick the "Enable Auto Activation" check box.

    I know this tip will disable the uber cool Content Assist by not popping up automatically while you type or dot a method invokation, but you can still access this manually by hitting CTRL+SPACE whenever you need help. Try it! Before you know it, it'll be second nature.

  • Remove/undeploy unused contexts and applications.
    This is not really an Eclipse tip per-se but often when developing Web apps, you'll be running a servlet container or application server (Apache Tomcat, BEA Weblogic, IBM Websphere etc) at the same time. Remove unused contexts and applications from your app server. Sample and pre-installed applications would be guilty of this. Less contexts/applications used, less RAM used by your server. You can always redeploy anything you need again. Simple, no?

  • Change Heap memory used by Eclipse.
    By default, Eclipse gets allocated a pitiful amount of memory by the Java VM. The result is the ever-frustrating OutOfMemoryException. You can change this amount by modifying certain configuration parameters when launching Eclipse.

    On Windows:
    If you're launching Eclipse from command line, include the -Xms and -Xmx parameters with values larger than 64MB for Xms and 128MB for Xmx.
    e.g: -Xms128m -Xmx256m

    On Mac OSX:
    Edit the /Eclipse.app/Contents/Info.plist file and modify the Xms and Xmx settings.

    Sometimes, increasing the above setting to huge amounts would still yeild errors such as PermGen errors. This PermGen error comes when the VM runs out of PermSpace, which is an area where the VM stores java class definitions and structures of data. To combat this, you can add the switch -XX:MaxPermSize=128m.

    Having said this, sometimes, with framework such as Spring with Hibernate, you can still get errors. I've had success switching Java Runtimes Environments instead. BEA JRockit seem to work well where Sun's JRE fail with PermGen in these instances.

    Do take care to be aware of your computer's limitations. What memory you allocate to Eclipse would mean less memory for your computer overall. This could mean making things worse instead. I'd recommend running Eclipse on no less than 1GB of RAM if you intend to tweak Eclipse's memory settings. YMMV.

  • MyEclipse Tip: Disable validators
    MyEclipse is a full-featured IDE based on Eclipse and has a powerful runtime validators to validate everything from JSP files to DTD to even JavaScript. These comes with a cost of performance however.

    Disable by:
    Windows > Preferences > MyEclipse > Validation;
    Tick on the 'Suspend all validators' check box.

This is a pretty awesome video tip from Joe Walker of DWR fame showing how to upload files using DWR.Next.

We had a requirement to add mobile support to one of our projects. The requirement was to allow managers to access approval data via Java-enabled devices such as Blackberrys. We decided to, however go with the safe route of supporting MIDP 2 midlets instead of targetting specifically the BBs.

The midlet would be required to communicate to a JEE server via Web Services and should support WS security (We used XFire and ACEGI). For communicating, we settled on the KSOAP library, which worked pretty okay..but we hit a roadblock when we needed cookie support as well to KSOAP.

After a frustrating and fruitless Googling for a solution (mailing lists were not much help since KSOAP seems to be rather dead these days) , I decided to d/l the whole KSOAP libs and dependancies and got my hands dirty on the CVS copy of KSOAP and attempted adding cookie support.

So here's a modified version of the libs with cookie support for anyone who is facing the same problem. I also added a way to allow you to customize your SOAP headers so as to be able to send web services security headers etc. Just run the ANT build and deploy the .jars to your midp project. Cookies work by being stored into the device's local RecordStore.

ksoap.zip [5.4 MB]
So I was testing out a huge portion of code involving DWR and hibernate and I encountered massive slowdowns. Log files generated massive amounts of LazyInitializationException errors. Turned out that DWR attempted to convert hibernate objects, invoking all the lazy-loaded child objects along the way. The solution? Specify the converter in WEB-INF/dwr.xml as "hibernate".

<convert converter="hibernate" match="com.mymodels.mybean" />

This will tell DWR to skip all lazy-loaded children for hibernate. Of course you'll have to explicitly join children you need but its way easier to manage, IMO.
I had an instance once where we were supporting a GDS's XML format for communicating hotel and airlines data. To make parsing the extensive amount of XML, we used castor to marshal and unmarshal the XML to our Java models. Castor, as an XML marshalling library, is great! But sometimes there are instances where you want more control over your XML marshalling and unmarshalling.

The format of the XML (which will remain unnamed) was in such a way where the order of the elements mattered. The XML format felt like it was rushed by the vendor but there was nothing we could do to change that. Fortunately castor supports marshaling listeners.

For example, to unmarshal, castor has the UnmarshalListener interface which you can implement and apply whenever you wish to have more control over the marshaling process or keep track whenever marshaling takes place.

Four methods need to be implemented using this interface.
	public void initialized(Object arg0) 
	public void attributesProcessed(Object arg0) 
	public void fieldAdded(String arg0, Object parent, Object child) 
	public void unmarshalled(Object object) 

Like I said above, I had a problem where items had to be dealt with sequentially.

For example:
A item which holds description of a hotel room might follow up immediately with a element which holds the elements. To complicate it further, some might not come with a etc.

So the solution was to use the UnmarshalListener to keep track whenever a Rate or Description item is being processed and then, in our Parent object, keep track of the index of the XML being processed and manually insert the descriptions/rates.

Example:
public void fieldAdded(String arg0, Object parent, Object child) 
{
	if (arg0.equals("XMLHotelItem"))
	{
		GwsHotelAvailabilityResponse hotelAvailability = (GwsHotelAvailabilityResponse) parent;
		hotelAvailability.incrementErrorIndex();
			
		GwsHotelInsideShopRate insideShopRate = new GwsHotelInsideShopRate ();
		insideShopRate.setErrorIndex(hotelAvailability.getCurrentErrorIndex());
			
		hotelAvailability.insertHotelInsideShopRate (insideShopRate);
	}
		
	// Hotel Description Handling
	if (arg0.equals("hotelRates"))
		{			
			GwsHotelCompleteAvailabilityResponse parentResponse = (GwsHotelCompleteAvailabilityResponse) parent;
			
			parentResponse.incrementIndex();			
		} else 
		if (arg0.equals("descriptions"))
		{
			GwsHotelCompleteAvailabilityResponse parentResponse = (GwsHotelCompleteAvailabilityResponse) parent;
			parentResponse.insertDescriptionString((String) child);			
		}
	}

	public void unmarshalled(Object object) 
	{
		if (object instanceof GwsHotelCompleteAvailabilityResponse)
		{			
			GwsHotelCompleteAvailabilityResponse response = (GwsHotelCompleteAvailabilityResponse) object;
			
			// Set room master responses
			if (response.getHotelTypeIndicator().equals("R"))
			{
				Iterator it = response.getHotelRates().iterator();
				
				while (it.hasNext())
				{
					GwsHotelRate rate = (GwsHotelRate) it.next();
					RoomMasterCodeTranslator trans = new RoomMasterCodeTranslator (rate.getBicCode());
					
					rate.insertRoomTypeDescription(trans.getRoomQuality() + " room.");
					rate.insertRoomTypeDescription(trans.getNumberOfBedsAsString() + " bed(s).");
					rate.insertRoomTypeDescription(trans.getBedType() + " bed(s).");					
				}
			}
		}
	}
}

To make use of the UnmarshalListener:
Mapping unmapping = new Mapping ();
unmapping.loadMapping(getClass().getResource(PATH_TO_CASTOR_MAPPING_FILE));		
Unmarshaller un = new Unmarshaller (XmlResponse.class);

un.setUnmarshalListener(new CustomXmlUnmarshalListener());
un.setIgnoreExtraElements(true);
un.setMapping(unmapping);
		
StringReader sr = new StringReader (xml);
return (XmlResponse) un.unmarshal(sr);

Page: