KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > distrans > persistence > XAPoolCache


1 /*
2   Copyright (C) 2001-2003 Lionel Seinturier <Lionel.Seinturier@lip6.fr>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.distrans.persistence;
19
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
26 import org.enhydra.jdbc.standard.StandardXADataSource;
27
28 /**
29  * This class implements a cache of connections
30  * towards multiple XADataSource.
31  * Contrary to a simple pool that manages multiple connections
32  * towards a single XADataSource, this cache manages connections
33  * for each XADataSource registered in the cache.
34  * Hence, this is a pool of pools.
35  *
36  * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr>
37  * @version 1.0
38  */

39 public class XAPoolCache {
40
41     /**
42      * Cache of connections:
43      * - keys are StandardXADataSource objects
44      * - values are Connection objects
45      */

46     private static Map JavaDoc pools = new HashMap JavaDoc();
47     
48     /** Size of each pool. */
49     private final static int POOL_SIZE = 4;
50     
51     
52     /**
53      * Get a connection for a XADataSource.
54      * Either return the reference towards an already
55      * existing connection, or create a new one.
56      *
57      * @param ds the XADataSource instance
58      * @return a SQL connection
59      */

60     public static Connection JavaDoc getConnection( StandardXADataSource ds )
61         throws SQLException JavaDoc {
62
63         /** Check whether the connection is pooled and still open. */
64         Connection JavaDoc connection = (Connection JavaDoc) pools.get(ds);
65         if ( connection!= null && !connection.isClosed() ) {
66             return connection;
67         }
68         
69         StandardXAPoolDataSource pool =
70             new StandardXAPoolDataSource(POOL_SIZE);
71         pool.setUser( ds.getUser() );
72         pool.setPassword( ds.getPassword() );
73         pool.setTransactionManager( ds.getTransactionManager() );
74         pool.setDataSource(ds);
75         
76         connection = pool.getConnection();
77         pools.put(ds,connection);
78
79         return connection;
80     }
81     
82 }
83
Popular Tags