You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "gavinWithGu (via GitHub)" <gi...@apache.org> on 2023/12/13 01:16:41 UTC

[I] Question for shardingsphere-jdbc 5.4.1 sample project: Failed to load driver class org.h2.Driver [shardingsphere]

gavinWithGu opened a new issue, #29386:
URL: https://github.com/apache/shardingsphere/issues/29386

   ## Question
   Hi team,
   
   I generate the sample project by project **shardingsphere-jdbc-example-generator**, and try to test with the project **readwrite-splitting--spring-boot-starter-jdbc--standalone--local**, but get the error logs below, seem like the project is not able to load the driver classes which was defined in the config.yaml
   
   ```
   Exception in thread "main" com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Failed to load driver class org.h2.Driver in either of HikariConfig class loader or Thread context classloader
   	at com.zaxxer.hikari.pool.HikariPool.throwPoolInitializationException(HikariPool.java:589)
   	at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:575)
   	at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
   	at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
   	at org.apache.shardingsphere.example.readwritesplitting.spring.boot.starter.jdbc.repository.OrderRepository.dropTable(OrderRepository.java:51)
   	at org.apache.shardingsphere.example.readwritesplitting.spring.boot.starter.jdbc.service.ExampleService.cleanEnvironment(ExampleService.java:139)
   	at org.apache.shardingsphere.example.readwritesplitting.spring.boot.starter.jdbc.service.ExampleService.run(ExampleService.java:58)
   	at org.apache.shardingsphere.example.readwritesplitting.spring.boot.starter.jdbc.ExampleMain.main(ExampleMain.java:33)
   Caused by: java.lang.RuntimeException: Failed to load driver class org.h2.Driver in either of HikariConfig class loader or Thread context classloader
   	at com.zaxxer.hikari.HikariConfig.setDriverClassName(HikariConfig.java:485)
   	at org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepository.init(JDBCRepository.java:61)
   	at org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader.findService(TypedSPILoader.java:61)
   	at org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader.getService(TypedSPILoader.java:110)
   	at org.apache.shardingsphere.mode.manager.standalone.NewStandaloneContextManagerBuilder.build(NewStandaloneContextManagerBuilder.java:48)
   	at org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource.createContextManager(ShardingSphereDataSource.java:78)
   	at org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource.<init>(ShardingSphereDataSource.java:66)
   	at org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory.createDataSource(ShardingSphereDataSourceFactory.java:95)
   	at org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory.createDataSource(YamlShardingSphereDataSourceFactory.java:167)
   	at org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory.createDataSource(YamlShardingSphereDataSourceFactory.java:102)
   	at org.apache.shardingsphere.driver.jdbc.core.driver.DriverDataSourceCache.createDataSource(DriverDataSourceCache.java:52)
   	at org.apache.shardingsphere.driver.jdbc.core.driver.DriverDataSourceCache.lambda$get$0(DriverDataSourceCache.java:46)
   	at org.apache.shardingsphere.driver.jdbc.core.driver.DriverDataSourceCache$$Lambda$249/1416415959.apply(Unknown Source)
   	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(Unknown Source)
   	at org.apache.shardingsphere.driver.jdbc.core.driver.DriverDataSourceCache.get(DriverDataSourceCache.java:46)
   	at org.apache.shardingsphere.driver.ShardingSphereDriver.connect(ShardingSphereDriver.java:53)
   	at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:121)
   	at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354)
   	at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202)
   	at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473)
   	at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554)
   	... 6 more
   ```
   
   **application.yaml**
   ```
   server:
     port: 8082
   spring:
     datasource:
       driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
       url: jdbc:shardingsphere:classpath:config.yaml
   ```
   
   **config.yaml**
   ```
   
   mode:
     type: Standalone
     repository:
       type: JDBC
       props:
         path: demo
   
   dataSources:
     ds_0:
       dataSourceClassName: com.zaxxer.hikari.HikariDataSource
       driverClassName: com.mysql.jdbc.Driver
       jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true
       username: root
       password: ***
       maxPoolSize: 10
     ds_1:
       dataSourceClassName: com.zaxxer.hikari.HikariDataSource
       driverClassName: com.mysql.jdbc.Driver
       jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true
       username: root
       password: ***
       maxPoolSize: 10
     ds_2:
       dataSourceClassName: com.zaxxer.hikari.HikariDataSource
       driverClassName: com.mysql.jdbc.Driver
       jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_2?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true
       username: root
       password: ***
       maxPoolSize: 10
   
   rules:
   - !READWRITE_SPLITTING
     dataSources:
       readwrite_ds:
         writeDataSourceName: ds_0
         readDataSourceNames:
           - ds_1
           - ds_2
         loadBalancerName: round_robin
     loadBalancers:
       round_robin:
         type: ROUND_ROBIN
   
   props:
     sql-show: true
   
   ```
   
   **pom.xml**
   ```
   
       <modelVersion>4.0.0</modelVersion>
       <groupId>org.apache.shardingsphere.example</groupId>
       <artifactId>readwrite-splitting--spring-boot-starter-jdbc--standalone--local</artifactId>
       <version>5.4.1</version>
       <name>${project.artifactId}</name>
       
       <dependencies>
           <dependency>
               <groupId>org.apache.shardingsphere</groupId>
               <artifactId>shardingsphere-jdbc-core</artifactId>
               <version>${project.version}</version>
           </dependency>
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-jdbc</artifactId>
               <version>2.2.0.RELEASE</version>
               <exclusions>
                   <exclusion>
                       <artifactId>snakeyaml</artifactId>
                       <groupId>org.yaml</groupId>
                   </exclusion>
               </exclusions>
           </dependency>
           <dependency>
               <artifactId>snakeyaml</artifactId>
               <groupId>org.yaml</groupId>
               <version>1.33</version>
           </dependency>
           
           <dependency>
               <groupId>org.projectlombok</groupId>
               <artifactId>lombok</artifactId>
               <version>1.18.20</version>
               <scope>provided</scope>
           </dependency>
           <dependency>
               <groupId>com.zaxxer</groupId>
               <artifactId>HikariCP</artifactId>
               <version>3.4.2</version>
           </dependency>
           <dependency>
               <groupId>com.mysql</groupId>
               <artifactId>mysql-connector-j</artifactId>
               <version>8.0.31</version>
           </dependency>
           
           <dependency>
               <groupId>org.slf4j</groupId>
               <artifactId>slf4j-api</artifactId>
               <version>1.7.7</version>
           </dependency>
           <dependency>
               <groupId>ch.qos.logback</groupId>
               <artifactId>logback-classic</artifactId>
               <version>1.2.10</version>
           </dependency>
       </dependencies>
   ```
   
   Cloud you please check the error and guide me how to slove it?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Question for shardingsphere-jdbc 5.4.1 sample project: Failed to load driver class org.h2.Driver [shardingsphere]

Posted by "comecny (via GitHub)" <gi...@apache.org>.
comecny commented on issue #29386:
URL: https://github.com/apache/shardingsphere/issues/29386#issuecomment-1854269971

   When referencing shardingsphere-example-generator, it may include the parent dependency shardingsphere-examples. In the shardingsphere-examples parent dependency, an H2 data source is introduced, which may lead to this error.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Question for shardingsphere-jdbc 5.4.1 sample project: Failed to load driver class org.h2.Driver [shardingsphere]

Posted by "gavinWithGu (via GitHub)" <gi...@apache.org>.
gavinWithGu closed issue #29386: Question for shardingsphere-jdbc 5.4.1 sample project: Failed to load driver class org.h2.Driver 
URL: https://github.com/apache/shardingsphere/issues/29386


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Question for shardingsphere-jdbc 5.4.1 sample project: Failed to load driver class org.h2.Driver [shardingsphere]

Posted by "comecny (via GitHub)" <gi...@apache.org>.
comecny commented on issue #29386:
URL: https://github.com/apache/shardingsphere/issues/29386#issuecomment-1854201949

   When you are referencing shardingsphere-example-generator, it is possible that it includes the parent dependency shardingsphere-examples. In the shardingsphere-examples parent dependency, an H2 data source is introduced, which may lead to dependency conflicts


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Question for shardingsphere-jdbc 5.4.1 sample project: Failed to load driver class org.h2.Driver [shardingsphere]

Posted by "gavinWithGu (via GitHub)" <gi...@apache.org>.
gavinWithGu commented on issue #29386:
URL: https://github.com/apache/shardingsphere/issues/29386#issuecomment-1881236553

   Hi @linghengqian 
   One more question, does shardingsphere support complex sql query, the table structure is as below:
   
    **table structure** 
   ```
   CREATE TABLE `operation_time` (
     `id` bigint NOT NULL AUTO_INCREMENT,
     `count` bigint DEFAULT NULL,
     `created_at` datetime NOT NULL,
     `expansion_valve` int NOT NULL,
     `power` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
     `thing_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
     `unit_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
     PRIMARY KEY (`id`)
   ) 
   ```
   
   **SQL Query**
   `SELECT COUNT ( `operationt0_` . `unit_id` ) AS `unit_id_count`,     `operationt0_` . `thing_id` AS `thing_id`,     `operationt0_` . `unit_id` AS `unit_id`     FROM `operation_time` `operationt0_`     WHERE `operationt0_` . `thing_id` = ?    AND ( `operationt0_` . `created_at` BETWEEN ? AND ? )     AND `operationt0_` . `power` = ?    GROUP BY `operationt0_` . `thing_id` , `operationt0_` . `unit_id``
   
   **thing_id** is the sharding_key, but the query contains **between and, group by** ,etc.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Question for shardingsphere-jdbc 5.4.1 sample project: Failed to load driver class org.h2.Driver [shardingsphere]

Posted by "linghengqian (via GitHub)" <gi...@apache.org>.
linghengqian commented on issue #29386:
URL: https://github.com/apache/shardingsphere/issues/29386#issuecomment-1869076572

   > > * Is this the error log you get when writing unit tests in the code generated inside the target folder? The current example module is not easy to debug directly.
   > > * It seems that you have added sensitive information. Is it inconvenient for you to rewrite the relevant information as h2database and share it as a separate project as git on github.com?
   > 
   > @linghengqian Thanks for the reply Since you mentioned that it is not easy to debug the code which was automaticlly generated, can you provide the way how we can get started with Shardingsphere functionality? currently what we need is the solution of read-write split + sharding of table
   
   - To debug specific code, you only need to modify the database connection information of the generated yaml, and then copy the generated sub-project code to another place to try debugging. All SQL executed by example assumes that the connection is MySQL, so you can use H2database in MySQL mode directly.
   
   - The unit tests at https://github.com/apache/shardingsphere/tree/master/test/native are basically modified from the example module. You can try it for reference.
   
   - It's hard for me to confirm what's wrong with the current issue without involving a separate project. Can you hide the database information and upload the project files to github.com as a separate git? This way I can debug via codespaces.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Question for shardingsphere-jdbc 5.4.1 sample project: Failed to load driver class org.h2.Driver [shardingsphere]

Posted by "gavinWithGu (via GitHub)" <gi...@apache.org>.
gavinWithGu commented on issue #29386:
URL: https://github.com/apache/shardingsphere/issues/29386#issuecomment-1869544777

   @linghengqian 
   Now I am using the old version of sharding-jdbc: 4.0.1. The table sharding function works fine, then I tried to config the file mentioned in the link below to support read-write split, but it did not take effect. The select query always goes to the writer node
   [user-manual_4.0.1](https://shardingsphere.apache.org/document/4.0.1/cn/user-manual/shardingsphere-jdbc/yaml-config/rules/readwrite-splitting/
   )
   
   **application-standardsharding.yml**
   ```
   spring:
     shardingsphere:
       datasource:
         names: db1, db2
         db1:
           type: com.zaxxer.hikari.HikariDataSource
           driver-class-name: com.mysql.cj.jdbc.Driver
           jdbc-url: jdbc:mysql://***/shardingsphere_1
           username: 123
           password: 123
         db2:
           type: com.zaxxer.hikari.HikariDataSource
           driver-class-name: com.mysql.cj.jdbc.Driver
           jdbc-url: jdbc:mysql://***/shardingsphere_2
           username: 123
           password: 123
       sharding:
         tables:
           student:
             actual-data-nodes: db1.tb_student_$->{1..3},db2.tb_student_$->{1..3}
             database-strategy:
               standard:
                 sharding-column: id
                 precise-algorithm-class-name: com.shardingsphere.sharding.jpa.algorithm.DatabasePreciseShardingAlgorithm
             table-strategy:
               standard:
                 sharding-column: id
                 precise-algorithm-class-name: com.shardingsphere.sharding.jpa.algorithm.TablePreciseShardingAlgorithm
       readwrite_splitting:
           dataSources:
             db:
               type: Static
               load-balancer-name: round_robin
               props:
                  write-data-source-name: db1
                  read-data-source-names: db2
           loadBalancers:
             round_robin:
               type: ROUND_ROBIN
   ```
   
   Github reporitory
   [sharding-jdbc-poc](https://github.com/gavinWithGu/sharding-jdbc-poc)
   
   Please check and provide the solution regarding how to enable the read-write split.
   
   Thanks a lot.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Question for shardingsphere-jdbc 5.4.1 sample project: Failed to load driver class org.h2.Driver [shardingsphere]

Posted by "linghengqian (via GitHub)" <gi...@apache.org>.
linghengqian commented on issue #29386:
URL: https://github.com/apache/shardingsphere/issues/29386#issuecomment-1884403064

   @gavinWithGu 
   
   - As for my late reply, the actual situation is that I didn't finish my Christmas vacation until next Saturday, and I forgot about this issue. 
   
   - I noticed that your example was using shardingsphere 4, which in disguise required me to read it for a longer time, because I joined the ShardingSphere community and became a committer long after ShardingSphere 5 was released, and I know almost nothing about the source code design of earlier versions. 😂
   
   - The parsing of complex SQL should be fine in most cases, but I hope you can open a new issue for that SQL. The fact that the Parser module can parse normally does not mean that the SQL can be used in all scenarios. I would still encourage writing a unit test via testcontainers-java.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Question for shardingsphere-jdbc 5.4.1 sample project: Failed to load driver class org.h2.Driver [shardingsphere]

Posted by "linghengqian (via GitHub)" <gi...@apache.org>.
linghengqian commented on issue #29386:
URL: https://github.com/apache/shardingsphere/issues/29386#issuecomment-1854480275

   - Is this the error log you get when writing unit tests in the code generated inside the target folder? The current example module is not easy to debug directly.  
   
   - It seems that you have added sensitive information. Is it inconvenient for you to rewrite the relevant information as h2database and share it as a separate project as git on github.com?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Question for shardingsphere-jdbc 5.4.1 sample project: Failed to load driver class org.h2.Driver [shardingsphere]

Posted by "gavinWithGu (via GitHub)" <gi...@apache.org>.
gavinWithGu commented on issue #29386:
URL: https://github.com/apache/shardingsphere/issues/29386#issuecomment-1868530075

   > * Is this the error log you get when writing unit tests in the code generated inside the target folder? The current example module is not easy to debug directly.
   > * It seems that you have added sensitive information. Is it inconvenient for you to rewrite the relevant information as h2database and share it as a separate project as git on github.com?
   
   @linghengqian  Thanks for the reply
   Since you mentioned that it is not easy to debug the code which was automaticlly generated, can you provide the way how we can get started with Shardingsphere functionality? currently what we need is the solution of read-write split + sharding of table


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org