Skip to content

Commit

Permalink
modify socketTimeout default value (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweicugw authored Sep 26, 2023
1 parent 260a98a commit 3872e10
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:替换成最近的日期 |
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jd/jdbc/vitess/VitessJdbcUrlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
50 changes: 39 additions & 11 deletions src/test/java/com/jd/jdbc/vitess/VitessJdbcUrlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
}
}

0 comments on commit 3872e10

Please sign in to comment.