Browse Source

修改多数据源依赖

zhouhao 8 years ago
parent
commit
2689095962

+ 5 - 1
hsweb-web-controller/src/main/java/org/hsweb/web/controller/RestControllerExceptionTranslator.java

@@ -3,6 +3,8 @@ package org.hsweb.web.controller;
 import com.alibaba.fastjson.JSON;
 import org.hsweb.web.core.exception.*;
 import org.hsweb.web.core.message.ResponseMessage;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.core.annotation.Order;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.*;
@@ -14,6 +16,8 @@ import javax.servlet.http.HttpServletResponse;
 @Order(1)
 public class RestControllerExceptionTranslator {
 
+    private Logger logger = LoggerFactory.getLogger(this.getClass());
+
     @ExceptionHandler(ValidationException.class)
     @ResponseStatus(HttpStatus.BAD_REQUEST)
     @ResponseBody
@@ -36,7 +40,6 @@ public class RestControllerExceptionTranslator {
         return ResponseMessage.error(exception.getMessage(), exception.getStatus());
     }
 
-
     @ExceptionHandler(AuthorizeException.class)
     @ResponseStatus(HttpStatus.UNAUTHORIZED)
     @ResponseBody
@@ -63,6 +66,7 @@ public class RestControllerExceptionTranslator {
     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
     @ResponseBody
     ResponseMessage handleException(Throwable exception) {
+        logger.error("未知错误", exception);
         return ResponseMessage.error(exception.getMessage(), 500);
     }
 

+ 4 - 31
hsweb-web-datasource/pom.xml

@@ -33,11 +33,10 @@
             <artifactId>h2</artifactId>
             <scope>test</scope>
         </dependency>
-
-        <!--<dependency>-->
-            <!--<groupId>org.springframework.boot</groupId>-->
-            <!--<artifactId>spring-boot-starter-web</artifactId>-->
-        <!--</dependency>-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-jta-atomikos</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
@@ -66,32 +65,6 @@
             <scope>test</scope>
         </dependency>
 
-        <!--atomikos-->
-        <dependency>
-            <groupId>com.atomikos</groupId>
-            <artifactId>transactions-jdbc</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>com.atomikos</groupId>
-            <artifactId>transactions-jta</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>com.atomikos</groupId>
-            <artifactId>transactions</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>com.atomikos</groupId>
-            <artifactId>transactions-api</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>com.atomikos</groupId>
-            <artifactId>atomikos-util</artifactId>
-        </dependency>
-
         <dependency>
             <groupId>javax.transaction</groupId>
             <artifactId>jta</artifactId>

+ 2 - 25
hsweb-web-datasource/src/main/java/org/hsweb/web/datasource/dynamic/DynamicDataSourceAutoConfiguration.java

@@ -19,13 +19,10 @@ package org.hsweb.web.datasource.dynamic;
 import com.atomikos.icatch.jta.UserTransactionImp;
 import com.atomikos.icatch.jta.UserTransactionManager;
 import com.atomikos.jdbc.AtomikosDataSourceBean;
-import org.hsweb.commons.StringUtils;
 import org.hsweb.web.core.datasource.DynamicDataSource;
-import org.hsweb.web.service.datasource.DynamicDataSourceService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
@@ -44,11 +41,6 @@ public class DynamicDataSourceAutoConfiguration {
     @Autowired
     private DataSourceProperties properties;
 
-    static {
-        //  com.atomikos.icatch.config.Configuration.init();
-        //  com.atomikos.icatch.config.Configuration.installCompositeTransactionManager(new CompositeTransactionManagerImp());
-    }
-
     /**
      * 默认数据库链接
      */
@@ -59,10 +51,8 @@ public class DynamicDataSourceAutoConfiguration {
         dataSourceBean.getXaProperties().putAll(properties.getXa().getProperties());
         dataSourceBean.setXaDataSourceClassName(properties.getXa().getDataSourceClassName());
         dataSourceBean.setUniqueResourceName("core");
-        dataSourceBean.setMinPoolSize(StringUtils.toInt(properties.getXa().getProperties().get("minPoolSize"), 5));
-        dataSourceBean.setMaxPoolSize(StringUtils.toInt(properties.getXa().getProperties().get("maxPoolSize"), 200));
-        dataSourceBean.setTestQuery(properties.getXa().getProperties().get("validationQuery"));
-        dataSourceBean.setBorrowConnectionTimeout(60);
+        dataSourceBean.setMinPoolSize(5);
+        dataSourceBean.setMaxPoolSize(200);
         return dataSourceBean;
     }
 
@@ -71,19 +61,6 @@ public class DynamicDataSourceAutoConfiguration {
         return new DynamicXaDataSourceImpl(dataSource);
     }
 
-    /**
-     * 动态数据源
-     */
-    @Bean(initMethod = "init", destroyMethod = "close")
-    public AtomikosDataSourceBean atomikosDataSourceBean(DynamicXaDataSourceImpl dynamicDataSource) {
-        AtomikosDataSourceBean dataSourceBean = new AtomikosDataSourceBean();
-        dataSourceBean.setXaDataSource(dynamicDataSource);
-        dataSourceBean.setUniqueResourceName("dynamic");
-        dataSourceBean.setMaxPoolSize(StringUtils.toInt(properties.getXa().getProperties().get("maxPoolSize"), 200));
-        dataSourceBean.setBorrowConnectionTimeout(30);
-        return dataSourceBean;
-    }
-
     @Bean
     public UserTransactionManager userTransactionManager() {
         UserTransactionManager transactionManager = new UserTransactionManager();

+ 0 - 2
hsweb-web-datasource/src/main/java/org/hsweb/web/datasource/dynamic/DynamicDataSourceServiceImpl.java

@@ -27,7 +27,6 @@ import org.hsweb.web.service.datasource.DynamicDataSourceService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
 import org.springframework.stereotype.Service;
 
@@ -139,7 +138,6 @@ public class DynamicDataSourceServiceImpl implements DynamicDataSourceService {
         dataSourceBean.setUniqueResourceName("ds_" + dataSource.getId());
         dataSourceBean.setMaxPoolSize(200);
         dataSourceBean.setMinPoolSize(5);
-        dataSourceBean.setTestQuery(dataSource.getTestSql());
         dataSourceBean.setBorrowConnectionTimeout(60);
         try {
             dataSourceBean.init();

+ 1 - 0
hsweb-web-service/hsweb-web-service-simple/src/test/java/org/hsweb/web/service/impl/datasource/DatasourceTests.java

@@ -45,6 +45,7 @@ public class DatasourceTests extends AbstractTestCase {
 
     @Before
     public void setup() throws SQLException {
+        dataSourceService.delete("test");
         DataSource dataSource = new DataSource();
         dataSource.setId("test");
         dataSource.setName("test");

+ 2 - 1
hsweb-web-service/hsweb-web-service-simple/src/test/java/org/hsweb/web/service/impl/datasource/TestService.java

@@ -35,12 +35,13 @@ public class TestService {
     }
 
     @Transactional
-    public void test() throws SQLException {
+    public void test() throws Exception {
         sqlExecutor.exec(new SimpleSQL("drop table if exists s_test"));
         sqlExecutor.exec(new SimpleSQL("create table s_test(name varchar(32))"));
         System.out.println(sqlExecutor.list(new SimpleSQL("select * from s_test"), new SimpleMapWrapper()));
         System.out.println(sqlExecutor.insert(new SimpleSQL("insert into s_test values ('默认数据源')")));
         DynamicDataSource.use("test");
+        Thread.sleep(30000);
         sqlExecutor.exec(new SimpleSQL("drop table if exists s_test"));
         sqlExecutor.exec(new SimpleSQL("create table s_test(name varchar(32))"));
         System.out.println(sqlExecutor.list(new SimpleSQL("select * from s_test"), new SimpleMapWrapper()));

+ 1 - 1
hsweb-web-service/hsweb-web-service-simple/src/test/resources/transactions.properties

@@ -13,7 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-
+com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
 com.atomikos.icatch.serial_jta_transactions=false
 com.atomikos.icatch.output_dir=./data/atomikos
 com.atomikos.icatch.log_base_dir=./data/atomikos

+ 0 - 31
pom.xml

@@ -42,7 +42,6 @@
         <hsweb.ezorm.version>1.0-SNAPSHOT</hsweb.ezorm.version>
         <hsweb.commons.version>1.0-SNAPSHOT</hsweb.commons.version>
         <hsweb.expands.version>1.0-SNAPSHOT</hsweb.expands.version>
-        <atomikos.version>4.0.4</atomikos.version>
     </properties>
 
     <build>
@@ -112,36 +111,6 @@
 
     <dependencyManagement>
         <dependencies>
-            <!--atomikos-->
-            <dependency>
-                <groupId>com.atomikos</groupId>
-                <artifactId>transactions-jdbc</artifactId>
-                <version>${atomikos.version}</version>
-            </dependency>
-
-            <dependency>
-                <groupId>com.atomikos</groupId>
-                <artifactId>transactions-jta</artifactId>
-                <version>${atomikos.version}</version>
-            </dependency>
-
-            <dependency>
-                <groupId>com.atomikos</groupId>
-                <artifactId>transactions</artifactId>
-                <version>${atomikos.version}</version>
-            </dependency>
-
-            <dependency>
-                <groupId>com.atomikos</groupId>
-                <artifactId>transactions-api</artifactId>
-                <version>${atomikos.version}</version>
-            </dependency>
-
-            <dependency>
-                <groupId>com.atomikos</groupId>
-                <artifactId>atomikos-util</artifactId>
-                <version>${atomikos.version}</version>
-            </dependency>
 
             <dependency>
                 <groupId>javax.transaction</groupId>