在Spring 3.0.1官方文件裡指出有三種指定Transaction Manager的方式,分別是jdbc、jta和以Hibernate為例的orm。jdbc需要指定DataSource甚至它的Connection Pool,如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> |
JTA則是透過JNDI機制找到Ap Server的transaction,不必指定dataSource,藉由jee:jndi-lookup去Ap Server找。如下:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/jpetstore"/> <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" /> |
至於ORM的設定,以Hibernate為例設定如:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value> org/springframework/samples/petclinic/hibernate/petclinic.hbm.xml </value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=${hibernate.dialect} </value> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> |
倘若ORM欲使用AP Server的Transaction Manager,可以單純使用和上例JTA相同的用法:
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" /> |
留言列表