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 .`