To enable transactions while accessing Hibernate objects, you need to provide
an instance of the Hibernate transaction manager, of
HibernateTransactionManager type, as described here. You can
then use the transacted() DSL command to create a transaction scope
in a route.
Example 2.3 shows how to
instantiate a Hibernate transaction manager, of
HibernateTransactionManager type, which is required if you want
to integrate Hibernate object-oriented persistence with Spring transactions. The
Hibernate transaction manager requires a reference to a Hibernate session
factory, and the Hibernate session factory takes a reference to a JDBC data
source.
Example 2.3. Hibernate Transaction Manager Configuration
<beans ... >
...
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>product.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>
<bean id="hibernateTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
</beans>In Example 2.3, the
hibernateTxManager bean is a local Hibernate transaction
manager instance, of HibernateTransactionManager type. There is
just one property you need to provide to the Hibernate transaction manager: a
reference to a Hibernate session factory.
In Example 2.3, the
mySessionFactory bean is a Hibernate session factory of
org.springframework.orm.hibernate3.LocalSessionFactory type.
This session factory bean is needed by the Hibernate transaction manager.
In general, you need to supply the following properties to a Hibernate
LocalSessionFactory bean instance:
- dataSource
An instance of
javax.sql.DataSource, which is the JDBC data source of the database that Hibernate is layered over. For details of how to configure a JDBC data source, see JDBC Data Source.- mappingResources
Specifies a list of one or more mapping association files on the class path. A Hibernate mapping association defines how Java objects map to database tables.
- hibernateProperties
Allows you to set any Hibernate property, by supplying a list of property settings. The most commonly needed property is
hibernate.dialect, which indicates to Hibernate what sort of database it is layered over, enabling Hibernate to optimize its interaction with the underlying database. The dialect is specified as a class name, which can have one of the following values:org.hibernate.dialect.Cache71Dialectorg.hibernate.dialect.DataDirectOracle9Dialectorg.hibernate.dialect.DB2390Dialectorg.hibernate.dialect.DB2400Dialectorg.hibernate.dialect.DB2Dialectorg.hibernate.dialect.DerbyDialectorg.hibernate.dialect.FirebirdDialectorg.hibernate.dialect.FrontBaseDialectorg.hibernate.dialect.H2Dialectorg.hibernate.dialect.HSQLDialectorg.hibernate.dialect.IngresDialectorg.hibernate.dialect.InterbaseDialectorg.hibernate.dialect.JDataStoreDialectorg.hibernate.dialect.MckoiDialectorg.hibernate.dialect.MimerSQLDialectorg.hibernate.dialect.MySQL5Dialectorg.hibernate.dialect.MySQL5InnoDBDialectorg.hibernate.dialect.MySQLDialectorg.hibernate.dialect.MySQLInnoDBDialectorg.hibernate.dialect.MySQLMyISAMDialectorg.hibernate.dialect.Oracle9Dialectorg.hibernate.dialect.OracleDialectorg.hibernate.dialect.PointbaseDialectorg.hibernate.dialect.PostgreSQLDialectorg.hibernate.dialect.ProgressDialectorg.hibernate.dialect.RDMSOS2200Dialectorg.hibernate.dialect.SAPDBDialectorg.hibernate.dialect.SQLServerDialectorg.hibernate.dialect.Sybase11Dialectorg.hibernate.dialect.SybaseAnywhereDialectorg.hibernate.dialect.SybaseDialectorg.hibernate.dialect.TimesTenDialect








