|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | + |
| 20 | +package org.apache.skywalking.apm.collector.client.shardingjdbc; |
| 21 | + |
| 22 | +import java.sql.Connection; |
| 23 | +import java.sql.PreparedStatement; |
| 24 | +import java.sql.ResultSet; |
| 25 | +import java.sql.SQLException; |
| 26 | +import java.sql.Statement; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Properties; |
| 30 | + |
| 31 | +import javax.sql.DataSource; |
| 32 | + |
| 33 | +import org.apache.commons.dbcp.BasicDataSource; |
| 34 | +import org.apache.skywalking.apm.collector.client.Client; |
| 35 | +import org.slf4j.Logger; |
| 36 | +import org.slf4j.LoggerFactory; |
| 37 | + |
| 38 | +import io.shardingjdbc.core.api.ShardingDataSourceFactory; |
| 39 | +import io.shardingjdbc.core.api.config.ShardingRuleConfiguration; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author linjiaqi |
| 43 | + */ |
| 44 | +public class ShardingjdbcClient implements Client { |
| 45 | + |
| 46 | + private final Logger logger = LoggerFactory.getLogger(ShardingjdbcClient.class); |
| 47 | + |
| 48 | + private Map<String, ShardingjdbcClientConfig> shardingjdbcClientConfig; |
| 49 | + |
| 50 | + private ShardingRuleConfiguration shardingRuleConfiguration; |
| 51 | + |
| 52 | + private Map<String, DataSource> shardingDataSource = new HashMap<String, DataSource>(); |
| 53 | + |
| 54 | + private DataSource dataSource; |
| 55 | + |
| 56 | + public ShardingjdbcClient(Map<String, ShardingjdbcClientConfig> shardingjdbcClientConfig, ShardingRuleConfiguration shardingRuleConfiguration) { |
| 57 | + this.shardingjdbcClientConfig = shardingjdbcClientConfig; |
| 58 | + this.shardingRuleConfiguration = shardingRuleConfiguration; |
| 59 | + } |
| 60 | + |
| 61 | + @Override public void initialize() throws ShardingjdbcClientException { |
| 62 | + try { |
| 63 | + shardingjdbcClientConfig.forEach((key, value) -> { |
| 64 | + BasicDataSource basicDataSource = new BasicDataSource(); |
| 65 | + basicDataSource.setDriverClassName(value.getDriverClass()); |
| 66 | + basicDataSource.setUrl(value.getUrl()); |
| 67 | + basicDataSource.setUsername(value.getUserName()); |
| 68 | + basicDataSource.setPassword(value.getPassword()); |
| 69 | + shardingDataSource.put(key, basicDataSource); |
| 70 | + logger.info("add sharding datasource: {}, url: {}", key, value.getUrl()); |
| 71 | + }); |
| 72 | + dataSource = ShardingDataSourceFactory.createDataSource(shardingDataSource, shardingRuleConfiguration, |
| 73 | + new HashMap<String, Object>(), new Properties()); |
| 74 | + } catch (Exception e) { |
| 75 | + logger.error("case the exception is 'Cannot load JDBC driver class', please add the driver mysql-connector-java-5.1.36.jar to collector-libs manual"); |
| 76 | + throw new ShardingjdbcClientException(e.getMessage(), e); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override public void shutdown() { |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + public Connection getConnection() throws SQLException { |
| 85 | + return dataSource.getConnection(); |
| 86 | + } |
| 87 | + |
| 88 | + public void execute(String sql) throws ShardingjdbcClientException { |
| 89 | + Connection conn = null; |
| 90 | + Statement statement = null; |
| 91 | + try { |
| 92 | + conn = getConnection(); |
| 93 | + statement = conn.createStatement(); |
| 94 | + statement.execute(sql); |
| 95 | + } catch (SQLException e) { |
| 96 | + throw new ShardingjdbcClientException(e.getMessage(), e); |
| 97 | + } finally { |
| 98 | + try { |
| 99 | + if (statement != null) { |
| 100 | + statement.close(); |
| 101 | + } |
| 102 | + if (conn != null) { |
| 103 | + conn.close(); |
| 104 | + } |
| 105 | + } catch (SQLException e) { |
| 106 | + throw new ShardingjdbcClientException(e.getMessage(), e); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public ResultSet executeQuery(String sql, Object[] params) throws ShardingjdbcClientException { |
| 112 | + logger.debug("execute query with result: {}", sql); |
| 113 | + ResultSet rs; |
| 114 | + PreparedStatement statement; |
| 115 | + try { |
| 116 | + statement = getConnection().prepareStatement(sql); |
| 117 | + if (params != null) { |
| 118 | + for (int i = 0; i < params.length; i++) { |
| 119 | + statement.setObject(i + 1, params[i]); |
| 120 | + } |
| 121 | + } |
| 122 | + rs = statement.executeQuery(); |
| 123 | + } catch (SQLException e) { |
| 124 | + throw new ShardingjdbcClientException(e.getMessage(), e); |
| 125 | + } |
| 126 | + return rs; |
| 127 | + } |
| 128 | + |
| 129 | + public boolean execute(String sql, Object[] params) throws ShardingjdbcClientException { |
| 130 | + logger.debug("execute insert/update/delete: {}", sql); |
| 131 | + boolean flag; |
| 132 | + Connection conn = null; |
| 133 | + PreparedStatement statement = null; |
| 134 | + try { |
| 135 | + conn = getConnection(); |
| 136 | + conn.setAutoCommit(true); |
| 137 | + statement = conn.prepareStatement(sql); |
| 138 | + if (params != null) { |
| 139 | + for (int i = 0; i < params.length; i++) { |
| 140 | + statement.setObject(i + 1, params[i]); |
| 141 | + } |
| 142 | + } |
| 143 | + flag = statement.execute(); |
| 144 | + } catch (SQLException e) { |
| 145 | + throw new ShardingjdbcClientException(e.getMessage(), e); |
| 146 | + } finally { |
| 147 | + try { |
| 148 | + if (statement != null) { |
| 149 | + statement.close(); |
| 150 | + } |
| 151 | + if (conn != null) { |
| 152 | + conn.close(); |
| 153 | + } |
| 154 | + } catch (SQLException e) { |
| 155 | + throw new ShardingjdbcClientException(e.getMessage(), e); |
| 156 | + } |
| 157 | + } |
| 158 | + return flag; |
| 159 | + } |
| 160 | +} |
0 commit comments