簡體   English   中英

@Transactional Spring MyBatis 不工作

[英]@Transactional Spring MyBatis not working

我有一個 spring webapp,一切正常,但現在我需要一種方法來進行事務處理,

這是我的 applicationContext.xml

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

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

    <!-- Bean para Nombre de Cliente -->

    <!-- 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>

    <beans:bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <beans:property name="basename" value="classpath:mensajes" />
        <beans:property name="defaultEncoding" value="UTF-8" />
    </beans:bean>

    <beans:bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <beans:property name="defaultLocale" value="es" />
        <beans:property name="cookieName" value="myAppLocaleCookie"></beans:property>
        <beans:property name="cookieMaxAge" value="3600"></beans:property>
    </beans:bean>


    <interceptors>
        <beans:bean id="localeChangeInterceptor"
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <beans:property name="paramName" value="locale" />
        </beans:bean>
    </interceptors>

    <context:component-scan base-package="com.web.*" />
    <context:component-scan base-package="com.*" />

</beans:beans>

這是我的database.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:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:tx="http://www.springframework.org/schema/tx"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="monitor-properties/monitor.properties" />


    <bean class="org.mybatis.spring.transaction.SpringManagedTransactionFactory"
        id="springManagedTransactionFactory">
    </bean>

 <!-- 
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
</bean>
 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="mapperLocations" value="classpath:com/*/database/*.xml" />
        <property name="dataSource" ref="dataSource" />
        <property name="transactionFactory" ref="springManagedTransactionFactory" />
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <bean id="adminSaveSettings" class="com.SaveSettings">
        <property name="sqlSession" ref="sqlSession" />
    </bean>

    <!-- ORACLE -->
 <bean id="dataSource" class="com.CustomBasicDataSource" 
        p:driverClassName="${oracle.driverClassName}" p:url="${oracle.url}" p:username="${oracle.username}" 
        p:password="${oracle.password}" />

在一個服務類中,我有一個自動裝配的屬性,這個屬性有一個像這樣的事務方法:我進行更新以將一行更改為“2”值,然后拋出 RuntimeException,如果一切正常,則更新必須回滾。

public class SaveSettings {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    private SqlSession sqlSession;


    public SqlSession getSqlSession() {
        return sqlSession;
    }


    public void setSqlSession(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }


    @Transactional(readOnly=false,rollbackFor=Exception.class)
    public int saveNewSettings(WebServer settings) {
        AdminPanelMapper qmap = sqlSession.getMapper(AdminPanelMapper.class);
        int inserted = 0;
        qmap.updateTo2();

        throw new NullPointerException();
    }
}

我從 2 天前開始嘗試在 google 和 stackoverflow 中找到的許多可能的解決方案,但它從來沒有運行良好。 在 database.xml 中,transactionManager 是注釋,因為我正在嘗試使用我發現的其他示例。

如果你想了解更多關於我的問題的信息,我會給你更多的細節,問我。 對不起,如果我沒有很好地解釋。

謝謝大家!

編輯:

如果我為 transactionManager bean 編寫並退出注釋,則會出現錯誤。

ERROR: org.springframework.web.context.ContextLoader `- Context initialization failed org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 9 in XML document from ServletContext resource [/WEB-INF/spring/database.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 65; cvc-complex-type.2.4.c: El comodín coincidente es estricto, pero no se ha encontrado ninguna declaración para el elemento 'tx:annotation-driven'.`

我在你的 xml 的交易配置中看不到。 你是不是忘記放那里了:

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

請注意,這與 spring-mvc 不同。

我遇到了同樣的問題。 我通過在類定義中添加@Transactional注釋來解決它。

@Transactional
public class SaveSettings {
    ...
    @Transactional(readOnly=false,rollbackFor=Exception.class)
    public int saveNewSettings(WebServer settings) {
        ...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM