spring中可以使用声明式事务和编程式事务,主要用的比较多的是声明式事务,下面举例子小结一下
首先在config.xml中定义好datasource和platformtranscationmanager
<!-- 定义DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
..........
</bean>
<!-- 定义TransactionManager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 定义BookDao -->
<bean id="bookDaoTarget" class="example.chapter6.BookDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 封装为事务的BookDao -->
<bean id="bookDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="bookDaoTarget" />
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<!-- 对以query开头的方法要求只读事务 -->
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<!-- 对于其他方法要求事务 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
则使用的代码例子为
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
BookDao bookDao = (BookDao)context.getBean("bookDao");
.....//之后的就是
try
{
}catch {}结构,程序中不需要再有处理事务方面的代码了。
另外,在JAVA 5中,可以用注释来更简单的配置,但要注意的是,必须使用spring 2.0才行
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
>
。。。。。。其他的和上面的相同,最后加上
<!-- 将所有具有@Transactional注解的Bean自动配置为声明式事务支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
然后,对接口使用注释
import org.springframework.transaction.annotation.Transactional;
@Transactional
public interface BookDao {
@Transactional(readOnly=true)
Book query(String id);
@Transactional(readOnly=true)
List<Book> queryAll();
void insert(Book book);
void update(Book book);
void delete(Book book);
}
要注意的是,@transcational的注释只能应用到public方法上
2 集成hiberante事务
比如用hibernatedaosupport支持DAO,使用spring 2.0的注解,比如
bookdao.java
import org.springframework.transaction.annotation.Transactional;
@Transactional
public interface BookDao {
@Transactional(readOnly=true)
Book query(String id);
@Transactional(readOnly=true)
List<Book> queryAll();
void insert(Book book);
void update(Book book);
void delete(Book book);
}
实体配置代码如下
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity(name="Book")
public class Book {
private String id;
private String name;
private String author;
@Id
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
}
然后其配置代码如下
<!-- 定义SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="annotatedClasses">
<list>
<value>example.chapter6.Book</value>
</list>
</property>
<property name="annotatedPackages">
<list>
<value>example.chapter6</value>
</list>
</property>
</bean>
<!-- 定义TransactionManager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义BookDao -->
<bean id="bookDao" class="example.chapter6.BookDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 将所有具有@Transactional注解的Bean自动配置为声明式事务支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>