Tuesday, November 18, 2008

Enabling C3P0 connection pooling provider in Hibernate 3.3.1.GA (Using Maven 2.0.9)

Hibernate documentation claims that turning on the c3p0 connection pooling provider requires only setting the property hibernate.c3p0.max_size in hibernate configuration file.
(Note: I'm using the hibernate.properties configuration, not the hibernate.cfg.xml, however this shouldn't make much of a difference)

After I set required hibernate.c3p0.* properties in my hibernate.properties file, and by reviewing the hibernate log in my IDE's console, I found this conspicious line:
INFO DriverManagerConnectionProvider:64 - Using Hibernate built-in connection pool (not for production use!)
which means that hibernate is still not using c3p0.

After searching online, I found a hint that c3p0 can be turned on by setting the property hibernate.connection.provider_class to the value org.hibernate.connection.C3P0ConnectionProvider in my hibernate configuration file  (or actually by uncommenting it in the hibernate distribution's default hibernate.properties file).
Then the conspicios log line happily switches to:
INFO ConnectionProviderFactory:95 - Initializing connection provider: org.hibernate.connection.C3P0ConnectionProvider

I had to add org.hibernate:hibernate-c3p0 (version 3.3.1.GA) to my list of maven dependencies to avoid having the following exception thrown:
org.hibernate.HibernateException: Could not instantiate connection provider: org.hibernate.connection.C3P0ConnectionProvider
Hint: hibernate-c3p0 is a C3P0-based implementation of the Hibernate ConnectionProvder contract (as defined by http://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0)
hibernate-c3p0 automatically calls c3p0:c3p0 (version 0.9.1) as a transitive dependency.

Versions of Interest:
Maven 2.0.9

Technologies of Interest (Tags):
Java

Friday, June 20, 2008

Reading "Design Patterns: Elements of Reusable Object-Oriented Software"


I previously read lots of design patterns books, each and every one of these books mentions "Design Patterns: Elements of Reusable Object-Oriented Software" as a primary -if not the first- source of information that inspired the writer while writing his book.

I'll refer to the book as the GoF book as it's universally known, which means "Gang of Four", referring to the four writers of the book (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides).

Whenever I come across a sentence referring to the GoF book, I used to think "well, this book seems very inspiring, however, it was written in 1994, 14 years ago -wow- that's like the stone age for an industry in which every thing gets outdated in like 5 minutes.

I expected to find outdated information and theoretical examples that don't relate a bit to the reality of our new solutions, I'm reading in design patterns as a sort of refereshment of information before I start designing and coding a new -one of a kind- online application in ActionScript and Flex, and I don't need a group of stone age nerds to tell me how to do it".

I was wrong.

I won't start praising the book and how far it inspired me and made me rediscover aspects of Object Oriented Design. I didn't know that such aspects could be powerfully utilized and put to action, namely the concept of "polymorphism". Just take it for granted: This book is awesome.

However, to start with the book, and especially if you're new to the patterns discussed -or design patterns in general- you'll face some difficulty reading and advancing through the chapters without trouble. The writers were trying to describe different aspects of the patterns and give early examples about differences and similarities, contrasting between different patterns' structures and applicability. There are lots of cross references between the patterns throughout the book, in locations where the cross-referenced patterns were not yet explained or discussed.

I personally felt this was very frustrating, since I had to deffer understanding these discussions and comparisons till I read about these referred-to patterns later in next chapters. These discussions and comparisons were mixed up with the book's text, not in their own sections, so I had to pass though that frustrating feeling every time I stepped through these discussions.

The writers put it this way:
A word of warning and encouragement: Don't worry if you don't understand this book completely on the first reading. We didn't understand it all on the first writing! Remember that this isn't a book to read once and put on a shelf.
Well, quite honest, but let's face it, who has the time to read a 350 pages book more than once consecutively? moreover, who doesn't worry if he doesn't understand many aspects of the book on the first round. Definitely not me!!!

After finishing the second chapter, I had a couple of red smoking ears, and an inner feeling that I'm a complete idiot!. I needed some really fast and short crash course about all patterns discussed in the book before I could be able to go through another page!, I needed to know what the hell they are talking about the next time they refer to some pattern that will be discussed later, hundreds of pages away.

My dearest Wikipedia came to the rescue. I printed all Wikipedia pages discussing the design patterns mentioned in the GoF book. Reading them went so smoothly and fast, there were tons of examples in many languages (C++, Java, C#, PHP, Actionscript, etc...).

The Wikipedia pages also included many citations from the Gof book. It seemed like the book was being injected into my brain just like Nero was being taught Kung-fu in The Matrix.

After reading the Wikipedia pages, I continued my trip through the GoF book, and it felt totally different this time, feeling the change in my perception of Object Oriented Design while being re-sculptured again.

Links: