Syn*_*sso 13 testing spring transactions jdbc rollback
我在尝试使用Spring-test时没有成功地获得JDBC事务回滚.当我运行以下内容时,始终会提交SQL更新.
package my.dao.impl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
@ContextConfiguration(locations={"classpath:ApplicationContext-test-DAOs.xml"})
@TransactionConfiguration(defaultRollback = true)
public class ConfirmationMatchingDAOImplTest {
@Autowired
private DataSource dataSource;
@Test
public void shouldInsertSomething() throws Exception {
final Connection connection = dataSource.getConnection();
final Statement statement = connection.createStatement();
statement.executeUpdate("insert into TEST_INSERT values (1, 'hello')");
statement.close();
connection.close();
}
}
Run Code Online (Sandbox Code Playgroud)
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://makeitfunky:1490;databaseName=fonzie"/>
<property name="username" value="ralph"/>
<property name="password" value="p0n1es_R_kew1"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
另外,我使用了太多的注释吗?我可以让它更干净一点吗?
Chi*_*ang 17
如果您没有使用@TestExecutionListeners注释显式配置测试执行侦听器,则默认情况下DependencyInjectionTestExecutionListener,Spring配置DirtiesContextTestExecutionListener,和TransactionalTestExecutionListener. TransactionalTestExecutionListener使用默认回滚语义提供事务测试执行.通过显式声明@TestExecutionListeners测试类并省略TransactionalTestExecutionListener侦听器列表,您将禁用事务支持.
您还必须@Transactional在类或方法级别添加注释.
您还必须使用DataSourceUtils来获取由DataSourceTransactionManager管理的事务连接.
| 归档时间: |
|
| 查看次数: |
26289 次 |
| 最近记录: |