Struts2+hibernate+spring事务配置没起作用

struts.xml


<action name="index" class="indexAction">
<result name="success">/top5w.jsp</result>
</action>


applicationContext.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>

<property name="url"><value>jdbc:mysql://localhost:3306/te</value></property>

<property name="username"><value>root</value></property>

<property name="password"><value>root</value></property>

<property name="maxActive"><value>100</value></property>

<property name="maxIdle"><value>20</value></property>

<property name="maxWait"><value>100</value></property>

</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

<prop key="hibernate.show_sql">true</prop>

</props>

</property>

<property name="mappingLocations">

<list>

<value>classpath:com/zhang/tao/model/*.hbm.xml</value>

</list>

</property>

</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<bean id="indexAction" class="com.zhang.struts.action.IndexAction" scope="prototype">

<property name="wordService" ref="kwordService" />

</bean>

<bean id="kwordService" class="com.zhang.struts.service.KwordService">

<property name="sessionFactory" ref="sessionFactory" />

</bean>



KwordService.java

private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {   
    return sessionFactory;   
}   

public void setSessionFactory(SessionFactory sessionFactory) {   
    this.sessionFactory = sessionFactory;   
}   

public List&lt;?&gt; getResult(String hql,int currPage){   

// Session session = sessionFactory.openSession();

Session session = sessionFactory.getCurrentSession();

Transaction tr = session.beginTransaction();

Query query = session.createQuery(hql);

query.setFirstResult(50*currPage);

query.setMaxResults(50);

List<?> list = query.list();

tr.commit();

// session.close();

return list;

}



IndexAction.java

private KwordService wordService;

public String toIndex(){

HttpServletRequest request = ServletActionContext.getRequest();

List<?> list = null;

String hql = "from com.zhang.tao.model.Kword";

list = wordService.getResult(hql,1);



如果在KwordService.java里用sessionFactory.openSession()就OK,但用getCurrentSession()就报错
如果在kwordservice类里使用session = sessionFactory.openSession();就OK,如果使用getCurrentSession()就报错
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

你都没有配置事件拦截器嘛

配置声明式事务的方法如下。主要利用BeanNameAutoProxyCreator自动创建事务代理

  <bean id="transactionInterceptor"
  class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
  <!-- 配置事务属性 -->
  <property name="transactionAttributes">
   <props>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>

 <bean
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
   <list>
    <value>fundService</value>
   </list>
  </property>
  <property name="interceptorNames">
   <list>
    <value>transactionInterceptor</value>
   </list>
  </property>
 </bean>

参考更详细的配置
http://hi.baidu.com/minordragon/blog/item/5c928f7e0666f23d0dd7da0a.html

恕我无知....请告知哪里配置事务了?
是不是应该配置一下aop或者声明式事务呢

getCurrentSession()中的session不用你关,你commit的时候会自己close





这是是个事务管理接口, 你还要配置TransactionProxy和transactionAttribute

Spring结合Hibernate声明式事务配置 :
[url=http://www.phome.asia/forum/thread/18065.html]www.phome.asia/forum/thread/18065.html[/url]



tx:attributes




/tx:attributes
/tx:advice


/aop:config

struts2配置事务 引用HibernateTransactionManager配置 超级经典配置