1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.pool.service;
35
36 import java.sql.Connection;
37 import java.sql.SQLException;
38
39 import java.util.Map;
40
41 import javax.naming.Context;
42 import javax.naming.InitialContext;
43
44 import javax.sql.DataSource;
45
46 import org.apache.logging.log4j.Logger;
47
48
49
50
51 public class TomcatConnectionService implements ConnectionService
52 {
53 private DataSource _ds;
54 private String _strPoolName;
55 private Logger _logger;
56
57
58
59
60 @Override
61 public Connection getConnection( )
62 {
63 Connection conn = null;
64
65 try
66 {
67 if ( _ds != null )
68 {
69 conn = _ds.getConnection( );
70 _logger.debug( "The connexion is get" );
71
72 }
73 }
74 catch( Exception e )
75 {
76 _logger.error( "Erreur when getting the connexion with the pool : {}", getPoolName( ), e );
77 }
78
79 return conn;
80 }
81
82
83
84
85 @Override
86 public void freeConnection( Connection conn )
87 {
88 try
89 {
90 conn.close( );
91 }
92 catch( SQLException e )
93 {
94 _logger.error( "SQL error when releasing the connexion with the pool : {}", getPoolName( ), e );
95 }
96 catch( Exception e )
97 {
98 _logger.error( "Error while releasing the connexion with the pool : {}", getPoolName( ), e );
99 }
100 }
101
102
103
104
105 @Override
106 public void init( Map<String, String> htParamsConnectionPool )
107 {
108 try
109 {
110 String strDs = htParamsConnectionPool.get( getPoolName( ) + ".ds" );
111 Context ctx = new InitialContext( );
112
113 _ds = (DataSource) ctx.lookup( "java:comp/env/" + strDs );
114 }
115 catch( Exception e )
116 {
117 _logger.error( "Error while initializing the pool {}", getPoolName( ), e );
118 }
119
120 _logger.info( "Initialization of the pool {}", getPoolName( ) );
121 }
122
123
124
125
126 @Override
127 public void setPoolName( String strPoolName )
128 {
129 _strPoolName = strPoolName;
130 }
131
132
133
134
135 @Override
136 public String getPoolName( )
137 {
138 return _strPoolName;
139 }
140
141
142
143
144
145
146
147 @Override
148 public void setLogger( Logger logger )
149 {
150 _logger = logger;
151 }
152
153
154
155
156 @Override
157 public Logger getLogger( )
158 {
159 return _logger;
160 }
161
162
163
164
165 @Override
166 public void release( )
167 {
168
169 }
170
171
172
173
174 @Override
175 public int getCurrentConnections( )
176 {
177 return ConnectionService.INFO_NOT_AVAILABLE;
178 }
179
180
181
182
183 @Override
184 public int getMaxConnections( )
185 {
186 return ConnectionService.INFO_NOT_AVAILABLE;
187 }
188
189
190
191
192 @Override
193 public String getPoolProvider( )
194 {
195 return "Tomcat";
196 }
197
198
199
200
201 @Override
202 public DataSource getDataSource( )
203 {
204 return _ds;
205 }
206 }