Skip to content

Commit

Permalink
fix setIdentityBack problem
Browse files Browse the repository at this point in the history
  • Loading branch information
c7ch23en committed Mar 16, 2020
1 parent b64ad10 commit 88c2d69
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public static void setGeneratedKeyBack(DaoTask<?> task, DalHints hints, List<?>
if (keyHolder == null || rawPojos == null || rawPojos.isEmpty())
return;

if (!(hints.is(DalHintEnum.setIdentityBack) && hints.isIdentityInsertDisabled()))
if (!hints.is(DalHintEnum.setIdentityBack))
return;

EntityManager em = EntityManager.getEntityManager(rawPojos.get(0).getClass());
Expand All @@ -329,7 +329,14 @@ public static void setGeneratedKeyBack(DaoTask<?> task, DalHints hints, List<?>

for (int i = 0; i < rawPojos.size(); i++)
if (!keyHolder.isEmptyKey(i)) {
setPrimaryKey(pkFlield, rawPojos.get(i), keyHolder.getKey(i));
Object rawPojo = rawPojos.get(i);
try {
if (!hints.isIdentityInsertDisabled() && pkFlield.get(rawPojo) != null)
continue;
} catch (Throwable t) {
// ignore
}
setPrimaryKey(pkFlield, rawPojo, keyHolder.getKey(i));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

Expand Down Expand Up @@ -267,8 +268,11 @@ public static String setToOrderedString(Set<String> origin) {
return EMPTY_SET;

Set<String> treeSet = new TreeSet<>();
treeSet.addAll(origin);
for (String item : origin) {
if (item != null)
treeSet.add(item);
}

return StringUtils.join(treeSet.toArray(), ",");
return StringUtils.join(treeSet, ",");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ private void prepareRequestContext() {
if (tables != null && tables.size() == 1) {
RequestContext ctx = new DalRequestContext().setLogicTableName(tables.get(0).getTableName());
hints.setRequestContext(ctx);
} else if (hints.getSpecifiedTableName() != null) {
RequestContext ctx = new DalRequestContext().setLogicTableName(hints.getSpecifiedTableName());
hints.setRequestContext(ctx);
}
} else if (hints.getSpecifiedTableName() != null) {
RequestContext ctx = new DalRequestContext().setLogicTableName(hints.getSpecifiedTableName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ public class MockDataSourceConfigureProvider implements IDataSourceConfigureProv

@Override
public IDataSourceConfigure getDataSourceConfigure() {
// connectionString="Server=10.32.20.139;port=3306;UID=root;password=!QAZ@WSX1qaz2wsx;database=llj_test;"
String connectionUrl = "jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8";
// connectionString="Server=10.32.20.143;port=3306;UID=root;password=!QAZ@WSX1qaz2wsx;database=llj_test;"
String connectionUrl = "jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8";
String userName = "root";
String password = "!QAZ@WSX1qaz2wsx";
String driverClass = "com.mysql.jdbc.Driver";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import static org.junit.Assert.*;

public class ForceSwitchableDataSourceTest {
private final static String IPHOST = "10.32.20.139";
private final static String IPHOST = "10.32.20.143";
private final static String DOMAINHOST = "dst56614";
private final static String INVALIDHOST = "1.1.1.1";

Expand Down Expand Up @@ -175,7 +175,7 @@ public void testConfigChangedAfterForceSwitch() throws Exception {
Properties newProperties = new Properties();
newProperties.setProperty(USER_NAME, "root");
newProperties.setProperty(PASSWORD, "!QAZ@WSX1qaz2wsx");
newProperties.setProperty(CONNECTION_URL, "jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
newProperties.setProperty(CONNECTION_URL, "jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
newProperties.setProperty(DRIVER_CLASS_NAME, "com.mysql.jdbc.Driver");

DataSourceConfigure newConfigure = new DataSourceConfigure("DalService2DB_w", newProperties);
Expand All @@ -202,7 +202,7 @@ public void testToString() throws Exception {
IDataSourceConfigureProvider provider = new MockDataSourceConfigureProvider();
ForceSwitchableDataSource dataSource = new ForceSwitchableDataSource(provider);
SwitchableDataSourceStatus status = dataSource.getStatus();
assertEquals("isForceSwitched: false, poolCreated: true, hostName: 10.32.20.139, port: 3306", status.toString());
assertEquals("isForceSwitched: false, poolCreated: true, hostName: 10.32.20.143, port: 3306", status.toString());
}

@Test
Expand All @@ -224,7 +224,7 @@ public void testProviderThrowException() throws Exception {
Properties properties = new Properties();
properties.setProperty(USER_NAME, "root");
properties.setProperty(PASSWORD, "!QAZ@WSX1qaz2wsx");
properties.setProperty(CONNECTION_URL, "jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
properties.setProperty(CONNECTION_URL, "jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
properties.setProperty(DRIVER_CLASS_NAME, "com.mysql.jdbc.Driver");
DataSourceConfigure dataSourceConfigure = new DataSourceConfigure("DalService2DB_w", properties);
dataSource.forceSwitch(SerializableDataSourceConfig.valueOf(dataSourceConfigure), DOMAINHOST, 3306);
Expand All @@ -238,7 +238,7 @@ public void testProviderThrowException() throws Exception {
assertEquals("3306", status1.getPort().toString());
assertEquals("root", dataSource.getSingleDataSource().getDataSourceConfigure().getUserName());
assertEquals("!QAZ@WSX1qaz2wsx", dataSource.getSingleDataSource().getDataSourceConfigure().getPassword());
assertEquals("jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;", dataSource.getSingleDataSource().getName());
assertEquals("jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;", dataSource.getSingleDataSource().getName());

dataSource.forceSwitch(SerializableDataSourceConfig.valueOf(dataSourceConfigure), INVALIDHOST, 3306);
Thread.sleep(20000);
Expand All @@ -265,7 +265,7 @@ public void testProviderThrowException() throws Exception {
assertEquals(IPHOST, status3.getHostName().toLowerCase());
assertTrue(status3.isForceSwitched());
assertTrue(status3.isPoolCreated());
assertEquals("jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;", dataSource.getSingleDataSource().getName());
assertEquals("jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;", dataSource.getSingleDataSource().getName());
assertEquals("3306", status3.getPort().toString());

dataSource.restore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ private DataSourceConfigure getDataSourceConfig() {
Properties p = new Properties();
p.setProperty("userName", "root");
p.setProperty("password", "!QAZ@WSX1qaz2wsx");
p.setProperty("connectionUrl", "jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8");
p.setProperty("connectionUrl", "jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8");
p.setProperty("driverClassName", "com.mysql.jdbc.Driver");
return new DataSourceConfigure("llj_test", p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void testOneSuccessDataSourceAfterOneConnectTimeoutDataSource() throws Ex
Properties p3 = new Properties();
p3.setProperty("userName", "root");
p3.setProperty("password", "111111");
p3.setProperty("connectionUrl", "jdbc:mysql://10.32.20.139:3306/test");
p3.setProperty("connectionUrl", "jdbc:mysql://10.32.20.143:3306/test");
p3.setProperty("driverClassName", "com.mysql.jdbc.Driver");
p3.setProperty("connectionProperties", "connectTimeout=3000");
final DataSourceConfigure configure3 = new DataSourceConfigure("test", p3);
Expand All @@ -217,7 +217,7 @@ public void run() {
Thread.sleep(4000);
Assert.assertNotNull(refreshableDataSource.getSingleDataSource());
Assert.assertNotNull(refreshableDataSource.getSingleDataSource().getDataSource());
Assert.assertEquals("jdbc:mysql://10.32.20.139:3306/test", refreshableDataSource.getSingleDataSource().getDataSourceConfigure().getProperty("connectionUrl"));
Assert.assertEquals("jdbc:mysql://10.32.20.143:3306/test", refreshableDataSource.getSingleDataSource().getDataSourceConfigure().getProperty("connectionUrl"));
}

@Test
Expand Down Expand Up @@ -300,7 +300,7 @@ public void testDataSourceSwitchNotify() throws Exception {
Properties p2 = new Properties();
p2.setProperty("userName", "root");
p2.setProperty("password", "!QAZ@WSX1qaz2wsx");
p2.setProperty("connectionUrl", "jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p2.setProperty("connectionUrl", "jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p2.setProperty("driverClassName", "com.mysql.jdbc.Driver");
DataSourceConfigure configure2 = new DataSourceConfigure("test2", p2);

Expand All @@ -325,7 +325,7 @@ public void run() {
//System.out.println(listenerOne.getStep() + ", " + listenerTwo.getStep());
if (listenerOne.getStep() == 10 && listenerTwo.getStep() == 20) {
switched.set(true);
Assert.assertEquals("jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;", refreshableDataSource.getSingleDataSource().getDataSourceConfigure().getConnectionUrl());
Assert.assertEquals("jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;", refreshableDataSource.getSingleDataSource().getDataSourceConfigure().getConnectionUrl());
}
latch.countDown();
}
Expand Down Expand Up @@ -376,7 +376,7 @@ public void testExecuteListenerTimeOut() throws Exception {
Properties p2 = new Properties();
p2.setProperty("userName", "root");
p2.setProperty("password", "!QAZ@WSX1qaz2wsx");
p2.setProperty("connectionUrl", "jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p2.setProperty("connectionUrl", "jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p2.setProperty("driverClassName", "com.mysql.jdbc.Driver");
DataSourceConfigure configure2 = new DataSourceConfigure("test2", p2);

Expand Down Expand Up @@ -434,7 +434,7 @@ public void testSetExecuteSwitchListenerTimeout() throws Exception {
Properties p2 = new Properties();
p2.setProperty("userName", "root");
p2.setProperty("password", "!QAZ@WSX1qaz2wsx");
p2.setProperty("connectionUrl", "jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p2.setProperty("connectionUrl", "jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p2.setProperty("driverClassName", "com.mysql.jdbc.Driver");
DataSourceConfigure dataSourceConfigure1 = new DataSourceConfigure("test2", p2);

Expand Down Expand Up @@ -488,7 +488,7 @@ public void testGetConnectionPerformance() throws Exception {
Properties p2 = new Properties();
p2.setProperty("userName", "root");
p2.setProperty("password", "!QAZ@WSX1qaz2wsx");
p2.setProperty("connectionUrl", "jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p2.setProperty("connectionUrl", "jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p2.setProperty("driverClassName", "com.mysql.jdbc.Driver");
DataSourceConfigure configure2 = new DataSourceConfigure("test2", p2);

Expand Down Expand Up @@ -516,7 +516,7 @@ public void testGetConnection() throws Exception {
Properties p2 = new Properties();
p2.setProperty("userName", "root");
p2.setProperty("password", "!QAZ@WSX1qaz2wsx");
p2.setProperty("connectionUrl", "jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p2.setProperty("connectionUrl", "jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p2.setProperty("driverClassName", "com.mysql.jdbc.Driver");
DataSourceConfigure configure2 = new DataSourceConfigure("test2", p2);

Expand All @@ -540,7 +540,7 @@ public void run() {
Connection connection = refreshableDataSource.getConnection();
long endTime = System.currentTimeMillis();
System.out.println(startTime + ":" + (endTime - startTime));
if ("jdbc:mysql://10.32.20.139:3306/llj_test".equalsIgnoreCase(ConnectionHelper.obtainUrl(connection))) {
if ("jdbc:mysql://10.32.20.143:3306/llj_test".equalsIgnoreCase(ConnectionHelper.obtainUrl(connection))) {
Assert.assertEquals(listenerOne.getStep(), 10);
Assert.assertEquals(listenerTwo.getStep(), 20);
}
Expand All @@ -565,7 +565,7 @@ public void testReusedSingleDataSource() {
Properties p1 = new Properties();
p1.setProperty("userName", "root");
p1.setProperty("password", "!QAZ@WSX1qaz2wsx");
p1.setProperty("connectionUrl", "jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p1.setProperty("connectionUrl", "jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p1.setProperty("driverClassName", "com.mysql.jdbc.Driver");
DataSourceConfigure config1 = new DataSourceConfigure("config1", p1);
DataSourceConfigure config2 = new DataSourceConfigure("config2", p1);
Expand All @@ -592,7 +592,7 @@ public void testDataSourceSwitch() throws Exception {
Properties p1 = new Properties();
p1.setProperty("userName", "root");
p1.setProperty("password", "!QAZ@WSX1qaz2wsx");
p1.setProperty("connectionUrl", "jdbc:mysql://10.32.20.139:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p1.setProperty("connectionUrl", "jdbc:mysql://10.32.20.143:3306/llj_test?useUnicode=true&characterEncoding=UTF-8;");
p1.setProperty("driverClassName", "com.mysql.jdbc.Driver");

Properties p2 = new Properties();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ctrip.platform.dal.dao.helper;

import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.util.HashSet;
Expand Down Expand Up @@ -132,6 +133,12 @@ public void testSetToString() {
set6.add("NotFound");
String str6 = LoggerHelper.setToOrderedString(set6);
assertTrue("NotFound".equals(str6));

Set<String> containNullObjectSet = new HashSet<>();
containNullObjectSet.add(StringUtils.EMPTY);
containNullObjectSet.add(null);
String actualResult = LoggerHelper.setToOrderedString(containNullObjectSet);
assertEquals(StringUtils.EMPTY, actualResult);
} catch (Throwable e) {
e.printStackTrace();
fail();
Expand Down
8 changes: 4 additions & 4 deletions dal-client/src/test/resources/TestCluster.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<DAL>
<Cluster name="TestCluster" dbCategory="mysql" version="1">
<DatabaseShards>
<DatabaseShard index="0" masterDomain="10.32.20.139" slaveDomain="10.32.20.139">
<Database ip="10.32.20.139" port="3306" dbName="Shard_0" uid="na" pwd="qazwsxedc"/>
<DatabaseShard index="0" masterDomain="10.32.20.143" slaveDomain="10.32.20.143">
<Database ip="10.32.20.143" port="3306" dbName="Shard_0" uid="na" pwd="qazwsxedc"/>
</DatabaseShard>
<DatabaseShard index="1" masterDomain="10.32.20.139" slaveDomain="10.32.20.139">
<Database ip="10.32.20.139" port="3306" dbName="Shard_1" uid="na" pwd="qazwsxedc"/>
<DatabaseShard index="1" masterDomain="10.32.20.143" slaveDomain="10.32.20.143">
<Database ip="10.32.20.143" port="3306" dbName="Shard_1" uid="na" pwd="qazwsxedc"/>
</DatabaseShard>
</DatabaseShards>
<ShardStrategies>
Expand Down
8 changes: 4 additions & 4 deletions dal-client/src/test/resources/clusterName1.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<DAL>
<Cluster name="clustername1" dbCategory="mysql" version="1">
<DatabaseShards>
<DatabaseShard index="0" masterDomain="10.32.20.139" slaveDomain="10.32.20.139">
<Database ip="10.32.20.139" port="3306" dbName="Shard_0" uid="na" pwd="qazwsxedc"/>
<DatabaseShard index="0" masterDomain="10.32.20.143" slaveDomain="10.32.20.143">
<Database ip="10.32.20.143" port="3306" dbName="Shard_0" uid="na" pwd="qazwsxedc"/>
</DatabaseShard>
<DatabaseShard index="1" masterDomain="10.32.20.139" slaveDomain="10.32.20.139">
<Database ip="10.32.20.139" port="3306" dbName="Shard_1" uid="na" pwd="qazwsxedc"/>
<DatabaseShard index="1" masterDomain="10.32.20.143" slaveDomain="10.32.20.143">
<Database ip="10.32.20.143" port="3306" dbName="Shard_1" uid="na" pwd="qazwsxedc"/>
</DatabaseShard>
</DatabaseShards>
<ShardStrategies>
Expand Down
8 changes: 4 additions & 4 deletions dal-client/src/test/resources/clusterName2.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<DAL>
<Cluster name="clustername2" dbCategory="mysql" version="1">
<DatabaseShards>
<DatabaseShard index="0" masterDomain="10.32.20.139" slaveDomain="10.32.20.139">
<Database ip="10.32.20.139" port="3306" dbName="Shard_0" uid="na" pwd="qazwsxedc"/>
<DatabaseShard index="0" masterDomain="10.32.20.143" slaveDomain="10.32.20.143">
<Database ip="10.32.20.143" port="3306" dbName="Shard_0" uid="na" pwd="qazwsxedc"/>
</DatabaseShard>
<DatabaseShard index="1" masterDomain="10.32.20.139" slaveDomain="10.32.20.139">
<Database ip="10.32.20.139" port="3306" dbName="Shard_1" uid="na" pwd="qazwsxedc"/>
<DatabaseShard index="1" masterDomain="10.32.20.143" slaveDomain="10.32.20.143">
<Database ip="10.32.20.143" port="3306" dbName="Shard_1" uid="na" pwd="qazwsxedc"/>
</DatabaseShard>
</DatabaseShards>
<ShardStrategies>
Expand Down

0 comments on commit 88c2d69

Please sign in to comment.