From 3872e105842a6770fe530d1ad277ee9c06b3a7ab Mon Sep 17 00:00:00 2001 From: wangweicugw <38103831+wangweicugw@users.noreply.github.com> Date: Tue, 26 Sep 2023 14:42:00 +0800 Subject: [PATCH] modify socketTimeout default value (#139) --- docs/properties.md | 2 +- .../jd/jdbc/vitess/VitessJdbcUrlParser.java | 2 +- .../jdbc/vitess/VitessJdbcUrlParserTest.java | 50 +++++++++++++++---- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/docs/properties.md b/docs/properties.md index ea2b0c7..4c50d70 100644 --- a/docs/properties.md +++ b/docs/properties.md @@ -21,7 +21,7 @@ | password | String | | 连接时使用的密码。 | | characterEncoding | String | utf8 | 是指定所处理字符的解码和编码的格式,或者说是标准。若项目的字符集和MySQL数据库字符集设置为同一字符集则url可以不加此参数。 | | serverTimezone | String | | 设置时区 | -| socketTimeout | int | 10000 | 查询超时时间,最小值不得小于1000,小于1000时默认设置为1000 | +| socketTimeout | int | 10000 | 查询超时时间 | | allowMultiQueries| boolean| true| 在一条语句中,允许使用“;”来分隔多条查询。不可更改,VtDriver强制设置为true| | maxAllowedPacket | byte | 65535(64k) | 设置server接受的数据包的大小 | | zeroDateTimeBehavior | String | exception | JAVA连接MySQL数据库,在操作值为0的timestamp类型时不能正确的处理,而是默认抛出一个异常。参数,exception:默认值;convertToNull:将日期转换成NULL值;round:替换成最近的日期 | diff --git a/src/main/java/com/jd/jdbc/vitess/VitessJdbcUrlParser.java b/src/main/java/com/jd/jdbc/vitess/VitessJdbcUrlParser.java index 2b5202b..8efe5f9 100755 --- a/src/main/java/com/jd/jdbc/vitess/VitessJdbcUrlParser.java +++ b/src/main/java/com/jd/jdbc/vitess/VitessJdbcUrlParser.java @@ -77,7 +77,7 @@ public static Properties parse(String url, Properties info) throws SQLException Integer socketTimeout = null; if (parsedProperties.containsKey(VitessPropertyKey.SOCKET_TIMEOUT.getKeyName())) { socketTimeout = Utils.getInteger(parsedProperties, VitessPropertyKey.SOCKET_TIMEOUT.getKeyName()); - if (socketTimeout != null && socketTimeout < 1000) { + if (socketTimeout != null && socketTimeout < 0) { socketTimeout = 1000; } } diff --git a/src/test/java/com/jd/jdbc/vitess/VitessJdbcUrlParserTest.java b/src/test/java/com/jd/jdbc/vitess/VitessJdbcUrlParserTest.java index a3338d0..17935fb 100644 --- a/src/test/java/com/jd/jdbc/vitess/VitessJdbcUrlParserTest.java +++ b/src/test/java/com/jd/jdbc/vitess/VitessJdbcUrlParserTest.java @@ -18,36 +18,38 @@ import com.jd.jdbc.discovery.TopologyWatcherManager; import com.jd.jdbc.sqlparser.utils.StringUtils; +import static com.jd.jdbc.vitess.VitessJdbcUrlParser.JDBC_VITESS_PREFIX; import java.net.URI; import java.net.URISyntaxException; -import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; +import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import testsuite.TestSuite; -import testsuite.internal.environment.TestSuiteEnv; - -import static com.jd.jdbc.vitess.VitessJdbcUrlParser.JDBC_VITESS_PREFIX; import static testsuite.internal.TestSuiteShardSpec.TWO_SHARDS; +import testsuite.internal.environment.TestSuiteEnv; public class VitessJdbcUrlParserTest extends TestSuite { - TestSuiteEnv env = Driver.of(TWO_SHARDS); + private TestSuiteEnv env = Driver.of(TWO_SHARDS); - String schema = getKeyspace(env); + private String schema = getKeyspace(env); - String role = "rw"; + private String role = "rw"; - String user = getUser(env); + private String user = getUser(env); - String password = getPassword(env); + private String password = getPassword(env); + + + private Integer socketTimeout; @Rule public ExpectedException thrown = ExpectedException.none(); - private Connection conn; + private VitessConnection conn; private void init() throws SQLException { TopologyWatcherManager.INSTANCE.resetScheduledExecutor(); @@ -87,7 +89,11 @@ private void init() throws SQLException { connecturlionUrl += "&role=" + role; } - this.conn = DriverManager.getConnection(connecturlionUrl); + if (socketTimeout != null) { + connecturlionUrl += "&socketTimeout=" + socketTimeout; + } + this.conn = (VitessConnection) DriverManager.getConnection(connecturlionUrl); + try (Statement stmt = this.conn.createStatement()) { stmt.executeUpdate("delete from plan_test"); } @@ -124,4 +130,26 @@ public void testRoleParam() throws SQLException { thrown.expectMessage("'role=" + role + "' " + "error in jdbc url"); init(); } + + @Test + public void testSocketTimeout() throws SQLException { + socketTimeout = -1; + init(); + Integer getSocketTimeout = Integer.valueOf(conn.getProperties().getProperty("socketTimeout")); + if (!getSocketTimeout.equals(1000)) { + Assert.fail("testSocketTimeout is fail"); + } + socketTimeout = 0; + init(); + getSocketTimeout = Integer.valueOf(conn.getProperties().getProperty("socketTimeout")); + if (!getSocketTimeout.equals(socketTimeout)) { + Assert.fail("testSocketTimeout is fail"); + } + socketTimeout = 500; + init(); + getSocketTimeout = Integer.valueOf(conn.getProperties().getProperty("socketTimeout")); + if (!getSocketTimeout.equals(socketTimeout)) { + Assert.fail("testSocketTimeout is fail"); + } + } }