Migrating from iBatis 2.x to 3.0
March 27, 2010 Leave a comment
I recently migrated a project from iBatis 2.x to 3.0 and thought I would share some notes on the differences between the two:
- You will need to download asm-3.2 from http://asm.ow2.org/.
- The iBatis xml configuration file format has been changed a little, the handling of different environments is much improved, allowing you to easily switch data sources in the xml file.
- The mapper xml file format has been changed a lot, the naming has been cleaned up, and the dynamic SQL generation is a lot cleaner. One gotcha is that you need to set the jdbcType of columns which allow NULL values (this was not the case before, but is not a bit deal.)
- SqlClientMap has been done away with and replaced by a SqlSession which has a much cleaner API and a much better life-cycle. Note that you can set a SqlSession to autocommit or you can call .commit(), and it is always a great idea to .close() a SqlSession.
- On thing that did bite me is that SqlSession cache results so a second SELECT will return the same results even though the data was changed by another process (not sure if this applies to INSERT and UPDATE though,) so you should use .clearCache() if you want the results to come from the database as opposed to the cache.
A few other notes:
- With iBatis 2.0 I would create a bunch of SqlClientMap and cache them in a hash so that I would not need to create new ones for each session. This helped performance a lot but was not good practice. You cannot do that with SqlSession, so what I did was to create and cache my SqlSessionFactory objects and create new a SqlSession from them when I needed. The reason I needed to do this is that the project in question has about 45 databases. SqlSessionFactory objects can be shared across threads.
- I use C3P0 as my data sources and a little work was needed to make it work properly under iBatis 3.0. What I did was to create a new combo pooled data source for each driver/url/user name combination and cache those in a hash returning them as needed by iBatis.
- iBatis 3.0 is much, much faster than 2.0 when it comes to SELECTs, this is a very welcome change, however INSERTs/UPDATEs/DELETEs are a little slower but not enough to have any impact.
- Finally the beans used by iBatis are “enhanced by CGLIB”, so the result of ‘o.getClass()’ will return ‘O$$EnhancerByCGLIB$$2632338b’ which makes a clause like ‘getClass() != o.getClass()’ in the .equals() method quite useless and you need to use ‘!(o instanceofO)’ instead, otherwise objects which are the same will not be reported as such. Of course this assumes that you have an .equals() method in your beans.
You will also want to read the iBatis 3.0 documentation.