简体   繁体   English

是否应该保持JDBC连接?

[英]Should I persist JDBC connection or not?

For my integration tests I have build common validation class which has methods like 对于我的集成测试,我建立了通用的验证类,该类具有以下方法

public void countcheck(table1, table 2){ // makes a JDBC connection and run the query to check counts and then closes the connection };

public void nullcheck(Table, ColumnName) {// makes a JDBC connection and the run query to make sure there are no Nulls and then closes the connection );

and so on. 等等。 I distribute these common validation methods as Jars for integration testers. 我将这些常见的验证方法作为Jars分发给集成测试人员。 My dilemma is whether I should open and close connection for each method , I don't want testers to worry about opening and closing connections and just worry about calling validation methods. 我的难题是我是否应该为每种方法打开和关闭连接,我不想让测试人员担心打开和关闭连接,而不必担心调用验证方法。 I am looking for alternative design or this is something good enough. 我正在寻找替代设计,或者这已经足够好了。 My worry is if there in a test suite there are 10 tests then there would be 10 connections going and closing which perhaps is not a good sign? 我担心的是,如果一个测试套件中有10个测试,那么将有10个连接进行和关闭,这可能不是一个好兆头吗? I want testers to worry about makig and closing connections. 我希望测试人员担心makig和关闭连接。 Its a Junit based test framework and DB is Oracle. 它是基于Junit的测试框架,而数据库是Oracle。

You should not "bake in" the decision on handling DB connections into your code. 您不应该“接受”将数据库连接处理到代码中的决定。 Instead, you should defer this decision to people deploying your framework. 相反,您应该将此决定推迟到部署框架的人员那里。

If they decide to reuse connections, they could configure connection pool for that; 如果他们决定重用连接,则可以为此配置连接池 if they decide to open and close connections every time, they could configure the framework without connection pooling. 如果他们决定每次都打开和关闭连接,则可以配置框架而无需连接池。

Your task is reduced to providing the enabling technology to them. 您的任务减少到为他们提供支持技术。

I typically pass a DataSource into such Objects (constructors) and get the connection on each business Method. 我通常将DataSource传递到此类对象(构造函数)中,并获取每个业务方法上的连接。 In standalone mode this might mean it is not using a proper pool, but when you onlyexecute a handfull of statements thats no problm. 在独立模式下,这可能意味着它没有使用适当的缓冲池,但是当您仅执行少数语句时,就没有问题了。

In production you can then pass in a real pool. 在生产中,您可以传递一个真实的池。 The advantage of keeping a data source around as opposed to a Connection is, that this scenario has better self-healing capabilities and reducesthe number of active connections. 与连接相比,保持数据源的优势在于,此方案具有更好的自我修复功能并减少了活动连接的数量。 (It might be not so important in test scenarios, but whynot have generally useable validators). (它在测试场景中可能不是那么重要,但是为什么没有通常可用的验证器)。

Validator(DataSource ds) {
    this.ds = ds;
}
validate(String val, ...) {
    try (Connection c = ds.getConnection()) {
        ...
    }
}

Most drivers have a native DataSource, but you can also have a genericone which implements getConnection() to return DriverManager.getConnection(url); 大多数驱动程序都有本机DataSource,但是您也可以有一个通用类,该类实现getConnection()以return DriverManager.getConnection(url); . It is also a small interface easily mockable. 这也是一个易于模拟的小界面。

If your tests get bigger, you use a simple pooling datasource like the one from Apache Commons DBCP DriverAdapterCPDS . 如果您的测试变得更大,则可以使用简单的合并数据源,例如来自Apache Commons DBCP DriverAdapterCPDS数据源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM