-
Notifications
You must be signed in to change notification settings - Fork 427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Executor service instead of thread #162
Changes from 6 commits
d5f10d5
72d596b
2979464
585c0b5
4e46838
ff2d811
ba54360
1780179
a9d41cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,12 +13,16 @@ | |
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.UUID; | ||
|
||
import javax.sql.DataSource; | ||
import javax.sql.PooledConnection; | ||
|
||
import org.apache.commons.dbcp2.BasicDataSource; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
|
@@ -30,6 +34,8 @@ | |
import com.microsoft.sqlserver.testframework.DBConnection; | ||
import com.microsoft.sqlserver.testframework.DBTable; | ||
import com.microsoft.sqlserver.testframework.util.RandomUtil; | ||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
public class PoolingTest extends AbstractTest { | ||
|
@@ -139,4 +145,52 @@ public void testConnectionPoolClientConnectionId() throws SQLException { | |
|
||
assertEquals(Id1, Id2, "ClientConnection Ids from pool are not the same."); | ||
} | ||
|
||
@Test | ||
public void testHikariCP() throws SQLException { | ||
HikariConfig config = new HikariConfig(); | ||
config.setJdbcUrl(connectionString); | ||
HikariDataSource ds = new HikariDataSource(config); | ||
|
||
connect(ds); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example for thread-counts (untested):
|
||
@Test | ||
public void testApacheDBCP() throws SQLException { | ||
BasicDataSource ds = new BasicDataSource(); | ||
ds.setUrl(connectionString); | ||
|
||
connect(ds); | ||
} | ||
|
||
private static void connect(DataSource ds) throws SQLException { | ||
Connection con = null; | ||
PreparedStatement pst = null; | ||
ResultSet rs = null; | ||
|
||
try { | ||
con = ds.getConnection(); | ||
con.isValid(5); | ||
|
||
pst = con.prepareStatement("SELECT SUSER_SNAME()"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For testing timeout: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hello @fwi , could you please inform me what the benefits are by testing setQueryTimeout() instead of isValid()? from my understanding, both create a new thread for the timeout timer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently "isValid" ends up using the timeout timer, but this is an implementation choice (other JDBC drivers do it differently for speed and this JDBC driver might one day too) and might change in the future. Also "isValid" is already called by the connection pool before returing a connection from the pool (see for example "validationTimeout" in HikariCP). Besides that, the original issue was about "queries with a time-out", so why not test that explicitly and make sure to use a query with a time-out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that makes sense, I will change it. |
||
rs = pst.executeQuery(); | ||
|
||
while (rs.next()) { | ||
rs.getString(1); | ||
} | ||
} | ||
finally { | ||
if (rs != null) { | ||
rs.close(); | ||
} | ||
|
||
if (pst != null) { | ||
pst.close(); | ||
} | ||
|
||
if (con != null) { | ||
con.close(); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing performance with 'trivial queries', so add a loop:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fwi : Thanks for suggestions. We are trying out
Java MicroBenchmark Harness (JMH)
test cases for performance testing. We are figuring out how & when we can open performance test cases. At the same time we want our Travis-CI & Appveyour builds should not be long running.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad choice of words on my part: this is actually a functional test. Remember when we changed a little bit of code to prevent creating lots of threads when many queries are executed rapidly? How do you know there is not another mistake like that still in the code? This test simply measures the amount of threads before and after executing many queries rapidly. Measuring is knowing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a side note: the datasource backed by the pool implementation does need to be closed when the test is finished. If not, connections remain open and the pool implementation itself starts threads as well that are shutdown when the datasource is closed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Final note: verify
ttEndCount
is at least 1 to ensure the test is working (ifttEndCount
is zero, the test did not do its intended job).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of doing
if (ttEndCount - ttStartCount > 2) { throw exception}
, I guess only verifyttEndCount
is at least 1 is sufficient. In the for loop, if we increase the termination (e.g. to 100000), it may create more than 2 timeout threads.