EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hivetranse.transaction.jdbc]

COVERAGE SUMMARY FOR SOURCE FILE [ConnectionsRepositoryImpl.java]

nameclass, %method, %block, %line, %
ConnectionsRepositoryImpl.java100% (1/1)100% (6/6)85%  (103/121)83%  (35.8/43)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ConnectionsRepositoryImpl100% (1/1)100% (6/6)85%  (103/121)83%  (35.8/43)
closeConnection (String, Connection): SQLException 100% (1/1)36%  (4/11)40%  (2/5)
getConnection (String, DataSource): Connection 100% (1/1)74%  (20/27)67%  (6/9)
endAllConnections (boolean, boolean): void 100% (1/1)93%  (51/55)93%  (16.8/18)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
ConnectionsRepositoryImpl (): void 100% (1/1)100% (8/8)100% (2/2)
terminateConnection (String, Connection, boolean): SQLException 100% (1/1)100% (16/16)100% (8/8)

1//  Copyright 2004-2007 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14 
15package net.sourceforge.hivetranse.transaction.jdbc;
16 
17import java.sql.Connection;
18import java.sql.SQLException;
19import java.util.HashMap;
20import java.util.Iterator;
21import java.util.Map;
22 
23import javax.sql.DataSource;
24 
25import org.apache.commons.logging.Log;
26import org.apache.commons.logging.LogFactory;
27 
28/**
29 * Actual implementation class for <code>ConnectionsRepository</code>.
30 * Enlisted connections are stored in a <code>Map</code>.
31 *
32 * @author Jean-Francois Poilpret
33 */
34public class ConnectionsRepositoryImpl implements ConnectionsRepository
35{
36        static final private Log        _logger = LogFactory.getLog(ConnectionsRepositoryImpl.class);
37        
38        public Connection        getConnection(String id, DataSource ds)
39                throws SQLException
40        {
41                Connection cnx = _connections.get(id);
42                if (cnx == null)
43                {
44                        try
45                        {
46                                cnx = ds.getConnection();
47                                _connections.put(id, cnx);
48                        }
49                        catch (SQLException e)
50                        {
51                                _logger.warn("getConnection", e);
52                                throw e;
53                        }
54                }
55                return cnx;
56        }
57        
58        public void                endAllConnections(boolean close, boolean commit)
59                throws SQLException
60        {
61                SQLException exc = null;
62                SQLException e;
63                Iterator<Map.Entry<String, Connection>> i = _connections.entrySet().iterator();
64                while (i.hasNext())
65                {
66                        Map.Entry<String, Connection> entry = i.next();
67                        String id = entry.getKey();
68                        Connection cnx = entry.getValue();
69                        e = terminateConnection(id, cnx, commit);
70                        if (e != null && exc == null)
71                        {
72                                // We keep track only of the first exception occurring
73                                exc = e;
74                        }
75                        if (close)
76                        {
77                                e = closeConnection(id, cnx);
78                                if (e != null && exc == null)
79                                {
80                                        // We keep track only of the first exception occurring
81                                        exc = e;
82                                }
83                                i.remove();
84                        }
85                }
86                if (exc != null)
87                {
88                        throw exc;
89                }
90        }
91        
92        protected SQLException        terminateConnection(String id, Connection cnx, boolean commit)
93        {
94                try
95                {
96                        if (commit)
97                        {
98                                cnx.commit();
99                        }
100                        else
101                        {
102                                cnx.rollback();
103                        }
104                        return null;
105                }
106                catch (SQLException e)
107                {
108                        _logger.warn("terminateConnection", e);
109                        return e;
110                }
111        }
112 
113        protected SQLException        closeConnection(String id, Connection cnx)
114        {
115                try
116                {
117                        cnx.close();
118                        return null;
119                }
120                catch (SQLException e)
121                {
122                        _logger.warn("closeConnection", e);
123                        return e;
124                }
125        }
126 
127        private final Map<String, Connection>        _connections = new HashMap<String, Connection>();
128}

[all classes][net.sourceforge.hivetranse.transaction.jdbc]
EMMA 2.0.5312 (C) Vladimir Roubtsov