Skip to content

Commit

Permalink
optimize: remove mariadb API (#6330)
Browse files Browse the repository at this point in the history
  • Loading branch information
slievrly authored Feb 5, 2024
1 parent 843d2a4 commit c3da4d8
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 38 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6301](https://github.com/apache/incubator-seata/pull/6301)] upgrade console frontend dependencies and supported nodejs versions
- [[#6301](https://github.com/apache/incubator-seata/pull/6312)] add saga related io.seata compatible api
- [[#6313](https://github.com/apache/incubator-seata/pull/6313)] console display the version number
- [[#6330](https://github.com/apache/incubator-seata/pull/6330)] remove mariadb API
- [[#6329](https://github.com/apache/incubator-seata/pull/6312)] add saga subcomponent-level io.seata compatible api
- [[#6254](https://github.com/apache/incubator-seata/pull/6254)] optimize Hessian Serialize

Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
- [[#6301](https://github.com/apache/incubator-seata/pull/6301)] 升级console前端依赖及支持的nodejs版本
- [[#6301](https://github.com/apache/incubator-seata/pull/6312)] 添加saga相关的io.seata兼容性API
- [[#6313](https://github.com/apache/incubator-seata/pull/6313)] console展示版本号
- [[#6330](https://github.com/apache/incubator-seata/pull/6330)] 去除 mariadb API
- [[#6329](https://github.com/apache/incubator-seata/pull/6312)] 添加saga子组件的io.seata兼容性API
- [[#6254](https://github.com/apache/incubator-seata/pull/6254)] 优化Hessian 序列化

Expand Down
3 changes: 1 addition & 2 deletions rm-datasource/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>provided</scope>
<optional>true</optional>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.sql.XAConnection;
import javax.transaction.xa.XAException;

import com.alibaba.druid.util.JdbcUtils;
import com.alibaba.druid.util.MySqlUtils;
import com.alibaba.druid.util.PGUtils;

import org.apache.seata.rm.BaseDataSourceResource;
import org.apache.seata.sqlparser.util.JdbcConstants;
import org.mariadb.jdbc.MariaDbConnection;
import org.mariadb.jdbc.MariaXaConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -44,49 +48,101 @@ public static XAConnection createXAConnection(Connection physicalConn, BaseDataS
return createXAConnection(physicalConn, dataSourceResource.getDriver(), dataSourceResource.getDbType());
}

public static XAConnection createXAConnection(Connection physicalConn, Driver driver, String dbType) throws SQLException {
public static XAConnection createXAConnection(Connection physicalConn, Driver driver, String dbType)
throws SQLException {
if (JdbcConstants.MYSQL.equals(dbType)) {
return MySqlUtils.createXAConnection(driver, physicalConn);
} else {
switch (dbType) {
case JdbcConstants.ORACLE:
try {
try {
switch (dbType) {
case JdbcConstants.ORACLE:
// https://github.com/alibaba/druid/issues/3707
// before Druid issue fixed, just make ORACLE XA connection in my way.
// return OracleUtils.OracleXAConnection(physicalConn);
String physicalConnClassName = physicalConn.getClass().getName();
if ("oracle.jdbc.driver.T4CConnection".equals(physicalConnClassName)) {
return createOracleXAConnection(physicalConn, "oracle.jdbc.driver.T4CXAConnection");
return createXAConnection(physicalConn, "oracle.jdbc.driver.T4CXAConnection", dbType);
} else {
return createOracleXAConnection(physicalConn, "oracle.jdbc.xa.client.OracleXAConnection");
return createXAConnection(physicalConn, "oracle.jdbc.xa.client.OracleXAConnection", dbType);
}
} catch (XAException xae) {
throw new SQLException("create xaConnection error", xae);
}
case JdbcConstants.MARIADB:
return new MariaXaConnection((MariaDbConnection)physicalConn);
case JdbcConstants.POSTGRESQL:
return PGUtils.createXAConnection(physicalConn);
default:
throw new SQLException("xa not support dbType: " + dbType);
case JdbcConstants.MARIADB:
return createXAConnection(physicalConn, "org.mariadb.jdbc.MariaXaConnection", dbType);
case JdbcConstants.POSTGRESQL:
return PGUtils.createXAConnection(physicalConn);
default:
throw new SQLException("xa not support dbType: " + dbType);
}
} catch (Exception xae) {
throw new SQLException("create xaConnection error", xae);
}
}
}

private static XAConnection createOracleXAConnection(Connection physicalConnection, String xaConnectionClassName) throws XAException, SQLException {
private static XAConnection createXAConnection(Connection physicalConnection, String xaConnectionClassName,
String dbType) throws XAException, SQLException {
try {
Class xaConnectionClass = Class.forName(xaConnectionClassName);
Constructor<XAConnection> constructor = xaConnectionClass.getConstructor(Connection.class);
Constructor<XAConnection> constructor = getConstructorByDBType(xaConnectionClass, dbType);
if (constructor == null) {
throw new SQLException("xa not support dbType: " + dbType);
}
constructor.setAccessible(true);
return constructor.newInstance(physicalConnection);
List<Object> params = getInitargsByDBType(dbType, physicalConnection);
return constructor.newInstance(params.toArray(new Object[0]));
} catch (Exception e) {
LOGGER.warn("Failed to create Oracle XA Connection " + xaConnectionClassName + " on " + physicalConnection);
LOGGER.warn("Failed to create XA Connection " + xaConnectionClassName + " on " + physicalConnection);
if (e instanceof XAException) {
throw (XAException) e;
throw (XAException)e;
} else {
throw new SQLException(e);
}
}

}

private static Constructor<XAConnection> getConstructorByDBType(Class xaConnectionClass, String dbType) throws SQLException {
try {
switch (dbType) {
case JdbcConstants.ORACLE:
return xaConnectionClass.getConstructor(Connection.class);
case JdbcConstants.MARIADB:
//MariaXaConnection(MariaDbConnection connection)
Class mariaXaConnectionClass = Class.forName("org.mariadb.jdbc.MariaDbConnection");
return xaConnectionClass.getConstructor(mariaXaConnectionClass);
default:
throw new SQLException("xa reflect not support dbType: " + dbType);
}
} catch (Exception e) {
throw new SQLException(e);
}
}

private static <T> List<T> getInitargsByDBType(String dbType, Object... params) throws SQLException {
List result = new ArrayList<>();
if (params.length == 0) {
return null;
}
if (!(params[0] instanceof Connection)) {
throw new SQLException("not support params: " + Arrays.toString(params));
}

try {
switch (dbType) {
case JdbcConstants.ORACLE:
result.add(params[0]);
return result;
case JdbcConstants.MARIADB:
Class mariaDbConnectionClass = Class.forName("org.mariadb.jdbc.MariaDbConnection");
if (mariaDbConnectionClass.isInstance(params[0])) {
Object mariaDbConnectionInstance = mariaDbConnectionClass.cast(params[0]);
result.add(mariaDbConnectionInstance);
return result;
}
default:
throw new SQLException("xa reflect not support dbType: " + dbType);
}
} catch (Exception e) {
throw new SQLException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@
*/
package org.apache.seata.rm.xa;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.SQLException;

import javax.sql.DataSource;
import javax.sql.PooledConnection;
import javax.sql.XAConnection;

import com.alibaba.druid.pool.DruidDataSource;

import com.mysql.jdbc.JDBC4MySQLConnection;
import com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper;
import org.apache.seata.core.context.RootContext;
Expand All @@ -26,22 +36,12 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mariadb.jdbc.MariaDbConnection;
import org.mockito.Mockito;

import javax.sql.DataSource;
import javax.sql.PooledConnection;
import javax.sql.XAConnection;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.SQLException;

import static org.mockito.ArgumentMatchers.any;

/**
* Tests for DataSourceProxyXA
*
*/
public class DataSourceProxyXATest {

Expand Down Expand Up @@ -91,10 +91,11 @@ public void testGetConnection() throws SQLException {
}

@Test
public void testGetMariaXaConnection() throws SQLException {
public void testGetMariaXaConnection() throws SQLException, ClassNotFoundException {
// Mock
Driver driver = Mockito.mock(Driver.class);
MariaDbConnection connection = Mockito.mock(MariaDbConnection.class);
Class clazz = Class.forName("org.mariadb.jdbc.MariaDbConnection");
Connection connection = (Connection)(Mockito.mock(clazz));
Mockito.when(connection.getAutoCommit()).thenReturn(true);
DatabaseMetaData metaData = Mockito.mock(DatabaseMetaData.class);
Mockito.when(metaData.getURL()).thenReturn("jdbc:mariadb:xxx");
Expand All @@ -120,12 +121,12 @@ public void testGetMariaXaConnection() throws SQLException {

XAConnection xaConnection = connectionProxyXA.getWrappedXAConnection();
Connection connectionInXA = xaConnection.getConnection();
Assertions.assertTrue(connectionInXA instanceof MariaDbConnection);
Assertions.assertEquals("org.mariadb.jdbc.MariaDbConnection", connectionInXA.getClass().getName());
tearDown();
}

@AfterAll
public static void tearDown(){
public static void tearDown() {
RootContext.unbind();
}
}

0 comments on commit c3da4d8

Please sign in to comment.