File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\table\TableLinkConnection.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.table;
8
9 import java.sql.Connection;
10 import java.sql.SQLException;
11 import java.util.HashMap;
12 import org.h2.message.DbException;
13 import org.h2.util.JdbcUtils;
14 import org.h2.util.StringUtils;
15 import org.h2.util.Utils;
16
17 /**
18 * A connection for a linked table. The same connection may be used for multiple
19 * tables, that means a connection may be shared.
20 */
21 public class TableLinkConnection {
22
23 /**
24 * The map where the link is kept.
25 */
26 private final HashMap<TableLinkConnection, TableLinkConnection> map;
27
28 /**
29 * The connection information.
30 */
31 private final String driver, url, user, password;
32
33 /**
34 * The database connection.
35 */
36 private Connection conn;
37
38 /**
39 * How many times the connection is used.
40 */
41 private int useCounter;
42
43 private TableLinkConnection(
44 HashMap<TableLinkConnection, TableLinkConnection> map,
45 String driver, String url, String user, String password) {
46 this.map = map;
47 this.driver = driver;
48 this.url = url;
49 this.user = user;
50 this.password = password;
51 }
52
53 /**
54 * Open a new connection.
55 *
56 * @param map the map where the connection should be stored
57 * (if shared connections are enabled).
58 * @param driver the JDBC driver class name
59 * @param url the database URL
60 * @param user the user name
61 * @param password the password
62 * @param shareLinkedConnections if connections should be shared
63 * @return a connection
64 */
65 public static TableLinkConnection open(
66 HashMap<TableLinkConnection, TableLinkConnection> map,
67 String driver, String url, String user, String password, boolean shareLinkedConnections) {
68 TableLinkConnection t = new TableLinkConnection(map, driver, url, user, password);
69 if (!shareLinkedConnections) {
70 t.open();
71 return t;
72 }
73 synchronized (map) {
74 TableLinkConnection result;
75 result = map.get(t);
76 if (result == null) {
77 synchronized (t) {
78 t.open();
79 }
80 // put the connection in the map after is has been opened,
81 // so we know it works
82 map.put(t, t);
83 result = t;
84 }
85 synchronized (result) {
86 result.useCounter++;
87 }
88 return result;
89 }
90 }
91
92 private void open() {
93 try {
94 conn = JdbcUtils.getConnection(driver, url, user, password);
95 } catch (SQLException e) {
96 throw DbException.convert(e);
97 }
98 }
99
100 public int hashCode() {
101 return Utils.hashCode(driver)
102 ^ Utils.hashCode(url)
103 ^ Utils.hashCode(user)
104 ^ Utils.hashCode(password);
105 }
106
107 public boolean equals(Object o) {
108 if (o instanceof TableLinkConnection) {
109 TableLinkConnection other = (TableLinkConnection) o;
110 return StringUtils.equals(driver, other.driver)
111 && StringUtils.equals(url, other.url)
112 && StringUtils.equals(user, other.user)
113 && StringUtils.equals(password, other.password);
114 }
115 return false;
116 }
117
118 /**
119 * Get the connection.
120 * This method and methods on the statement must be
121 * synchronized on this object.
122 *
123 * @return the connection
124 */
125 Connection getConnection() {
126 return conn;
127 }
128
129 /**
130 * Closes the connection if this is the last link to it.
131 *
132 * @param force if the connection needs to be closed even if it is still
133 * used elsewhere (for example, because the connection is broken)
134 */
135 synchronized void close(boolean force) {
136 if (--useCounter <= 0 || force) {
137 JdbcUtils.closeSilently(conn);
138 synchronized (map) {
139 map.remove(this);
140 }
141 }
142 }
143
144 }
145