Thanks for hint, but could you be more specific?! What is wrong with my approach, that I cannot get rollback?
public void storeOwnerPlain(Owner owner) throws SQLException {
String[] sql = new String[2];
Connection con = null;
try {
// con = getDataSource().getConnection();
con = DataSourceUtils.getConnection(getDataSource());
TransactionStatus status = TransactionInterceptor.currentTransactionStatus();
sql[0] = "insert into owners " +
"(first_name, last_name, address, city, telephone)" +
"values ('Ang', 'Lee', '1st Street', 'Boston', '10000')";
sql[1] = "insert into owners " +
"(first_name, last_name, address, city, telephone)" +
"values ('Ang', 'lee', 'Taiwan')";
Statement stat = con.createStatement();
for (int i = 0; i < 2; i++) {
stat.executeUpdate(sql[i]);
}
} catch (SQLException e) {
logger.info(".. sql.exception: " + e);
throw e;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for Petclinic on JDBC.
-->
<beans>
<!-- ========================= GENERAL DEFINITIONS ========================= -->
<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
<property name="location"><value>/WEB-INF/jdbc.properties</value></property>
</bean>
<!-- Message source for this context, loaded from localized "messages_xx" files -->
<bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
<property name="basename"><value>messages</value></property>
</bean>
<!-- ========================= RESOURCE DEFINITIONS ========================= -->
<!-- Local DataSource that works in any environment -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
<property name="driverClassName"><value>${jdbc.driverClassNa me}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
<bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.Transac tionAwareDataSourceProxy">
<property name="targetDataSource"><ref local="dataSource"/></property>
</bean>
<!-- JNDI DataSource for J2EE environments -->
<!--
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
<property name="jndiName"><value>java:comp/env/jdbc/petclinic</value></property>
</bean>
-->
<!-- Transaction manager for a single JDBC DataSource (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<property name="dataSource"><ref local="dataSourceProxy"/></property>
</bean>
<!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
<!--
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTran sactionManager"/>
-->
<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
<!-- Petclinic primary business object: HSQL JDBC implementation -->
<!-- (only initialized when referenced by the clinic proxy) -->
<bean id="clinic" class="org.springframework.samples.petclinic.jdbc. HsqlJdbcClinic" lazy-init="true">
<property name="dataSource"><ref local="dataSourceProxy"/></property>
</bean>
<!-- Petclinic primary business object: MySQL JDBC implementation -->
<!-- (only initialized when referenced by the clinic proxy) -->
<bean id="mysqlClinic" class="org.springframework.samples.petclinic.jdbc. MySQLJdbcClinic" lazy-init="true">
<property name="dataSource"><ref local="dataSource"/></property>
</bean>
<!-- Transaction Interceptor set up to do PROPAGATION_REQUIRED on all methods -->
<bean id="matchAllWithPropReq"
class="org.springframework.transaction.interceptor .MatchAlwaysTransactionAttributeSource">
<property name="transactionAttribute"><value>PROPAGATION_REQ UIRED</value></property>
</bean>
<bean id="matchAllTxInterceptor"
class="org.springframework.transaction.interceptor .TransactionInterceptor">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="transactionAttributeSource"><ref bean="matchAllWithPropReq"/></property>
</bean>
<!-- One BeanNameAutoProxyCreator handles all beans where we want all methods to use
PROPAGATION_REQUIRED -->
<bean id="autoProxyCreator"
class="org.springframework.aop.framework.autoproxy .BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<idref local="matchAllTxInterceptor"/>
</list>
</property>
<property name="beanNames">
<list>
<idref local="clinic"/>
</list>
</property>
</bean>
</beans>
error message on web page:
Internal error
java.sql.SQLException: Column count does not match in statement [insert into owners (first_name, last_name, address, city, telephone)values ('Ang', 'lee', 'Taiwan')] at org.hsqldb.jdbc.jdbcUtil.sqlException(Unknown Source) at org.hsqldb.jdbc.jdbcStatement.fetchResult(Unknown Source) at org.hsqldb.jdbc.jdbcStatement.executeUpdate(Unknow n Source) at org.springframework.samples.petclinic.jdbc.HsqlJdb cClinic.storeOwnerPlain(HsqlJdbcClinic.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at org.springframework.aop.framework.AopProxyUtils.in vokeJoinpointUsingReflection(AopProxyUtils.java:61 ) at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:149) at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :116) at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:56) at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :138) at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:152) at $Proxy69.storeOwnerPlain(Unknown Source) at org.springframework.samples.petclinic.web.AddOwner Form.onSubmit(AddOwnerForm.java:28) at


Reply With Quote