From c4c5faf8756ad056b2c826762b2a2bae4491a985 Mon Sep 17 00:00:00 2001 From: Viken Date: Fri, 19 Jul 2019 17:02:41 +0800 Subject: [PATCH] add undo_log.sql and add database driver class config (#1323) --- .../core/constants/ConfigurationKeys.java | 4 ++++ .../store/db/AbstractDataSourceGenerator.java | 16 +++++++++++++--- .../store/db/DbcpDataSourceGenerator.java | 2 +- .../store/db/DruidDataSourceGenerator.java | 2 +- server/src/main/resources/db_undo_log.sql | 19 +++++++++++++++++++ server/src/main/resources/file.conf | 1 + 6 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 server/src/main/resources/db_undo_log.sql diff --git a/core/src/main/java/io/seata/core/constants/ConfigurationKeys.java b/core/src/main/java/io/seata/core/constants/ConfigurationKeys.java index 99de0be73e3..3d3585ed2f8 100644 --- a/core/src/main/java/io/seata/core/constants/ConfigurationKeys.java +++ b/core/src/main/java/io/seata/core/constants/ConfigurationKeys.java @@ -141,6 +141,10 @@ public class ConfigurationKeys { * The constant STORE_DB_TYPE. */ public static final String STORE_DB_TYPE = "store.db.db-type"; + /** + * The constant STORE_DB_DRIVER_CLASS_NAME. + */ + public static final String STORE_DB_DRIVER_CLASS_NAME = "store.db.driver-class-name"; /** * The constant STORE_DB_URL. diff --git a/core/src/main/java/io/seata/core/store/db/AbstractDataSourceGenerator.java b/core/src/main/java/io/seata/core/store/db/AbstractDataSourceGenerator.java index 070f69ce7e9..40846480a7d 100644 --- a/core/src/main/java/io/seata/core/store/db/AbstractDataSourceGenerator.java +++ b/core/src/main/java/io/seata/core/store/db/AbstractDataSourceGenerator.java @@ -43,7 +43,17 @@ public abstract class AbstractDataSourceGenerator implements DataSourceGenerator protected DBType getDBType() { return DBType.valueof(CONFIG.getConfig(ConfigurationKeys.STORE_DB_TYPE)); } - + /** + * get db driver class name + * @return the db driver class name + */ + protected String getDriverClassName() { + String driverClassName = CONFIG.getConfig(ConfigurationKeys.STORE_DB_DRIVER_CLASS_NAME); + if (StringUtils.isBlank(driverClassName)) { + throw new StoreException("the {store.db.driver-class-name} can't be empty."); + } + return driverClassName; + } /** * Get url string. * @@ -52,7 +62,7 @@ protected DBType getDBType() { protected String getUrl() { String url = CONFIG.getConfig(ConfigurationKeys.STORE_DB_URL); if (StringUtils.isBlank(url)) { - throw new StoreException("the {store.db.url} can't empty."); + throw new StoreException("the {store.db.url} can't be empty."); } return url; } @@ -65,7 +75,7 @@ protected String getUrl() { protected String getUser() { String user = CONFIG.getConfig(ConfigurationKeys.STORE_DB_USER); if (StringUtils.isBlank(user)) { - throw new StoreException("the {store.db.user} can't empty."); + throw new StoreException("the {store.db.user} can't be empty."); } return user; } diff --git a/server/src/main/java/io/seata/server/store/db/DbcpDataSourceGenerator.java b/server/src/main/java/io/seata/server/store/db/DbcpDataSourceGenerator.java index 2cc8e14929e..9c7187a6cc5 100644 --- a/server/src/main/java/io/seata/server/store/db/DbcpDataSourceGenerator.java +++ b/server/src/main/java/io/seata/server/store/db/DbcpDataSourceGenerator.java @@ -33,7 +33,7 @@ public class DbcpDataSourceGenerator extends AbstractDataSourceGenerator { @Override public DataSource generateDataSource() { BasicDataSource ds = new BasicDataSource(); - ds.setDriverClassName(getDriverName(getDBType())); + ds.setDriverClassName(getDriverClassName()); ds.setUrl(getUrl()); ds.setUsername(getUser()); ds.setPassword(getPassword()); diff --git a/server/src/main/java/io/seata/server/store/db/DruidDataSourceGenerator.java b/server/src/main/java/io/seata/server/store/db/DruidDataSourceGenerator.java index 04ddfe77b63..f438e81f8a4 100644 --- a/server/src/main/java/io/seata/server/store/db/DruidDataSourceGenerator.java +++ b/server/src/main/java/io/seata/server/store/db/DruidDataSourceGenerator.java @@ -33,7 +33,7 @@ public class DruidDataSourceGenerator extends AbstractDataSourceGenerator { @Override public DataSource generateDataSource() { DruidDataSource ds = new DruidDataSource(); - ds.setDriverClassName(getDriverName(getDBType())); + ds.setDriverClassName(getDriverClassName()); ds.setUrl(getUrl()); ds.setUsername(getUser()); ds.setPassword(getPassword()); diff --git a/server/src/main/resources/db_undo_log.sql b/server/src/main/resources/db_undo_log.sql new file mode 100644 index 00000000000..f87b7cdd78b --- /dev/null +++ b/server/src/main/resources/db_undo_log.sql @@ -0,0 +1,19 @@ +-- the table to store seata xid data +-- 0.7.0+ add context +-- you must to init this sql for you business databese. the seata server not need it. +-- 此脚本必须初始化在你当前的业务数据库中,用于AT 模式XID记录。与server端无关(注:业务数据库) +-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log +drop table `undo_log`; +CREATE TABLE `undo_log` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `branch_id` bigint(20) NOT NULL, + `xid` varchar(100) NOT NULL, + `context` varchar(128) NOT NULL, + `rollback_info` longblob NOT NULL, + `log_status` int(11) NOT NULL, + `log_created` datetime NOT NULL, + `log_modified` datetime NOT NULL, + `ext` varchar(100) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; \ No newline at end of file diff --git a/server/src/main/resources/file.conf b/server/src/main/resources/file.conf index da6b0dddd5f..a5607c1c092 100644 --- a/server/src/main/resources/file.conf +++ b/server/src/main/resources/file.conf @@ -76,6 +76,7 @@ store { datasource = "dbcp" ## mysql/oracle/h2/oceanbase etc. db-type = "mysql" + driver-class-name = "com.mysql.jdbc.Driver" url = "jdbc:mysql://127.0.0.1:3306/seata" user = "mysql" password = "mysql"