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.portal.service.database;
35
36 import fr.paris.lutece.portal.service.init.LuteceInitException;
37 import fr.paris.lutece.portal.service.util.AppException;
38 import fr.paris.lutece.portal.service.util.AppPathService;
39 import fr.paris.lutece.util.ReferenceList;
40 import fr.paris.lutece.util.pool.PoolManager;
41 import fr.paris.lutece.util.pool.service.ConnectionService;
42
43 import java.io.InputStream;
44
45 import java.sql.Connection;
46
47
48
49
50
51 public final class AppConnectionService
52 {
53
54
55 public static final String NO_POOL_DEFINED = "none";
56 public static final String DEFAULT_POOL_NAME = "portal";
57 private static PoolManager _poolManager;
58 private static PluginConnectionService _connectionService;
59
60
61
62
63 private AppConnectionService( )
64 {
65 }
66
67
68
69
70
71
72
73
74
75 public static void init( String strConfigPath, String strConfigFilename, String strPoolName )
76 throws LuteceInitException
77 {
78 try
79 {
80 InputStream is = AppPathService.getResourceAsStream( strConfigPath, strConfigFilename );
81 _poolManager = PoolManager.getInstance( is );
82 _connectionService = new PluginConnectionService( strPoolName );
83 is.close( );
84 }
85 catch ( Exception e )
86 {
87 throw new LuteceInitException( "Error initializing pool : " + e.getMessage( ), e );
88 }
89 }
90
91
92
93
94
95
96 public static Connection getConnection( )
97 {
98 if ( _poolManager == null )
99 {
100 throw new RuntimeException( "* Erreur * getConnection : Le pool de connexion n'est pas initialise !" );
101 }
102
103 Connection conn = _connectionService.getConnection( );
104
105 return conn;
106 }
107
108
109
110
111
112
113 public static void freeConnection( Connection conn )
114 {
115 _connectionService.freeConnection( conn );
116 }
117
118
119
120
121 public static void releasePool( )
122 {
123 _poolManager.release( );
124 }
125
126
127
128
129
130
131 public static PoolManager getPoolManager( )
132 {
133 if ( _poolManager == null )
134 {
135 throw new AppException( "PoolManager was not initialized !" );
136 }
137
138 return _poolManager;
139 }
140
141
142
143
144
145
146 public static void getPoolList( ReferenceList list )
147 {
148 for ( ConnectionService cs : _poolManager.getPools( ) )
149 {
150 list.addItem( cs.getPoolName( ), cs.getPoolName( ) );
151 }
152 }
153
154
155
156
157
158 public static PluginConnectionService getDefaultConnectionService( )
159 {
160 return _connectionService;
161 }
162 }