1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.db;
15
16 import java.sql.Connection;
17 import java.sql.SQLException;
18
19 import javax.sql.DataSource;
20
21 import ch.qos.logback.core.db.dialect.SQLDialectCode;
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class DataSourceConnectionSource extends ConnectionSourceBase {
36
37 private DataSource dataSource;
38
39 @Override
40 public void start() {
41 if (dataSource == null) {
42 addWarn("WARNING: No data source specified");
43 } else {
44 Connection connection = null;
45 try {
46 connection = getConnection();
47 } catch (SQLException se) {
48 addWarn("Could not get a connection to discover the dialect to use.",
49 se);
50 }
51 if (connection != null) {
52 discoverConnnectionProperties();
53 }
54 if (!supportsGetGeneratedKeys()
55 && getSQLDialectCode() == SQLDialectCode.UNKNOWN_DIALECT) {
56 addWarn("Connection does not support GetGeneratedKey method and could not discover the dialect.");
57 }
58 }
59 super.start();
60 }
61
62
63
64
65 public Connection getConnection() throws SQLException {
66 if (dataSource == null) {
67 addError("WARNING: No data source specified");
68 return null;
69 }
70
71 if (getUser() == null) {
72 return dataSource.getConnection();
73 } else {
74 return dataSource.getConnection(getUser(), getPassword());
75 }
76 }
77
78 public DataSource getDataSource() {
79 return dataSource;
80 }
81
82 public void setDataSource(DataSource dataSource) {
83 this.dataSource = dataSource;
84 }
85 }