@Transactional Spring MyBatis无法正常工作

时间:2015-03-05 12:26:11

标签: java database spring mybatis transactional

我有一个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}" />

并且在一个服务类中我有一个autowired属性,并且这个属性有一个这样的事务方法:我做了一个更新,将一行更改为“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天前尝试了在谷歌和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'.`

2 个答案:

答案 0 :(得分:0)

我无法在您的xml交易配置中看到。 天啊,你忘了放在那里:

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

请注意,这与spring-mvc thingy不同。

答案 1 :(得分:0)

我遇到了同样的问题。然后通过在类定义中添加 @Transactional 注释来解决该问题。

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