读书人

Spring诠注事务不起作用

发布时间: 2013-09-08 15:21:21 作者: rapoo

Spring注解事务不起作用
Spring中注解配置如下:


<!-- 加载外部的配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 指定主配置文件的路径 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置数据连接池c3p0 -->
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库连接信息 -->
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 其他信息 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
</property>
</bean>


<!-- 配置声明式事务管理,采用基于注解的方式 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>


Entity

@Entity
@Table(name = "FHMS_FUNCTIONS")
@GenericGenerator(name = "id", strategy = "increment")
public class Function {
@Id


@Column(name = "func_id")
@GeneratedValue(generator = "id", strategy = GenerationType.AUTO)
private int id;
/**



spring的filter:

<!-- 配置spring的事务开启拦截器,以解决懒加载的问题 -->
<filter>
<filter-name>spring</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>spring</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


后台的日志只输出一句
Hibernate: select max(func_id) from FHMS_FUNCTIONS

[解决办法]
配置声明式事务 没有配置完整,事务的传播特性没有绑定,这个是我在网上搜来的
<!--  配置事务传播特性 -->

<tx:advice id="TestAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="save*" propagation="REQUIRED"/>

<tx:method name="del*" propagation="REQUIRED"/>

<tx:method name="update*" propagation="REQUIRED"/>

<tx:method name="add*" propagation="REQUIRED"/>

<tx:method name="find*" propagation="REQUIRED"/>

<tx:method name="get*" propagation="REQUIRED"/>

<tx:method name="apply*" propagation="REQUIRED"/>

</tx:attributes>

</tx:advice>

<!-- 配置参与事务的类 -->

<aop:config>

<aop:pointcut id="allTestServiceMethod" expression="execution(* com.test.testAda.test.model.service.*.*(..))"/>

<aop:advisor pointcut-ref="allTestServiceMethod" advice-ref="TestAdvice" />

</aop:config>

------解决方案--------------------


没有指定序列 ,我这里有一个例子:


@Id
@SequenceGenerator(name="generator",sequenceName="seq_article",allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="generator")
@Column(name="id",unique=true,nullable=false)
public Long getId() {
return id;
}

读书人网 >J2EE开发

热点推荐