or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

datasource-configuration.mdindex.mdmonitoring-statistics.mdsecurity-filtering.mdsql-processing.md

index.mddocs/

0

# Druid

1

2

Druid is a high-performance JDBC connection pool and database monitoring library developed by Alibaba. It provides comprehensive database connectivity with built-in monitoring, SQL parsing, security features, and support for 50+ database types including MySQL, PostgreSQL, Oracle, SQL Server, and many others.

3

4

## Package Information

5

6

- **Package Name**: com.alibaba:druid

7

- **Package Type**: Maven

8

- **Language**: Java

9

- **Installation**: Add to Maven pom.xml:

10

11

```xml

12

<dependency>

13

<groupId>com.alibaba</groupId>

14

<artifactId>druid</artifactId>

15

<version>1.2.25</version>

16

</dependency>

17

```

18

19

## Core Imports

20

21

```java

22

// DataSource and connection pooling

23

import com.alibaba.druid.pool.DruidDataSource;

24

import com.alibaba.druid.pool.DruidDataSourceFactory;

25

import com.alibaba.druid.pool.DruidDataSourceStatManager;

26

import com.alibaba.druid.pool.DruidPooledConnection;

27

28

// SQL parsing and processing

29

import com.alibaba.druid.sql.SQLUtils;

30

import com.alibaba.druid.sql.SQLUtils.FormatOption;

31

import com.alibaba.druid.sql.ast.SQLStatement;

32

import com.alibaba.druid.sql.ast.SQLExpr;

33

import com.alibaba.druid.sql.parser.SQLParserFeature;

34

import com.alibaba.druid.DbType;

35

36

// Security and filtering

37

import com.alibaba.druid.wall.WallFilter;

38

import com.alibaba.druid.wall.WallConfig;

39

import com.alibaba.druid.filter.Filter;

40

41

// Statistics and monitoring

42

import com.alibaba.druid.stat.DruidStatService;

43

import com.alibaba.druid.stat.JdbcSqlStat;

44

import com.alibaba.druid.stat.JdbcDataSourceStat;

45

46

// Common exceptions

47

import com.alibaba.druid.pool.GetConnectionTimeoutException;

48

import com.alibaba.druid.pool.DataSourceClosedException;

49

import com.alibaba.druid.wall.WallSQLException;

50

import com.alibaba.druid.sql.parser.ParserException;

51

```

52

53

## Basic Usage

54

55

```java

56

import com.alibaba.druid.pool.DruidDataSource;

57

import java.sql.Connection;

58

import java.sql.SQLException;

59

60

// Create and configure DataSource

61

DruidDataSource dataSource = new DruidDataSource();

62

dataSource.setUrl("jdbc:mysql://localhost:3306/test");

63

dataSource.setUsername("root");

64

dataSource.setPassword("password");

65

dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");

66

67

// Configure connection pool

68

dataSource.setInitialSize(5);

69

dataSource.setMaxActive(10);

70

dataSource.setMinIdle(5);

71

dataSource.setMaxWait(60000);

72

73

// Get connection from pool

74

try (Connection conn = dataSource.getConnection()) {

75

// Use connection for database operations

76

System.out.println("Connected to database: " + conn.getMetaData().getDatabaseProductName());

77

} catch (SQLException e) {

78

e.printStackTrace();

79

}

80

81

// Close DataSource when done

82

dataSource.close();

83

```

84

85

## Architecture

86

87

Druid is organized into several key components:

88

89

- **Connection Pool**: High-performance JDBC connection pooling with configurable parameters

90

- **SQL Parser**: Complete SQL parsing engine supporting multiple database dialects

91

- **Filter System**: Extensible filter framework for intercepting JDBC operations

92

- **Security Framework**: SQL injection protection through WallFilter

93

- **Monitoring System**: Real-time statistics and performance monitoring

94

- **Database Dialects**: Support for 50+ database types with dialect-specific features

95

96

## Capabilities

97

98

### DataSource Configuration and Connection Pooling

99

100

Core functionality for creating and managing JDBC connection pools with extensive configuration options.

101

102

```java { .api }

103

// Main DataSource implementation

104

class DruidDataSource extends DruidAbstractDataSource implements DataSource, DataSourceProxy, Serializable {

105

public DruidDataSource();

106

public DruidDataSource(boolean fairLock);

107

108

// Configuration methods

109

public void setUrl(String jdbcUrl);

110

public void setUsername(String username);

111

public void setPassword(String password);

112

public void setDriverClassName(String driverClassName);

113

public void setInitialSize(int initialSize);

114

public void setMaxActive(int maxActive);

115

public void setMinIdle(int minIdle);

116

public void setMaxWait(long maxWaitMillis);

117

118

// Connection management

119

public Connection getConnection() throws SQLException;

120

public Connection getConnection(String username, String password) throws SQLException;

121

public void close();

122

}

123

124

// Factory for creating DataSource from Properties

125

class DruidDataSourceFactory {

126

public static DataSource createDataSource(Properties properties) throws Exception;

127

public static DataSource createDataSource(Map<String, Object> properties) throws Exception;

128

}

129

```

130

131

[DataSource Configuration](./datasource-configuration.md)

132

133

### SQL Processing and Parsing

134

135

Comprehensive SQL parsing engine supporting multiple database dialects with AST manipulation capabilities.

136

137

```java { .api }

138

// Primary SQL utilities

139

class SQLUtils {

140

// Parsing methods

141

public static List<SQLStatement> parseStatements(String sql, String dbType, SQLParserFeature... features);

142

public static SQLStatement parseSingleStatement(String sql, String dbType, SQLParserFeature... features);

143

public static SQLStatement parseSingleStatement(String sql, String dbType, boolean keepComments);

144

145

// Formatting methods

146

public static String format(String sql, String dbType);

147

public static String format(String sql, String dbType, SQLUtils.FormatOption... options);

148

public static String toSQLString(SQLObject sqlObject, String dbType);

149

150

// Validation methods

151

public static boolean isValidName(String name);

152

}

153

154

// Database type enumeration supporting 50+ database types

155

enum DbType {

156

// Major relational databases

157

mysql, mariadb, oracle, postgresql, sqlserver,

158

// Cloud and big data platforms

159

clickhouse, presto, hive, spark, snowflake, bigquery,

160

// Other databases

161

h2, db2, sqlite, derby, hsqldb,

162

// Specialized databases

163

oceanbase_oracle, polardb, tidb, drds,

164

// ... and 30+ more database types

165

}

166

```

167

168

[SQL Processing](./sql-processing.md)

169

170

### Security and SQL Injection Protection

171

172

Advanced security filtering system with SQL injection protection and configurable security policies.

173

174

```java { .api }

175

// Main security filter

176

class WallFilter extends FilterEventAdapter implements Filter {

177

public WallFilter();

178

public WallFilter(WallProvider provider);

179

180

public void setConfig(WallConfig config);

181

public WallConfig getConfig();

182

public void setDbType(String dbType);

183

}

184

185

// Security configuration

186

class WallConfig {

187

public WallConfig();

188

public WallConfig(String dbType);

189

190

public void setSelectAllow(boolean selectAllow);

191

public void setDeleteAllow(boolean deleteAllow);

192

public void setUpdateAllow(boolean updateAllow);

193

public void setInsertAllow(boolean insertAllow);

194

public void setMultiStatementAllow(boolean multiStatementAllow);

195

}

196

```

197

198

[Security Filtering](./security-filtering.md)

199

200

### Monitoring and Statistics

201

202

Real-time performance monitoring and statistics collection for database operations.

203

204

```java { .api }

205

// Main statistics service

206

class DruidStatService {

207

public static DruidStatService getInstance();

208

209

public Object getDataSourceStatDataList();

210

public Object getSqlStatDataList(Integer dataSourceId);

211

public Object getActiveConnectionStackTraceList();

212

public void resetAll();

213

public void logAndResetDataSource();

214

}

215

216

// DataSource statistics management

217

class DruidDataSourceStatManager {

218

public static DruidDataSourceStatManager getInstance();

219

220

public Set<DruidDataSourceStatValue> getDataSourceList();

221

public DruidDataSourceStatValue getDataSourceStatValue(Integer identity);

222

}

223

```

224

225

[Monitoring and Statistics](./monitoring-statistics.md)

226

227

## Exception Types

228

229

```java { .api }

230

// Connection pool exceptions

231

class GetConnectionTimeoutException extends SQLException {

232

public GetConnectionTimeoutException(String message);

233

public GetConnectionTimeoutException(String message, Throwable cause);

234

}

235

236

class DataSourceClosedException extends SQLException {

237

public DataSourceClosedException(String message);

238

}

239

240

// Security exceptions

241

class WallSQLException extends SQLException {

242

public WallSQLException(String message);

243

public WallSQLException(String message, Throwable cause);

244

}

245

246

// SQL processing exceptions

247

class ParserException extends RuntimeException {

248

public ParserException(String message);

249

public ParserException(String message, Throwable cause);

250

public int getLine();

251

public int getColumn();

252

}

253

254

class SQLParseException extends ParserException {

255

public SQLParseException(String message);

256

public SQLParseException(String message, Throwable cause);

257

}

258

259

// Core runtime exceptions

260

class DruidRuntimeException extends RuntimeException {

261

public DruidRuntimeException(String message);

262

public DruidRuntimeException(String message, Throwable cause);

263

}

264

265

class FastsqlException extends RuntimeException {

266

public FastsqlException(String message);

267

public FastsqlException(String message, Throwable cause);

268

}

269

```

270

271

## Database Support

272

273

Druid supports 50+ database types with dialect-specific parsing and features:

274

275

- **Relational**: MySQL, PostgreSQL, Oracle, SQL Server, H2, Derby, HSQLDB, SQLite, DB2

276

- **Big Data**: Hive, Spark SQL, Presto, ClickHouse, Impala, Kylin

277

- **Cloud**: Amazon Redshift, Google BigQuery, Snowflake

278

- **NoSQL**: Elasticsearch (SQL interface)

279

- **And many more**: Phoenix, Calcite, Drill, Greenplum, etc.

280

281

## Integration Support

282

283

Druid provides integration support for popular Java frameworks:

284

285

- **Spring Framework**: DruidLobHandler, MBean integration

286

- **Jakarta EE**: Servlet API compatibility

287

- **Hibernate**: Connection provider integration

288

- **iBatis/MyBatis**: DataSource configuration

289

- **Quartz**: Job scheduling integration