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