在我的单元测试中,我自动安装了一些使用URL的DataSources
jdbc:derby:memory:mydb;create=true
Run Code Online (Sandbox Code Playgroud)
创建内存中的DB.
要删除内存中的Derby数据库,您必须连接:
jdbc:derby:memory:mydb;drop=true
Run Code Online (Sandbox Code Playgroud)
我想在每次测试后都会发生这种情况,并从一个新的数据库开始.我怎么能用Spring做到这一点?
当我db=mem在配置文件中进行开发时,我使用Play Framework附带的内存数据库.
如何使用JDBC连接到此数据库?而不是默认方式的JPA.
我在我的控制器中尝试过这种方法:
public static void addToDB() {
try {
Connection conn = DriverManager.getConnection("jdbc:h2:mem:play");
Statement stmt = conn.createStatement();
stmt.execute(sql);
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到一条错误消息,我需要提供用户名和密码:
org.h2.jdbc.JdbcSQLException: Wrong user name or password [8004-149]
Run Code Online (Sandbox Code Playgroud)
如果我访问web控制台上使用/@db的用户名sa没有密码.
现在我通过/@db界面登录并创建了一个表users.
然后我在控制器方法中连接到数据库并使用此jdbc-string:jdbc:h2:mem:play:sa然后尝试插入表users但我收到此错误消息:
org.h2.jdbc.JdbcSQLException: Table "USERS" not found; SQL statement:
Run Code Online (Sandbox Code Playgroud)
我应该如何使用JDBC连接到Play Framework中的内存H2数据库?
database-connection jdbc h2 in-memory-database playframework
我有PostgreSQL数据库.它用于单元测试.
您对内存数据库的选择有什么建议吗?我希望该数据库与PostgreSQL兼容.
我们正在考虑在我们的应用程序中使用缓存系统来进行数据查找.这些数据很少会发生变化.我发现用出来了Redis,GemFire/ Geode,VoltDB,Aerospike,Hazlecast和其他几个.我入围了Geode和Redis.两者都是key-value内存商店.
Redis(基于C语言)似乎更容易使用并支持数据类型,如列表,散列,集合,排序集等.它还有一些很好的客户端,可用Java,NodeJS,C#等.它支持多个节点(master -奴隶).
Geode(基于Java)似乎有更多的功能,因为它的存在可以追溯到很久以前.它具有存储数据的区域概念.它有Java,C#,C++等客户端.它具有制作系统的定位器和服务器.它支持分布式服务器/节点(非共享/无单点故障).
我搜索互联网,但在读/写速度,内存使用,磁盘I/O转储,扩展,安全性方面找不到这两者之间的任何比较.有没有人遇到过这样的情况?
在由Hibernate创建的数据库,它是增加外键唯一键约束id_student的oe_iv_student_lang表,因为我们要实现Serializable这将使Hibernate不让我们加入其相应的子表与同一母公司的外键多行接口.
我附上了片段以便更好地理解..
学生班:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "oe_iv_student")
public class OeIvStudent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_student")
private Integer idStudent;
}
Run Code Online (Sandbox Code Playgroud)
学生郎班:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "oe_iv_student_lang")
public class OeIvStudentLang implements Serializable {
private static final long serialVersionUID = 1L; …Run Code Online (Sandbox Code Playgroud) 我第一次使用otj-pg-embedded,并希望将其合并到我们的测试框架中。详细如下
以下是Maven依赖项:
<dependency>
<groupId>com.opentable.components</groupId>
<artifactId>otj-pg-embedded</artifactId>
<version>0.12.5</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
代码是:
EmbeddedPostgres pg = EmbeddedPostgres.start();
Connection connection = pg.getPostgresDatabase().getConnection();
Statement s = connection.createStatement();
ResultSet rs = s.executeQuery("SELECT 1");
assertEquals(1, rs.getInt(1));
Run Code Online (Sandbox Code Playgroud)
但是它失败,并在代码的第1行出现java.lang.IllegalStateException。
堆栈跟踪为:
initdb: invalid locale settings; check LANG and LC_* environment variables
java.lang.IllegalStateException: Process [/var/folders/rj/3jd5_2n16g37lv1v550g9cqw0000gp/T/embedded-pg/PG-b210101549c90a94dbbada389b65c5d2/bin/initdb, -A, trust, -U, postgres, -D, /var/folders/rj/3jd5_2n16g37lv1v550g9cqw0000gp/T/epg2729813194709143982, -E, UTF-8] failed
at com.opentable.db.postgres.embedded.EmbeddedPostgres.system(EmbeddedPostgres.java:593)
at com.opentable.db.postgres.embedded.EmbeddedPostgres.initdb(EmbeddedPostgres.java:230)
at com.opentable.db.postgres.embedded.EmbeddedPostgres.<init>(EmbeddedPostgres.java:148)
at com.opentable.db.postgres.embedded.EmbeddedPostgres$Builder.start(EmbeddedPostgres.java:580)
at com.opentable.db.postgres.embedded.EmbeddedPostgres.start(EmbeddedPostgres.java:480)
Run Code Online (Sandbox Code Playgroud)
我在这里配置缺少什么吗?任何帮助,将不胜感激。
由于在这两种情况下,数据都存储在内存中,是否可以对两者具有近似相同的插入和并发性能?如果不是,为什么?
我使用内存数据库来对使用 Entity Framework Core 的方法进行单元测试。
我按如下方式设置数据库上下文:
var builder = new DbContextOptionsBuilder<DatabaseContext>().UseInMemoryDatabase();
var context = new DatabaseContext(builder.Options);
var databaseElements = Enumerable.Range(1, 10)
.Select(i => new DatabaseElement
{
Id = i,
Name = "Name of Element"
});
context.DatabaseElements.AddRange(databaseElements);
context.SaveChanges();
_databaseContext = context;
...
Run Code Online (Sandbox Code Playgroud)
我使用这个数据库上下文进行测试。这很好用。
是否可以使用它来引发实体框架异常?
例如,如果我可以测试我的方法是否按预期处理这些异常(例如没有到数据库的连接),那就太好了。
我有一个在事务中做一些工作的方法:
public async Task<int> AddAsync(Item item)
{
int result;
using (var transaction = await _context.Database.BeginTransactionAsync())
{
_context.Add(item);
// Save the item so it has an ItemId
result = await _context.SaveChangesAsync();
// perform some actions using that new item's ItemId
_otherRepository.Execute(item.ItemId);
transaction.Commit();
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
我想添加单元测试来检查如果_context.SaveChangesAsync或_otherRepository.Execute失败则事务会回滚,这可能吗?
sqlite unit-testing transactions in-memory-database entity-framework-core
我有使用 mysql 配置了 TypeORM 的 NestJs 应用程序。我想进行 e2e(integration) 测试,因此我想在以这种方式配置的测试中使用内存数据库:
{
type: 'sqlite',
database: ':memory:',
synchronize: true,
dropSchema: true,
entities: [`dist/**/*.entity{.ts,.js}`],
}
Run Code Online (Sandbox Code Playgroud)
以及测试的设置
beforeEach(async () => {
const moduleFixture: TestingModule =
await Test.createTestingModule({imports: [AppModule, UserModule]})
.overrideProvider(TypeOrmConfigService).useClass(MockTypeOrmConfigService)
.compile();
app = await moduleFixture.createNestApplication();
await app.init();
});
Run Code Online (Sandbox Code Playgroud)
. 运行测试时我得到了
AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session.
at new AlreadyHasActiveConnectionError (/Users/user/workspace/app/src/error/AlreadyHasActiveConnectionError.ts:8:9)
at ConnectionManager.Object.<anonymous>.ConnectionManager.create (/Users/user/workspace/app/src/connection/ConnectionManager.ts:57:23)
at Object.<anonymous> (/Users/user/workspace/app/src/index.ts:228:35)
at step …Run Code Online (Sandbox Code Playgroud)