Posts

Showing posts from September, 2013

Providing Connections for Hibernate

Hibernate, being an Object->Relational DB Mapper for Java, needs access to JDBC connections. Out of the box, Hibernate is fairly self contained when it comes to connection control. By default, Hibernate ships with the ability to obtain a data source implementation (   javax.sql.DataSource   ) from JNDI by setting the properties appropriately. You can find different kind of  Hibernate connection pool libraries   here          hibernate.connection.datasource = java:/comp/env/jdbc/test Alternatively, if JNDI isn't an option, you can use a Hibernate-internal connection pool implementation (C3PO), and simply give driver/url information for Hibernate to create and pool its own connections: hibernate.connection.driver_class = org.postgresql.Driver hibernate.connection.url = jdbc: postgresql://localhost/test hibernate.connection.username = root hibernate.connection.password = password12 hibernate.c3p0.min_size=5 hi...

Hibernate unit of work

 Unit of work First, let's define a unit of work. A unit of work is a design pattern described by Martin Fowler as “[maintaining] a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems. ”[ PoEAA ] In other words, its a series of operations we wish to carry out against the database together. Basically, it is a transaction, though fulfilling a unit of work will often span multiple physical database transactions. So really we are talking about a more abstract notion of a transaction. The term "business transaction" is also sometimes used in lieu of unit of work. do not open and close a Session for every simple database call in a single thread. The same is true for database transactions. Database calls in an application are made using a planned sequence; they are grouped into atomic units of work. This also means that auto-commit after every single SQL statement is useless in a...

Improving Hibernate's Performance

Connection Pooling:-         Opening a connection to a database is generally much more expensive than executing an SQL statement. A connection pool is used to minimize the number of connections opened between application and database. Much like a library, your application code needs to be strict about returning connections to the pool when complete, for if it does not do so, your application will run out of connections.This problem can be avoided by  always  using a  finally  block to close your connection, as shown throughout this book. Starving a Pool:-          Hibernate supports a variety of connection pooling mechanisms. If you are using an application server, you may wish to use the built-in pool (typically a connection is obtaining using JNDI). If you can't or don't wish to use your application server's built-in connection pool, Hibernate supports several other connection pools c3p0 http...