为何spring声明式事务无作用?

Sou2012 2009-01-04 02:31:18
为何spring声明式事务无作用?


//为何log中没有spring 动态代理的信息,有事务回滚信息?数据库中还是更新了test表中的字段


@Repository
public class BaseHibernateDao extends HibernateDaoSupport {
}


SERVICE

@Service
public class TestService extends BaseService {
@Autowired
TestDao testDao;

public void testTT(){
testDao.testTT();
}

}


DAO


@Service
public class TestDAO extends BaseHibernateDao{


public void testTT(){
String hql = " update Test set age= ? where id = 1000 ";
Query q = this.getSession(true).createQuery(hql);
q.setInteger(0, 3333333);
q.executeUpdate();


Test n = new Test();
n.setAddtime( new Date() );
n.age( new Long(101) );
n.setDatastr("22222222222");
n.setPid( Long.parseLong("sssssssssssss") );
//此处故意throw RuntimeException子类

session.save(n);
//为何log中没有spring 动态代理的信息,有事务回滚信息?数据库中还是更新了test表中的字段
}

}


...全文
484 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sou2012 2009-01-08
  • 打赏
  • 举报
回复
有没有人知道,或是给个JPA注解+HIBERNATE sessionFactory+spring 2.5.6+springIOC 事务成功的例子?
Sou2012 2009-01-06
  • 打赏
  • 举报
回复
还是看不到事务的LOG
海会圣贤 2009-01-06
  • 打赏
  • 举报
回复
up
Landor2004 2009-01-05
  • 打赏
  • 举报
回复
1 在spring-hibernate.xml 中最上面加上<context:component-scan base-package="*" />
2 规范一下注释吧,把BaseHibernateDao上的注释去掉
建议用junit测试,试试吧
ffumax 2009-01-05
  • 打赏
  • 举报
回复
<!-- ========================= TABLE T_BZ_JS_Criterion_Demand_Info: CONFIGURE ========================= -->
<bean id="bzJsCriterionDemandInfoDAO" class="com.excellence.gzby.standard.dao.BzJsCriterionDemandInfoDAO" singleton="true" init-method="initialize">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="sqlMapClient">
<ref local="sqlMapClient"/>
</property>
</bean>
<bean id="bzJsCriterionDemandInfoBll" class="com.excellence.gzby.standard.bll.BzJsCriterionDemandInfoBll">
<property name="_dao">
<ref local="bzJsCriterionDemandInfoDAO"/>
</property>
</bean>
<bean id="bzJsCriterionDemandInfoBllProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="bzJsCriterionDemandInfoBll"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED,-Throwable</prop>
<prop key="deleteAll*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
guolimin1118 2009-01-05
  • 打赏
  • 举报
回复
spring-hibernate.xml
没有看到你把SessionFactory
注入
因为HibernateDaoSupport需要他才可以把事物注入上
Landor2004 2009-01-05
  • 打赏
  • 举报
回复
可以回滚?看不到回滚信息?是不是log4j配置有问题,这是我的配置
# Configure logging for testing: optionally with log file
log4j.rootLogger=DEBUG, stdout
# log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.append=false
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
Sou2012 2009-01-05
  • 打赏
  • 举报
回复
贴结中。。。
yebing0736 2009-01-05
  • 打赏
  • 举报
回复
用这个配置文件吧

<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 事务拦截器 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="create*">
PROPAGATION_REQUIRED </prop>
<prop key="delete*">
PROPAGATION_REQUIRED </prop>
<prop key="del*">
PROPAGATION_REQUIRED </prop>
<prop key="update*">
PROPAGATION_REQUIRED </prop>
<prop key="start*">
PROPAGATION_REQUIRED </prop>
<prop key="cancel*">
PROPAGATION_REQUIRED </prop>
<prop key="stop*">
PROPAGATION_REQUIRED </prop>
<prop key="save*">
PROPAGATION_REQUIRED </prop>
<prop key="add*">
PROPAGATION_REQUIRED </prop>
<prop key="update*">
PROPAGATION_REQUIRED </prop>
<prop key="remove*">
PROPAGATION_REQUIRED </prop>
<prop key="pay*">
PROPAGATION_REQUIRED </prop>
<prop key="*">PROPAGATION_REQUIRED</prop>

</props>
</property>
</bean>


希望能帮助到你.
Sou2012 2009-01-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 BlueSuperMan 的回复:]
sping配置文件: <context:component-scan base-package="com.nes.platform.service.managerimp"/>
表示事務配置在managerimp下

mgr的顯示類
@Component
public class ContractTypeManagerImp implements ContractTypeManager {

private ContractTypeDao contractTypeDao;
@Autowired
public void setContractTypeDao(ContractTypeDao contractTypeDao) {
this.contractTypeDao = contractTypeDao;
}
@T…
[/Quote]

我用 HibernateTemplate可以回滚,但是看不到事务信息
BlueSuperMan 2009-01-05
  • 打赏
  • 举报
回复
sping配置文件:<context:component-scan base-package="com.nes.platform.service.managerimp"/>
表示事務配置在managerimp下

mgr的顯示類
@Component
public class ContractTypeManagerImp implements ContractTypeManager {

private ContractTypeDao contractTypeDao;
@Autowired
public void setContractTypeDao(ContractTypeDao contractTypeDao) {
this.contractTypeDao = contractTypeDao;
}
@Transactional
public void delete(LoginUserBO loginUser, ContractTypeBO contractTypeBO)
throws ApplicationException {
ContractTypeBO bo = contractTypeDao.get(loginUser, contractTypeBO.getUid());
contractTypeDao.delete(loginUser, bo);

}
、。。。。。。。。
Sou2012 2009-01-04
  • 打赏
  • 举报
回复
没有人帮忙看看吗
Sou2012 2009-01-04
  • 打赏
  • 举报
回复
http://forum.springframework.org/showthread.php?t=65541
spring官方贴
Sou2012 2009-01-04
  • 打赏
  • 举报
回复
配置文件如下,
1.spring-hibernate.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
default-autowire="byName">

<context:property-placeholder location="classpath:proxool.properties" />

<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver">
<value>${proxool.driver}</value>
</property>
<property name="driverUrl">
<value>${proxool.url}</value>
</property>
<property name="user">
<value>${proxool.username}</value>
</property>
<property name="password">
<value>${proxool.password}</value>
</property>
<property name="alias">
<value>${proxool.alias}</value>
</property>
<property name="houseKeepingSleepTime">
<value>${proxool.houseKeepingSleepTime}</value>
</property>
<property name="prototypeCount">
<value>${proxool.prototypeCount}</value>
</property>
<property name="maximumConnectionCount">
<value>${proxool.maximumConnectionCount}</value>
</property>
<property name="minimumConnectionCount">
<value>${proxool.minimumConnectionCount}</value>
</property>
<property name="trace">
<value>${proxool.trace}</value>
</property>
<property name="verbose">
<value>${proxool.verbose}</value>
</property>
</bean>


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>com.x.domain</value>

</list>
</property>
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.x.service.*.*(..))"/>
</aop:config>

<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="load" read-only="true" propagation="REQUIRED"/>
<tx:method name="query" read-only="true" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED" isolation="READ_COMMITTED" />
<tx:method name="insert*" propagation="REQUIRED" isolation="READ_COMMITTED" />
<tx:method name="create" propagation="REQUIRED" isolation="READ_COMMITTED" />
<tx:method name="save*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
<tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
<tx:method name="remove*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
<tx:method name="test*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
</tx:attributes>
</tx:advice>

</beans>


2.hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="java:hibernate/SessionFactory">
<property name="hibernate.connection.proxool_provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>
<property name="hibernate.proxool.xml">classpath*:proxool.xml</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.default_schema">managood</property>
<property name="hibernate.max_fetch_depth">2</property>
<property name="hibernate.default_batch_fetch_size">16</property>
<property name="hibernate.order_updates">true</property>
<property name="hibernate.generate_statistics">false</property>
<property name="hibernate.use_identifer_rollback">false</property>
<property name="hibernate.use_sql_comments">false</property>

<!-- Hibernate and JDBC -->
<property name="hibernate.jdbc.fetch_size">100</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="hibernate.jdbc.batch_versioned_data">false</property>
<property name="hibernate.jdbc.use_scrollable_resultset">false</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.connection.release_mode">auto</property>
<!-- Hibernate and JDBC -->

<!-- hibernate cache -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_minimal_puts">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region_prefix">2ndCache</property>
<property name="hibernate.cache.use_structured_entries">true</property>
<!-- hibernate cache -->

<!-- hibernate transaction -->
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.transaction.auto_close_session">true</property>
<!-- hibernate transaction -->

<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.bytecode.use_reflection_optimizer">true</property>

<!-- hibernate event -->
<event type="post-commit-insert">
<listener class="com.x.service.HibernateEventService"/>
</event>
<!-- hibernate event -->
</session-factory>
</hibernate-configuration>

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧