Tuesday, 1 February 2011

Spring Transaction Management


Spring Framework supports:
  • Programmatic transaction management.
  • Declarative transaction management.



Programmatic configuration:
-----------------------------------
In this approach Hibernate properties are set in the confguration object through its setProperty() method:


public Configuration setProperty(String propertyName, String propertyValue)


Configuration cfg = new Configuration()
    .setProperty("hibernate.connection.url", "jdbc:hsqldb:hsql://localhost/hiberdb")
    .setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
    .setProperty("hibernate.connection.username", "sa")
    .setProperty("hibernate.connection.password", "")
    .setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");


Or you create a java.Util.Properties object and pass it to Configuration constructor.


Or public Configuration setProperties(Properties properties);
  
To add resource files or hbm files:



config.addResource("com/packtpub/springhibernate/ch04/Student.hbm.xml");


Declarative configuration:
--------------------------------
In declarative configuration you load hibernate.properties file in classpath with key=value configuration.
Content in properties file will be as following:

#hibernate configuration
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc:hsqldb:hsql://localhost/hiberdb
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.pool_size=5
hibernate.show_sql=false
hibernate.dialect= org.hibernate.dialect.HSQLDialect

When using this approach, you need to introduce mapping fles to Hibernate programmatically, like this:
config.addClass(Student.class);

Or using XML file configuration as follwoing:

The following code shows a simple XML confguration fle with  
the basic confguration entries:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
          "http://hibernate.sourceforge.net/hibernate-configuration 
          -3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!-- Hibernate Properties -->
    <property name="connection.driver_class"> org.hsqldb.jdbcDriver</property>
    <property name="connection.url">jdbc:hsqldb:hsql://localhost/hiberdb</property>
    <property name="connection.username">sa</property>
    <property name="connection.password"> </property>
    <property name="pool_size">5</property>
    <property name="show_sql">false</property>
    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
    <!-- Mapping files -->
    <mapping resource="com/packtpub/springhibernate/ch04/ 
                                      Student.hbm.xml"/>
    <mapping resource="com/packtpub/springhibernate/ch04/ 
                                      Teacher.hbm.xml"/>
    <mapping resource="com/packtpub/springhibernate/ch04/ 
                                       Course.hbm.xml"/>
  </session-factory>
</hibernate-configuration>





No comments:

Post a Comment