01: ///////////////////////////////
02: // Makumba, Makumba tag library
03: // Copyright (C) 2000-2003 http://www.makumba.org
04: //
05: // This library is free software; you can redistribute it and/or
06: // modify it under the terms of the GNU Lesser General Public
07: // License as published by the Free Software Foundation; either
08: // version 2.1 of the License, or (at your option) any later version.
09: //
10: // This library is distributed in the hope that it will be useful,
11: // but WITHOUT ANY WARRANTY; without even the implied warranty of
12: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: // Lesser General Public License for more details.
14: //
15: // You should have received a copy of the GNU Lesser General Public
16: // License along with this library; if not, write to the Free Software
17: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: //
19: // -------------
20: // $Id: DbConnectionProvider.java 2119 2007-11-22 01:12:14Z cristian_bogdan $
21: // $Name$
22: /////////////////////////////////////
23:
24: package org.makumba.commons;
25:
26: import java.util.HashMap;
27: import java.util.Iterator;
28: import java.util.Map;
29:
30: import org.makumba.Transaction;
31: import org.makumba.providers.TransactionProvider;
32:
33: /**
34: * A group of database connections, at most one per database name. They can be closed all at a time. This object is not
35: * thread-safe. The typical use is database accesses made by a JSP page (which take place all in the same thread of the
36: * servlet engine).
37: *
38: * @author Cristian Bogdan
39: */
40: public class DbConnectionProvider {
41:
42: private TransactionProvider tp = new TransactionProvider();
43:
44: Map<String, Transaction> connections = new HashMap<String, Transaction>(
45: 7);
46:
47: public Transaction getConnectionTo(String dbname) {
48: Transaction db = (Transaction) connections.get(dbname);
49: if (db == null)
50: connections.put(dbname, db = tp.getConnectionTo(dbname));
51: return db;
52: }
53:
54: /** Close all connections. */
55: public void close() {
56: for (Iterator i = connections.values().iterator(); i.hasNext();)
57: ((Transaction) i.next()).close();
58: connections.clear();
59: }
60:
61: protected void finalize() {
62: close();
63: }
64:
65: }
|