2017-10-20 57 views
3

我的彈簧工具套裝中的項目有問題。 @Transactional註釋不起作用。@Transactional不能在我的彈簧工具套裝中工作

這裏是我的項目結構: -

web.xml中: -

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/root-context.xml</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

的servlet-context.xml中: -

<!-- Enables the Spring MVC @Controller programming model --> 
<annotation-driven /> 

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
<resources mapping="/resources/**" location="/resources/" /> 

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 

<context:component-scan base-package="com.project.dateandcrud" /> 

根上下文。 xml: -

<!-- Root Context: defines shared resources visible to all other web components --> 
<context:property-placeholder location="classpath:datasource-cfg.properties" /> 

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="${ds.driverClassName}" /> 
    <property name="url" value="${ds.url}" /> 
    <property name="username" value="${ds.username}" /> 
    <property name="password" value="${ds.password}" /> 
</bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan" value="com.project.dateandcrud.entity" /> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">validate</prop> 
     </props> 
    </property> 
</bean> 

<!-- Transaction Manager to make Transaction Support--> 
<tx:annotation-driven transaction-manager="transactionManager"/> 

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

我已經使用了DAOIMPL如下: - 當我不使用@Transactional註解我的項目工作正常

@Repository 
public class ProfileDaoImpl implements ProfileDao { 

@Autowired 
private SessionFactory sessionFactory; 

private Session session; 

@Override 
@Transactional 
public void addProfile(Profile p) { 

    session = sessionFactory.openSession(); 
//  session.beginTransaction(); 
    session.save(p); 
//  session.getTransaction().commit(); 
    session.close(); 

} 

。但是當使用註釋時它不起作用。

在我的servlet-context.xml或root-context.xml文件中是否有任何問題。

我曾嘗試 <context:component-scan base-package="com.project.dateandcrud" />這從selvlet-context.xml的遷入根的context.xml但也不能工作。

請建議我可能的錯誤。

+0

這是什麼意思它不工作?是否有任何異常彈出? –

+0

@MaciejKowalski沒有錯誤彈出窗口,只是它沒有插入數據到數據庫... – Space

回答

2

不要試圖打開/關閉休眠自己session,讓spring-tx處理這個:

@Transactional 
public void addProfile(Profile p) { 
    final Session session = sessionFactory.getCurrentSession(); 
    session.save(p); 
} 

而且擺脫servlet-context.xml<annotation-driven/>到您的root-context.xml配置。

這應該可以解決您的問題。

+0

但當我這樣做時,錯誤彈出窗口: - 無法獲得當前線程的事務同步會話 – Space

+0

@Space是的,你是對的。我只是仔細看看你的配置並更新了我的答案。 –

+0

最終經過一些調整,它的工作...請查看我的答案...謝謝Bodhdan – Space

0

這裏是解決方案,我已經找到....只是從根context.xml的移動<tx:annotation-driven transaction-manager="transactionManager"/>的servlet-context.xml的 ...