2014-04-29 92 views
0

我的項目最初開發時使用了與持久性單元和項目是spring,Struts2的hibernate集成一體。現在我需要使用jboss連接池和持久性單元。 任何人都可以讓我以最短的方式轉換項目中的那個需求。 當前spring.xmlSpring與jboss的持久化單元

<?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: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"> 


<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>properties/database.properties</value> 
    </property> 
</bean> 
<bean id="springdatasource" 

    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <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="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 

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

    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 
      <prop key="hibernate.format_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">update</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 

    <property name="annotatedClasses"> 
     <list> 

      <value>model.AtOrganisation</value> 

      <value>model.AtDivision</value> 



     </list> 
    </property> 

</bean> 



<bean id="orgdao" class="dao.OrganisationDaoImp"> 
    <property name="sessionfactory" ref="sessionFactory" /> 
</bean> 
<bean id="divdao" class="dao.DevisionDaoImpl"> 
    <property name="sessionfactory" ref="sessionFactory" /> 
</bean> 
<bean id="empAction" class="action.OraganisationAction"> 
    <property name="orgdao" ref="orgdao" /> 
</bean> 
<bean id="empAction2" class="action.DevisionAction"> 
    <property name="orgdao" ref="orgdao" /> 
</bean> 
<bean id="divAction" class="action.DevisionAction"> 
    <property name="divdao" ref="divdao" /> 
</bean> 

樣品DAO類

public class DevisionDaoImpl implements DevisionDao { 


private SessionFactory sessionfactory; 

public void setSessionfactory(SessionFactory sessionfactory) { 
    this.sessionfactory = sessionfactory; 
} 
@Override 
public List showDiv() { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public void addDiv(AtDivision div) { 
    Session session = sessionfactory.openSession(); 
    Transaction tx = null; 
    try { 
     tx = session.beginTransaction(); 
     session.saveOrUpdate(div); 
     tx.commit(); 
     //System.out.println("after save : " + org.getAoId()); 
    } catch (Exception e) { 
     if (tx != null) { 
      tx.rollback(); 
     } 
     e.printStackTrace(); 
    } finally { 
     session.close(); 
    } 
} 

}

+0

我相信,你只需要切換到JBoss的數據源,是嗎? –

+0

是的,我需要通過container.not來管理事務,而不是像我當前的代碼。 – Dilis

回答

0

1:改變你的數據源配置使用JBoss的數據源。這是可以做到這樣的:

<jee:jndi-lookup id="springdatasource" jndi-name="your-de-jndi-name" /> 

2:添加一個事務管理器使用Spring的事務管理器來控制交易:

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

3:添加註釋支持春季交易。添加到您的配置:

<tx:annotation-driven transaction-manager="transactionManager" /> 

4:最後裝飾你的DAO類使用@Transactional

+0

放哪裏? @Jilli-lookup id =「springdatasource」jndi-name =「your-de-jndi-name」/> – Dilis

+0

@Dillis將所有東西放在你的彈簧中xml –

+0

Thaks.I'll試試這個。 – Dilis