Skip to content

Commit

Permalink
Merge pull request #424 from MyCATApache/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
junwen12221 authored Dec 15, 2020
2 parents 82e1316 + 365d47e commit e89c458
Show file tree
Hide file tree
Showing 19 changed files with 423 additions and 175 deletions.
10 changes: 8 additions & 2 deletions common/src/main/java/io/mycat/Authenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ public interface Authenticator {
List<UserConfig> allUsers();

@Data
@AllArgsConstructor
public static class AuthInfo {
Exception exception;
String exception;
String rightPassword;
int errorCode;

public AuthInfo(String exception, String rightPassword, int errorCode) {
this.exception = exception;
this.rightPassword = rightPassword;
this.errorCode = errorCode;
}

public boolean isOk() {
return exception == null;
Expand Down
20 changes: 10 additions & 10 deletions datasource/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@
</dependency>

<!-- https://mvnrepository.com/artifact/com.atomikos/transactions-jdbc -->
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId>
<version>5.0.6</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.atomikos</groupId>-->
<!-- <artifactId>transactions-jdbc</artifactId>-->
<!-- <version>5.0.6</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/com.atomikos/transactions-api -->
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-api</artifactId>
<version>5.0.1</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.atomikos</groupId>-->
<!-- <artifactId>transactions-api</artifactId>-->
<!-- <version>5.0.1</version>-->
<!-- </dependency>-->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,97 +1,97 @@
/**
* Copyright (C) <2019> <chen junwen>
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If
* not, see <http://www.gnu.org/licenses/>.
*/
package io.mycat.datasource.jdbc.datasourceprovider;

import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.cj.jdbc.MysqlXADataSource;
import io.mycat.config.DatasourceConfig;
import io.mycat.config.DatasourceRootConfig;
import io.mycat.datasource.jdbc.DatasourceProvider;
import io.mycat.datasource.jdbc.datasource.JdbcDataSource;
import lombok.SneakyThrows;

import javax.transaction.UserTransaction;
import java.util.List;
import java.util.Properties;
/**
* @author Junwen Chen
**/
public class AtomikosDatasourceProvider implements DatasourceProvider {

@SneakyThrows
@Override
public JdbcDataSource createDataSource(DatasourceConfig config) {
String username = config.getUser();
String password = config.getPassword();
String url = config.getUrl();
String dbType = config.getDbType();
int maxRetryCount = config.getMaxRetryCount();
List<String> initSQL = config.getInitSqls();
int maxCon = config.getMaxCon();
int minCon = config.getMinCon();

Properties p = new Properties();
p.setProperty("pinGlobalTxToPhysicalConnection","true");
p.setProperty("com.atomikos.icatch.serial_jta_transactions", "false");
AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
ds.setXaProperties(p);
ds.setConcurrentConnectionValidation(true);
ds.setUniqueResourceName(config.getName());
ds.setPoolSize(minCon);
ds.setMaxPoolSize(maxCon);

ds.setBorrowConnectionTimeout(60);
///////////////////////////////////////
ds.setLocalTransactionMode(true);
//////////////////////////////////////
MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
mysqlXaDataSource.setUser(username);
mysqlXaDataSource.setPassword(password);
mysqlXaDataSource.setUrl(url);
mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
mysqlXaDataSource.setMaxReconnects(maxRetryCount);
mysqlXaDataSource.setConnectTimeout((int) config.getMaxConnectTimeout());
// DruidXADataSource datasource = new DruidXADataSource();
///**
// * Copyright (C) <2019> <chen junwen>
// *
// * This program is free software: you can redistribute it and/or modify it under the terms of the
// * GNU General Public License as published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
// * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// * General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License along with this program. If
// * not, see <http://www.gnu.org/licenses/>.
// */
//package io.mycat.datasource.jdbc.datasourceprovider;
//
// if (maxRetryCount > 0) {
// datasource.setConnectionErrorRetryAttempts(maxRetryCount);
// }
// if (dbType != null) {
// datasource.setDbType(dbType);
// }
// if (initSQL != null) {
// datasource.setConnectionInitSqls(
// SQLParserUtils.createSQLStatementParser(initSQL, dbType).parseStatementList().stream()
// .map(Object::toString).collect(
// Collectors.toList()));
// }
// if (jdbcDriver != null) {
// datasource.setD(jdbcDriver);
// }
ds.setXaDataSource(mysqlXaDataSource);
return new JdbcDataSource(config,ds);
}

@Override
public void closeDataSource(JdbcDataSource dataSource) {

}

@Override
public UserTransaction createUserTransaction() {
return new UserTransactionImp();
}
}
//import com.atomikos.icatch.jta.UserTransactionImp;
//import com.atomikos.jdbc.AtomikosDataSourceBean;
//import com.mysql.cj.jdbc.MysqlXADataSource;
//import io.mycat.config.DatasourceConfig;
//import io.mycat.config.DatasourceRootConfig;
//import io.mycat.datasource.jdbc.DatasourceProvider;
//import io.mycat.datasource.jdbc.datasource.JdbcDataSource;
//import lombok.SneakyThrows;
//
//import javax.transaction.UserTransaction;
//import java.util.List;
//import java.util.Properties;
///**
// * @author Junwen Chen
// **/
//public class AtomikosDatasourceProvider implements DatasourceProvider {
//
// @SneakyThrows
// @Override
// public JdbcDataSource createDataSource(DatasourceConfig config) {
// String username = config.getUser();
// String password = config.getPassword();
// String url = config.getUrl();
// String dbType = config.getDbType();
// int maxRetryCount = config.getMaxRetryCount();
// List<String> initSQL = config.getInitSqls();
// int maxCon = config.getMaxCon();
// int minCon = config.getMinCon();
//
// Properties p = new Properties();
// p.setProperty("pinGlobalTxToPhysicalConnection","true");
// p.setProperty("com.atomikos.icatch.serial_jta_transactions", "false");
// AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
// ds.setXaProperties(p);
// ds.setConcurrentConnectionValidation(true);
// ds.setUniqueResourceName(config.getName());
// ds.setPoolSize(minCon);
// ds.setMaxPoolSize(maxCon);
//
// ds.setBorrowConnectionTimeout(60);
// ///////////////////////////////////////
// ds.setLocalTransactionMode(true);
// //////////////////////////////////////
// MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
// mysqlXaDataSource.setUser(username);
// mysqlXaDataSource.setPassword(password);
// mysqlXaDataSource.setUrl(url);
// mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
// mysqlXaDataSource.setMaxReconnects(maxRetryCount);
// mysqlXaDataSource.setConnectTimeout((int) config.getMaxConnectTimeout());
//// DruidXADataSource datasource = new DruidXADataSource();
////
//// if (maxRetryCount > 0) {
//// datasource.setConnectionErrorRetryAttempts(maxRetryCount);
//// }
//// if (dbType != null) {
//// datasource.setDbType(dbType);
//// }
//// if (initSQL != null) {
//// datasource.setConnectionInitSqls(
//// SQLParserUtils.createSQLStatementParser(initSQL, dbType).parseStatementList().stream()
//// .map(Object::toString).collect(
//// Collectors.toList()));
//// }
//// if (jdbcDriver != null) {
//// datasource.setD(jdbcDriver);
//// }
// ds.setXaDataSource(mysqlXaDataSource);
// return new JdbcDataSource(config,ds);
// }
//
// @Override
// public void closeDataSource(JdbcDataSource dataSource) {
//
// }
//
// @Override
// public UserTransaction createUserTransaction() {
// return new UserTransactionImp();
// }
//}
25 changes: 25 additions & 0 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@
<artifactId>example</artifactId>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.25.Final</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -40,5 +59,11 @@
<version>1.0-1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
11 changes: 7 additions & 4 deletions example/src/test/java/io/mycat/assemble/RwTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ public void testRw() throws Exception {
execute(mycat, "create database " + db);
execute(mycat, "use " + db);

execute(mycat,
"/*+ mycat:createDatasource{\"name\":\"dw0\",\"url\":\"jdbc:mysql://127.0.0.1:3306\",\"user\":\"root\",\"password\":\"123456\"} */;");
execute(mycat,
"/*+ mycat:createDatasource{\"name\":\"dr0\",\"url\":\"jdbc:mysql://127.0.0.1:3307\",\"user\":\"root\",\"password\":\"123456\"} */;");
execute(mycat, CreateDataSourceHint
.create("dw0",
"jdbc:mysql://127.0.0.1:3306"));
execute(mycat, CreateDataSourceHint
.create("dr0",
"jdbc:mysql://127.0.0.1:3307"));

execute(mycat,
"/*+ mycat:createCluster{\"name\":\"c0\",\"masters\":[\"dw0\"],\"replicas\":[\"dr0\"]} */;");

Expand Down
28 changes: 28 additions & 0 deletions example/src/test/java/io/mycat/springdata/AbstractEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.mycat.springdata;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

/**
* @author Oliver Gierke
*/
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractEntity {

@Id
@GeneratedValue
Long id;

@CreatedDate
LocalDateTime createdDate;
@LastModifiedDate
LocalDateTime modifiedDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.mycat.springdata;

import io.mycat.assemble.MycatTest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Optional;

import static com.alibaba.druid.util.JdbcUtils.execute;

@EnableAsync
@SpringBootApplication
@EnableJpaAuditing
class AuditingConfiguration {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver"); //注册数据库驱动
String url = "jdbc:mysql://localhost:8066?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
//定义连接数据库的url
try(Connection conn = DriverManager.getConnection(url,"root","123456");){
execute(conn, "create database IF NOT EXISTS db1");
}

SpringApplication springApplication = new SpringApplication(AuditingConfiguration.class);
ConfigurableApplicationContext applicationContext = springApplication.run();
CustomerRepository bean = applicationContext.getBean(CustomerRepository.class);
Customer customer = new Customer();
customer.lastname = "1";
bean.save(customer);
Optional<Customer> byLastname = bean.findByLastname("1");
System.out.println();
}
}
24 changes: 24 additions & 0 deletions example/src/test/java/io/mycat/springdata/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.mycat.springdata;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.ToString;

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Getter
@ToString
@AllArgsConstructor
@Table( catalog="db1",name = "sys_user")
public class Customer extends AbstractEntity {

String firstname, lastname;

protected Customer() {
this.firstname = null;
this.lastname = null;
}
}
Loading

0 comments on commit e89c458

Please sign in to comment.