提交 651a4929 编写于 作者: T tristaZero

Merge branch 'dev' of ssh://github.com/shardingjdbc/sharding-jdbc into dev

......@@ -28,6 +28,7 @@ import lombok.Setter;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
......@@ -113,9 +114,22 @@ public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOpera
private void closeDataSource(final Map<String, DataSource> dataSourceMap) {
for (DataSource each : dataSourceMap.values()) {
try {
each.getClass().getDeclaredMethod("close").invoke(each);
findMethod(each, "close").invoke(each);
} catch (final ReflectiveOperationException ignored) {
}
}
}
@SuppressWarnings("unchecked")
private Method findMethod(final Object target, final String methodName, final Class<?>... parameterTypes) throws NoSuchMethodException {
Class clazz = target.getClass();
while (null != clazz) {
try {
return clazz.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException ignored) {
}
clazz = clazz.getSuperclass();
}
throw new NoSuchMethodException(String.format("Cannot find method '%s' in %s", methodName, target.getClass().getName()));
}
}
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package io.shardingsphere.transaction.xa.convert.dialect;
import com.google.common.base.Optional;
import io.shardingsphere.core.metadata.datasource.dialect.OracleDataSourceMetaData;
import io.shardingsphere.core.rule.DataSourceParameter;
import lombok.RequiredArgsConstructor;
import java.util.Properties;
/**
* Create Oracle XA property from datasource parameter.
*
* @author zhaojun
*/
@RequiredArgsConstructor
public class OracleXAProperty {
private final DataSourceParameter dataSourceParameter;
/**
* Build Oracle XA properties.
*
* @return Oracle XA properties
*/
public Properties build() {
Properties result = new Properties();
OracleDataSourceMetaData oracleMetaData = new OracleDataSourceMetaData(dataSourceParameter.getUrl());
result.setProperty("user", dataSourceParameter.getUsername());
result.setProperty("password", Optional.fromNullable(dataSourceParameter.getPassword()).or(""));
result.setProperty("serverName", oracleMetaData.getHostName());
result.setProperty("portNumber", String.valueOf(oracleMetaData.getPort()));
result.setProperty("databaseName", oracleMetaData.getSchemeName());
return result;
}
}
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package io.shardingsphere.transaction.xa.convert.dialect;
import com.google.common.base.Optional;
import io.shardingsphere.core.metadata.datasource.dialect.SQLServerDataSourceMetaData;
import io.shardingsphere.core.rule.DataSourceParameter;
import lombok.RequiredArgsConstructor;
import java.util.Properties;
/**
* Create SQLServer XA property from datasource parameter.
*
* @author zhaojun
*/
@RequiredArgsConstructor
public class SQLServerXAProperty {
private final DataSourceParameter dataSourceParameter;
/**
* Build SQLServer XA properties.
*
* @return SQLServer XA properties
*/
public Properties build() {
Properties result = new Properties();
SQLServerDataSourceMetaData sqlServerMetaData = new SQLServerDataSourceMetaData(dataSourceParameter.getUrl());
result.setProperty("user", dataSourceParameter.getUsername());
result.setProperty("password", Optional.fromNullable(dataSourceParameter.getPassword()).or(""));
result.setProperty("serverName", sqlServerMetaData.getHostName());
result.setProperty("portNumber", String.valueOf(sqlServerMetaData.getPort()));
result.setProperty("databaseName", sqlServerMetaData.getSchemeName());
return result;
}
}
......@@ -46,6 +46,10 @@ public class XAPropertyFactory {
return new PGXAProperty(dataSourceParameter).build();
case H2:
return new H2XAProperty(dataSourceParameter).build();
case SQLServer:
return new SQLServerXAProperty(dataSourceParameter).build();
case Oracle:
return new OracleXAProperty(dataSourceParameter).build();
default:
return new Properties();
}
......
......@@ -18,7 +18,6 @@
package io.shardingsphere.transaction.xa.convert.dialect;
import com.microsoft.sqlserver.jdbc.SQLServerXADataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import io.shardingsphere.core.constant.DatabaseType;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.Test;
......@@ -37,12 +36,6 @@ public class XADataSourceFactoryTest {
assertThat(xaDataSource, instanceOf(JdbcDataSource.class));
}
@Test
public void assertCreateMysqlXADataSource() {
XADataSource xaDataSource = XADataSourceFactory.build(DatabaseType.MySQL);
assertThat(xaDataSource, instanceOf(MysqlXADataSource.class));
}
@Test
public void assertCreatePGXADataSource() {
XADataSource xaDataSource = XADataSourceFactory.build(DatabaseType.PostgreSQL);
......
......@@ -85,5 +85,23 @@ public class XAPropertyFactoryTest {
@Test
public void assertGetSQLServerXAProperties() {
dataSourceParameter.setUrl("jdbc:sqlserver://db.sqlserver:1433;DatabaseName=test_db");
Properties xaProperties = XAPropertyFactory.build(XADatabaseType.SQLServer, dataSourceParameter);
assertThat(xaProperties.getProperty("user"), is("root"));
assertThat(xaProperties.getProperty("password"), is("root"));
assertThat(xaProperties.getProperty("serverName"), is("db.sqlserver"));
assertThat(xaProperties.getProperty("portNumber"), is("1433"));
assertThat(xaProperties.getProperty("databaseName"), is("test_db"));
}
@Test
public void assertGetOracleXAProperties() {
dataSourceParameter.setUrl("jdbc:oracle:thin:@//db.oracle:9999/test_db");
Properties xaProperties = XAPropertyFactory.build(XADatabaseType.Oracle, dataSourceParameter);
assertThat(xaProperties.getProperty("user"), is("root"));
assertThat(xaProperties.getProperty("password"), is("root"));
assertThat(xaProperties.getProperty("serverName"), is("db.oracle"));
assertThat(xaProperties.getProperty("portNumber"), is("9999"));
assertThat(xaProperties.getProperty("databaseName"), is("test_db"));
}
}
......@@ -27,6 +27,7 @@ import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.rule.DataSourceParameter;
import io.shardingsphere.transaction.xa.fixture.ReflectiveUtil;
import lombok.SneakyThrows;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......@@ -136,8 +137,8 @@ public final class AtomikosTransactionManagerTest {
}
@Test
public void assertWrapDataSourceForMySQL() {
XADataSource xaDataSource = new MysqlXADataSource();
public void assertWrapDataSourceForH2() {
XADataSource xaDataSource = new JdbcDataSource();
DataSourceParameter dataSourceParameter = new DataSourceParameter();
dataSourceParameter.setUsername("root");
dataSourceParameter.setPassword("root");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册