- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在玩弄纯 JDBC 和测试。我编写了简单的 DAO,它允许执行 CRUD 操作:
package pl.aszecowka;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcVehicleDao implements VehicleDao {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void insert(Vehicle vehicle) {
String sql = "insert into vehicle(vehicle_no,color, wheel, seat) values(?,?,?,?)";
Connection conn = null;
try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, vehicle.getVehicleNo());
ps.setString(2, vehicle.getColor());
ps.setInt(3, vehicle.getWheel());
ps.setInt(4, vehicle.getSeat());
ps.executeUpdate();
ps.close();
conn.commit();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
@Override
public void update(Vehicle vehicle) {
String sql = "update vehicle set color=?, wheel=?, seat=? where vehicle_no = ?";
Connection conn = null;
try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, vehicle.getColor());
ps.setString(4, vehicle.getVehicleNo());
ps.setInt(2, vehicle.getWheel());
ps.setInt(3, vehicle.getSeat());
ps.executeUpdate();
ps.close();
conn.commit();
} catch (SQLException ex) {
throw new RuntimeException(ex);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
}
}
}
}
@Override
public void delete(Vehicle vehicle) {
String sql = "delete from vehicle where vehicle_no = ? ";
Connection conn = null;
try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, vehicle.getVehicleNo());
int i = ps.executeUpdate();
System.out.println(i);
ps.close();
conn.commit();
} catch (SQLException ex) {
throw new RuntimeException(ex);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
}
}
}
}
@Override
public Vehicle findByVehicleNo(String vehicleNo) {
String sql = "select * from vehicle where vehicle_no = ?";
Connection conn = null;
try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, vehicleNo);
Vehicle vehicle = null;
ResultSet rs = ps.executeQuery();
if (rs.next()) {
vehicle = new Vehicle(rs.getString("vehicle_no"), rs.getString("color"), rs.getInt("wheel"), rs.getInt("seat"));
}
rs.close();
ps.close();
return vehicle;
} catch (SQLException ex) {
throw new RuntimeException(ex);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
}
}
}
}
}
这是我的 Spring 配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="url" value="jdbc:derby://localhost:1527/vehicle;create=true"/>
<property name="username" value="app"/>
<property name="password" value="app"/>
<property name="initialSize" value="2"/>
<property name="maxActive" value="5"/>
<property name="defaultAutoCommit" value="false" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="vehicleDao" class="pl.aszecowka.JdbcVehicleDao">
<property name="dataSource" ref="dataSource"/>
</bean>
还有我的测试:
@ContextConfiguration("/beans.xml")
public class JdbcVehicleDaoTest extends AbstractTransactionalJUnit4SpringContextTests {
@Resource
private VehicleDao vehicleDao;
@Test
public void testCRUD()
{
String vehicleNo = "ABCDEF";
String color = "blue";
int wheel = 4;
int seat = 4;
Vehicle vehicle = new Vehicle(vehicleNo, color, wheel, seat);
vehicleDao.insert(vehicle);
Vehicle fromDB = vehicleDao.findByVehicleNo(vehicleNo);
Assert.assertNotNull(fromDB);
Assert.assertEquals(vehicleNo, fromDB.getVehicleNo());
Assert.assertEquals(color, fromDB.getColor());
Assert.assertEquals(wheel, fromDB.getWheel());
Assert.assertEquals(seat, fromDB.getSeat());
color = "blue";
seat = 5;
wheel = 12;
fromDB.setColor(color);
fromDB.setSeat(seat);
fromDB.setWheel(wheel);
vehicleDao.update(fromDB);
fromDB = vehicleDao.findByVehicleNo(fromDB.getVehicleNo());
Assert.assertNotNull(fromDB);
Assert.assertEquals(vehicleNo, fromDB.getVehicleNo());
Assert.assertEquals(color, fromDB.getColor());
Assert.assertEquals(wheel, fromDB.getWheel());
Assert.assertEquals(seat, fromDB.getSeat());
vehicleDao.delete(fromDB);
fromDB = vehicleDao.findByVehicleNo(fromDB.getVehicleNo());
Assert.assertNull(fromDB);
}
@Test
public void testCheckIfTestRollbackWorks()
{
Vehicle vehicle = new Vehicle("ABCDEF", "blue", 4, 4);
vehicleDao.insert(vehicle);
}
}
在我的测试类中,我想避免任何自定义“清理”方法,这些方法会恢复测试期间所做的所有更改,因此我扩展了 AbstractTransactionalJUnit4SpringContextTests。这个类用@Transactional注解,据我所知这意味着所有测试方法的事务最后都会回滚。
一开始回滚不起作用,例如,如果 testCheckIfTestRollbackWorks() 作为第一个运行,第二个是 testCRUD,testCRUD 失败是因为抛出 SQLIntegrityConstraintViolationException: The statement was aborted because it would have cause a duplicate “VEHICLE”上定义的“SQL131026082556940”标识的唯一或主键约束或唯一索引中的键值。
经过一些研究,我发现 autoCommit 可能导致此类行为的信息,然后我将 defaultAutoCommit 属性设置为 false。
但是,testCRUD 不起作用,因为尽管我调用vehicleDao.insert(车辆);我在使用时找不到这个对象:vehicleDao.findByVehicleNo(vehicleNo);
我认为原因很简单:我的 DAO 没有执行任何提交。所以我添加了插入、更新和删除方法“conn.commit()”。
但是现在,一个测试的更改在另一个测试中再次可见:(我假设测试回滚它自己的交易,但不是这个在 DAO 方法中提交的交易。
有什么提示可以解决这个问题吗?
最佳答案
This class is annotated with @Transactional, and as far as I know it means that transactions for all test methods will rolled back at the end.
是的,但是这仅适用于使用单个和/或 spring 管理事务的代码。您的代码没有,您正在自己建立连接并对该连接进行提交。所以数据已经提交,提交的数据不能回滚。除此之外,事务对 spring 不可见(或在 springs 控制下),因此 spring 对此无能为力。
如果您希望 Spring 影响/驱动您的事务,请重写您的代码以使用 JdbcTemplate
或使用 TransactionAwareDataSourceProxy
。更多信息可以在 Spring Reference Guide 中找到.
链接:
关于java - 使用 AbstractTransactionalJUnit4SpringContextTests : changes in DB not reverted after test method 测试纯 JDBC DAO 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19611204/
我当前正在存储给定产品的上传图像,如下所示: class Product(db.Model): images= db.ListProperty(db.Blob) # More prop
每次对架构或新迁移文件进行更改时,我都会运行以下命令: rake db:drop db:create db:migrate db:seed 是否有预先构建的等效方法来执行此操作? 我从我读到的内容中想
在 android 中使用房间作为数据库。当我试图在 sqlviewer 中查看数据时,在数据库文件中找不到表Myapp.db 文件为空。数据/data/packageName/databases/M
我搜索并尝试了很多次,但没有找到我的答案。我有一些用小 cucumber (在 Rails 项目中)编写的项目的功能文件。所有步骤都已定义,如果我单独启动它们,功能本身运行得很好。我可以将所有场景与我
您必须承认,对于 Rails 和数据库的新手来说,rubyonrails.org 上的官方解释使所有这四个任务听起来完全一样。引用: rake db:test:clone Recreate the
当我尝试运行时: heroku run rake db:drop db:create db:migrate 我得到错误: Running rake db:drop attached to termin
rake db:migrate 和 rake db:reset 之间的区别对我来说非常清楚。我不明白的是 rake db:schema:load 与前两者有何不同。 只是为了确保我在同一页面上: ra
我们都知道,我们可以使用 Azure 函数(使用 out 参数或使用 return)在 cosmos DB 中一次保存一个文档,例如: object outputDocument = new { i
我有一个包含 60 多个表的 mysql 数据库。这是在我将 joomla 版本 2.5.3 从本地灯移植到网络服务器时构建的。 我运行 mysql-db: 移植后我发现我无法登录 amdin 区域。
我想轻松地将现有数据库迁移到 Azure 托管。在我的项目中,我使用 Entity Framework DB First。有什么经验教训或例子可以说明如何做到这一点吗? 最佳答案 您本地使用什么数据库
所以,我一直在使用 MagicalRecord 开发 iPad 应用程序,最近在转移到自动迁移商店后我遇到了一些问题。我需要将我的 .db 文件从一个设备同步到另一个设备,所以我需要所有数据都在 .d
自从我在 Heroku 上部署并希望与生产相匹配后,我最近切换到 postgres 来开发一个 Rails 应用程序。当我将数据库名称设置为“postgres”时,我的应用程序安装了 Postgres
我使用 oledb 提供程序(SQLOLEDB 和 SQL Native OLEDB 提供程序)创建了一个示例应用程序。 案例 1:提供者 = SQLOLEDB hr = ::CoInitialize
我正在为 NodeJs 使用 mongodb 驱动程序,其中有 3 个方法: 1) db.collection.insert 2) 数据库.collection.insertOne 3) db.col
我是 datomic 的新手,我仍在努力弄清楚系统是如何构建的。特别是,我不明白 :db.part/db 扮演什么角色,因为每次安装架构时似乎都需要它。有人可以解释一下这一切意味着什么吗? (需要 '
Berkeley DB 是否有空间索引,例如 R-tree? 最佳答案 有人问the same question on the Oracle forum .还没有甲骨文回答。但答案是否定的,它没有任何
请解释一下这是什么意思 $db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE); 它给了我一个错误 "E
berkeley-db-je 的最新版本是什么? 来自 oracle , 为 7.5。 但来自maven存储库,它是 18.3.12。 有没有人知道更多的细节? 最佳答案 Berkeley DB Ja
我不明白查询构建器的替换和更新之间的区别。尤其是替换文档... This method executes a REPLACE statement, which is basically the SQL
看起来 BerkeleyDB 被 Oracle 收购了,它没有在其网站上发布源代码? 最佳答案 Sleepycat 于 2006 年被 Oracle 收购。该产品继续在原始开源许可下可用,并继续得到增
我是一名优秀的程序员,十分优秀!