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

在一个服务类中,我有一个 Autowiring 的属性,这个属性有一个这样的事务方法:我进行更新以将一行更改为“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 不同。


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!