-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #424 from MyCATApache/dev
Dev
- Loading branch information
Showing
19 changed files
with
423 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 96 additions & 96 deletions
192
...src/main/java/io/mycat/datasource/jdbc/datasourceprovider/AtomikosDatasourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
example/src/test/java/io/mycat/springdata/AbstractEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
40 changes: 40 additions & 0 deletions
40
example/src/test/java/io/mycat/springdata/AuditingConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.