English 中文(简体)
@ 交易 Spring MyBatis没有工作
原标题:@Transactional Spring MyBatis not working

I have a spring webapp and everything was ok, but now i need one method to be transactional,

this is my 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>

这是我的数据库。

<?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}" />

and in one service class i have an autowired property, and this property has a transactional method like this: I make an update to change one row to "2" value, and after I throw a RuntimeException, and if everything is good, the update must be rollback.

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();
    }
}

I was since 2 days ago trying so much possible solutions i have found in google and stackoverflow, but it never runs good. In database.xml the transactionManager is comment because i was trying with other examples I found.

如果你想获得更多有关我问题的信息,我将给你更多的细节,请我发言。 如果我没有很好地解释,则会问。

Thanks for all!

EDIT:

And here is the error if i write and quit comments for 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 .`
问题回答

I can t see in your xml s configuration of transactions themselves. Haven t you forgot to put there:

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

很显然,这不同于春天。

I have met the same question. And I resolved it by adding a @Transactional annotation to the class definition.

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签