1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.guice.datasource.builtin;
17
18 import jakarta.inject.Inject;
19 import jakarta.inject.Named;
20 import jakarta.inject.Provider;
21
22 import java.util.Properties;
23
24 import javax.sql.DataSource;
25
26 import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
27
28
29
30
31 public final class UnpooledDataSourceProvider implements Provider<DataSource> {
32
33
34
35
36 private final UnpooledDataSource unpooledDataSource;
37
38
39
40
41
42
43
44
45
46
47
48 @Inject
49 public UnpooledDataSourceProvider(@Named("JDBC.driver") final String driver, @Named("JDBC.url") final String url,
50 @Named("JDBC.driverClassLoader") final ClassLoader driverClassLoader) {
51 unpooledDataSource = new UnpooledDataSource(driverClassLoader, driver, url, null, null);
52 }
53
54
55
56
57
58
59
60
61
62 @com.google.inject.Inject(optional = true)
63 public void setUser(@Named("JDBC.username") final String username) {
64 unpooledDataSource.setUsername(username);
65 }
66
67
68
69
70
71
72
73
74
75 @com.google.inject.Inject(optional = true)
76 public void setPassword(@Named("JDBC.password") final String password) {
77 unpooledDataSource.setPassword(password);
78 }
79
80
81
82
83
84
85
86 @com.google.inject.Inject(optional = true)
87 public void setAutoCommit(@Named("JDBC.autoCommit") final boolean autoCommit) {
88 unpooledDataSource.setAutoCommit(autoCommit);
89 }
90
91
92
93
94
95
96
97 @com.google.inject.Inject(optional = true)
98 public void setLoginTimeout(@Named("JDBC.loginTimeout") final int loginTimeout) {
99 unpooledDataSource.setLoginTimeout(loginTimeout);
100 }
101
102 @com.google.inject.Inject(optional = true)
103 public void setDriverProperties(@Named("JDBC.driverProperties") final Properties driverProperties) {
104 unpooledDataSource.setDriverProperties(driverProperties);
105 }
106
107 @Override
108 public DataSource get() {
109 return unpooledDataSource;
110 }
111
112 }