1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.connection; 31 32 import com.sun.jdo.api.persistence.support.Transaction; 33 import com.sun.jdo.spi.persistence.utility.I18NHelper; 34 import com.sun.jdo.spi.persistence.utility.DoubleLinkedList; 35 import com.sun.jdo.spi.persistence.utility.Linkable; 36 import com.sun.jdo.spi.persistence.support.sqlstore.utility.StringScanner; 37 import com.sun.jdo.spi.persistence.utility.logging.Logger; 38 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperSQLStore; 39 40 41 import java.sql.Connection ; 42 import java.sql.DriverManager ; 43 import java.sql.SQLException ; 44 import java.util.Hashtable ; 45 import java.util.ResourceBundle ; 46 47 208 public class ConnectionManager { 209 213 private String driverName; 214 215 219 private transient String expandedDriverName; 220 221 225 private String url; 226 227 231 private transient String expandedUrl; 232 233 237 private String userName; 238 239 243 private transient String expandedUserName; 244 245 249 private String password; 250 251 255 private transient String expandedPassword; 256 257 261 private int minPool; 262 263 267 private int maxPool; 268 269 273 private transient int poolSize; 274 275 279 private transient boolean pooling; 280 281 285 transient DoubleLinkedList freeList; 286 287 291 transient DoubleLinkedList busyList; 292 293 298 private transient Hashtable xactConnections; 299 300 304 transient boolean shutDownPending; 305 306 310 private transient boolean connectionBlocking; 311 312 317 private int msInterval; 318 319 324 private int msWait; 325 326 private static final int DEFAULT_RETRY_INTERVAL = 1000; 331 332 336 private transient boolean initialized; 337 338 342 private int loginTimeout; 343 344 347 private transient ConnectionImpl freeConn = null; 348 349 350 353 private static Logger logger = LogHelperSQLStore.getLogger(); 354 355 358 private final static ResourceBundle messages = I18NHelper.loadBundle( 359 "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", ConnectionManager.class.getClassLoader()); 361 362 363 367 static final String SQL_SUCCESS = "00000"; 372 static final String SQL_WARNING = "01000"; 377 static final String SQL_CURSOR_OP = "01001"; 382 static final String SQL_DISCONNECT = "01002"; 387 static final String SQL_NULL_ELIM = "01003"; 392 static final String SQL_R_TRUNC = "01004"; 397 static final String SQL_INSUFF_ITEM = "01005"; 402 static final String SQL_NOT_REVOKED = "01006"; 407 static final String SQL_NOT_GRANTED = "01007"; 412 static final String SQL_ZERO_BIT_PAD = "01008"; 417 static final String SQL_COND_TOO_LONG = "01009"; 423 static final String SQL_QUERY_TOO_LONG = "0100A"; 429 static final String SQL_NO_DATA = "02000"; 434 static final String SQL_DYN_ERROR = "07000"; 439 static final String SQL_USING_NO_PARAM = "07001"; 445 static final String SQL_USING_NO_TARGET = "07002"; 451 static final String SQL_CURSOR_NOEXE = "07003"; 457 static final String SQL_USING_REQ = "07004"; 463 static final String SQL_PREP_NO_CURSOR = "07005"; 469 static final String SQL_RESTRIC_ATTR = "07006"; 475 static final String SQL_USING_RESULTS = "07007"; 481 static final String SQL_INVAL_DESC_CNT = "07008"; 486 static final String SQL_INVAL_DESC_IDX = "07009"; 491 static final String SQL_CONN = "08000"; 496 static final String SQL_CLIENT_NO_CONN = "08001"; 502 static final String SQL_CONN_IN_USE = "08002"; 508 static final String SQL_NO_CONN = "08003"; 513 static final String SQL_REJECT_CONN = "08004"; 519 static final String SQL_CONN_FAIL = "08006"; 524 static final String SQL_TRANS_UNK = "08007"; 529 static final String SQL_NO_SUPPORT = "0A000"; 534 static final String SQL_NO_SUPPORT_MULTI = "0A001"; 540 static final String SQL_INVALID_VALUE = "21000"; 545 static final String SQL_DATA = "22000"; 550 static final String SQL_DATA_RTRUNC = "22001"; 556 static final String SQL_DATA_NULL = "22002"; 562 static final String SQL_OUT_OF_RANGE = "22003"; 568 static final String SQL_DATA_EXCEPT = "22005"; 573 static final String SQL_DATETIME_FMT = "22007"; 578 static final String SQL_DATETIME_OVFLO = "22008"; 583 static final String SQL_TIMEZONE = "22009"; 589 static final String SQL_SUBSTR_ERROR = "22011"; 594 static final String SQL_DIV_BY_ZERO = "22012"; 599 static final String SQL_INTERVAL_OVFLO = "22015"; 604 static final String SQL_INVAL_CHAR_CAST = "22018"; 610 static final String SQL_INVAL_ESCAPE_CHAR = "22019"; 615 static final String SQL_CHAR_NOT_REP = "22021"; 620 static final String SQL_IND_OVERFLOW = "22022"; 625 static final String SQL_INVAL_PARAM_VALUE = "22023"; 630 static final String SQL_UNTERM_C_STR = "22024"; 635 static final String SQL_INVAL_ESCAPE_SEQ = "22025"; 640 static final String SQL_STR_LEN_MISMATCH = "22026"; 645 static final String SQL_TRIM_ERROR = "22027"; 650 static final String SQL_INTEG_CONSTRAINT = "23000"; 655 static final String SQL_INVAL_CURSOR_STATE = "24000"; 660 static final String SQL_INVAL_TRANS_STATE = "25000"; 665 static final String SQL_INVAL_SQL_NAME = "26000"; 670 static final String SQL_INVAL_AUTH = "28000"; 675 static final String SQL_SYNTAX_DIRECT = "2A000"; 681 static final String SQL_DESC_EXIST = "2B000"; 687 static final String SQL_INVAL_CHAR_SET = "2C000"; 692 static final String SQL_INVAL_TRANS_TERM = "2D000"; 697 static final String SQL_INVAL_CONN_NAME = "2E000"; 702 static final String SQL_INVAL_SQL_DESC_NAME = "33000"; 707 static final String SQL_INVAL_CURSOR_NAME = "34000"; 712 static final String SQL_INVAL_COND_NUM = "35000"; 717 static final String SQL_SYNTAX_DYNAMIC = "37000"; 723 static final String SQL_AMBIG_CURSOR = "3C000"; 728 static final String SQL_INVAL_CATALOG = "3D000"; 733 static final String SQL_INVAL_SCHEMA_NAME = "3F000"; 738 static final String SQL_TRANS_ROLLBACK = "40000"; 743 static final String SQL_TRANS_SERIAL_FAIL = "40001"; 749 static final String SQL_TRANS_INTEG = "40002"; 755 static final String SQL_TRANS_COMP_UNK = "40003"; 761 static final String SQL_SYNTAX = "42000"; 766 static final String SQL_CHECK_OPT = "44000"; 771 static final String SQL_RMT_DB_ACCESS = "HZ "; 776 777 784 public ConnectionManager() { 785 super(); 786 this.driverName = null; 787 this.expandedDriverName = null; 788 this.url = null; 789 this.expandedUrl = null; 790 this.userName = null; 791 this.expandedUserName = null; 792 this.password = null; 793 this.expandedPassword = null; 794 this.minPool = 0; 795 this.maxPool = 0; 796 this.busyList = null; 797 this.freeList = null; 798 this.poolSize = 0; 799 this.pooling = false; 800 this.xactConnections = null; 801 this.shutDownPending = false; 802 this.connectionBlocking = false; 803 this.msWait = 0; 804 this.msInterval = 0; 805 this.busyList = null; 806 this.xactConnections = null; 807 this.initialized = false; 808 } 809 810 812 820 public ConnectionManager(String driverName) 821 throws ClassNotFoundException , SQLException { 822 this(); 823 try { 824 this.setDriverName(driverName); 825 startUp(); 826 } catch (SQLException se) { 827 throw se; 828 } catch (ClassNotFoundException e) { 829 throw e; 830 } 831 } 832 833 842 public ConnectionManager(String driverName, String url) 843 throws ClassNotFoundException , SQLException { 844 this(); 845 try { 846 this.setDriverName(driverName); 847 this.setURL(url); 848 startUp(); 849 } catch (SQLException se) { 850 throw se; 851 } 852 } 853 854 864 public ConnectionManager(String driverName, String url, 865 String userName) throws ClassNotFoundException , SQLException { 866 this(); 867 try { 868 this.setDriverName(driverName); 869 this.setURL(url); 870 this.setUserName(userName); 871 startUp(); 872 } catch (SQLException se) { 873 throw se; 874 } 875 } 876 877 888 public ConnectionManager 889 ( 890 String driverName, 891 String url, 892 String userName, 893 String password 894 ) throws ClassNotFoundException , SQLException { 895 this(); 896 try { 897 this.setDriverName(driverName); 898 this.setURL(url); 899 this.setUserName(userName); 900 this.setPassword(password); 901 startUp(); 902 } catch (SQLException se) { 903 throw se; 904 } 905 } 906 907 927 public ConnectionManager 928 ( 929 String driverName, 930 String url, 931 String userName, 932 String password, 933 int minPool, 934 int maxPool 935 ) throws ClassNotFoundException , SQLException { 936 this(); 937 try { 938 this.setDriverName(driverName); 939 this.setURL(url); 940 this.setUserName(userName); 941 this.setPassword(password); 942 this.setMinPool(minPool); 943 this.setMaxPool(maxPool); 944 startUp(); 945 } catch (SQLException se) { 946 throw se; 947 } 948 } 949 950 983 public ConnectionManager 984 ( 985 String driverName, 986 String url, 987 String userName, 988 String password, 989 int minPool, 990 int maxPool, 991 int msWait 992 ) throws ClassNotFoundException , SQLException { 993 this(); 994 try { 995 this.setDriverName(driverName); 996 this.setURL(url); 997 this.setUserName(userName); 998 this.setPassword(password); 999 this.setMinPool(minPool); 1000 this.setMaxPool(maxPool); 1001 this.setMsWait(msWait); 1002 this.setMsInterval(DEFAULT_RETRY_INTERVAL); 1003 startUp(); 1004 } catch (SQLException se) { 1005 throw se; 1006 } 1007 } 1008 1009 1040 1041 public ConnectionManager 1042 ( 1043 String driverName, 1044 String url, 1045 String userName, 1046 String password, 1047 int minPool, 1048 int maxPool, 1049 int msWait, 1050 int msInterval 1051 ) throws ClassNotFoundException , SQLException { 1052 this(); 1053 try { 1054 this.setDriverName(driverName); 1055 this.setURL(url); 1056 this.setUserName(userName); 1057 this.setPassword(password); 1058 this.setMinPool(minPool); 1059 this.setMaxPool(maxPool); 1060 this.setMsWait(msWait); 1061 this.setMsInterval(msInterval); 1062 startUp(); 1063 } catch (SQLException se) { 1064 throw se; 1065 } 1066 } 1067 1068 1070 1071 1100 public synchronized Connection getConnection() throws SQLException { 1101 if (this.shutDownPending == true) { 1102 SQLException se = new SQLException 1103 ( 1104 StringScanner.createParamString 1105 ( 1106 I18NHelper.getMessage(messages, 1107 "connection.connectionmanager.isdown") ), 1109 SQL_NO_CONN 1110 ); 1111 throw se; 1112 } 1113 1114 ConnectionImpl conn = this.checkXact(); 1115 1116 if (conn != null) { 1117 } else if (!this.pooling) { 1120 conn = (ConnectionImpl) this.getConnection(this.userName, 1121 this.password); 1122 conn.setPooled(false); 1123 conn.checkXact(); 1124 } else { 1126 if (this.freeList.size <= 0) { 1128 if (this.poolSize < this.maxPool) { 1130 try { 1131 this.expandPool(1); } catch (SQLException se) { 1133 throw se; 1134 } 1135 } else if (this.connectionBlocking != true) { 1137 SQLException se = new SQLException 1139 ( 1140 StringScanner.createParamString 1141 ( 1142 I18NHelper.getMessage(messages, 1143 "connection.connectionmanager.maxpool") ), 1145 SQL_INVAL_PARAM_VALUE ); 1147 throw se; 1148 } else { 1150 try { 1151 this.waitForConnection(); } catch (SQLException se) { 1153 throw se; 1154 } 1155 } 1156 } 1157 conn = (ConnectionImpl) (this.freeList.removeFromHead()); 1158 if (conn == null) { 1159 SQLException se = new SQLException 1161 ( 1162 StringScanner.createParamString 1163 ( 1164 I18NHelper.getMessage(messages, 1165 "connection.connectionmanager.badvalue") ), 1167 SQL_INVAL_PARAM_VALUE ); 1169 throw se; 1170 } 1171 conn.setPooled(true); 1172 conn.checkXact(); 1173 this.busyList.insertAtTail((Linkable) conn); 1174 } 1175 conn.setFreePending(false); 1176 return ((Connection ) conn); 1177 } 1178 1179 1188 public synchronized Connection getConnection 1189 ( 1190 String userName, 1191 String password 1192 ) throws SQLException { 1193 boolean debug = logger.isLoggable(Logger.FINEST); 1194 1195 1196 if (this.shutDownPending == true) { 1197 SQLException se = new SQLException 1198 ( 1199 StringScanner.createParamString 1200 ( 1201 I18NHelper.getMessage(messages, 1202 "connection.connectionmanager.isdown") ), 1204 SQL_CONN_FAIL 1205 ); 1206 throw se; 1207 } 1208 1209 ConnectionImpl conn = this.checkXact(); 1210 1211 if (conn == null) { 1212 if (freeConn != null) { 1213 if (debug) { 1215 logger.finest("sqlstore.connection.conncectiomgr.found",freeConn); } 1217 conn = freeConn; 1218 freeConn = null; 1219 } else { 1220 try { 1222 conn = new ConnectionImpl 1224 ( 1225 DriverManager.getConnection 1226 ( 1227 this.expandedUrl, 1228 this.expandAttribute(userName), 1229 this.expandAttribute(password) 1230 ), 1231 this.expandedUrl, 1232 this.expandAttribute(userName), 1233 this 1234 ); 1235 if (debug) { 1236 logger.finest("sqlstore.connection.conncectiomgr.getnewconn",conn); } 1238 } catch (SQLException se) { 1239 throw se; 1240 } 1241 } 1242 conn.checkXact(); 1243 } else { 1244 if (!conn.getUserName().equals(this.expandAttribute(userName))) { 1245 SQLException se = new SQLException 1246 ( 1247 StringScanner.createParamString 1248 ( 1249 I18NHelper.getMessage(messages, 1250 "connection.connectionmanager.getconnection.mismatch") ), 1252 SQL_NO_CONN ); 1254 throw se; 1255 } 1256 } 1257 conn.setFreePending(false); 1258 conn.setPooled(false); 1259 this.busyList.insertAtTail((Linkable) conn); 1260 return ((Connection ) conn); 1261 } 1262 1263 1273 public synchronized Connection getConnection 1274 ( 1275 String url, 1276 String userName, 1277 String password 1278 ) throws SQLException { 1279 boolean debug = logger.isLoggable(Logger.FINEST); 1280 1281 if (this.shutDownPending == true) { 1282 SQLException se = new SQLException 1283 ( 1284 StringScanner.createParamString 1285 ( 1286 I18NHelper.getMessage(messages, 1287 "connection.connectionmanager.isdown") ), 1289 SQL_CONN_FAIL 1290 ); 1291 throw se; 1292 } 1293 1294 ConnectionImpl conn = this.checkXact(); 1295 1296 if (conn == null) { 1297 if (freeConn != null) { 1298 if (debug) { 1300 logger.finest("sqlstore.connection.conncectiomgr.found",freeConn); } 1302 conn = freeConn; 1303 freeConn = null; 1304 } else { 1305 try { 1307 conn = new ConnectionImpl 1309 ( 1310 DriverManager.getConnection 1311 ( 1312 this.expandAttribute(url), 1313 this.expandAttribute(userName), 1314 this.expandAttribute(password) 1315 ), 1316 this.expandAttribute(url), 1317 this.expandAttribute(userName), 1318 this 1319 ); 1320 if (debug) { 1321 logger.finest("sqlstore.connection.conncectiomgr.getnewconn",conn); } 1323 } catch (SQLException se) { 1324 throw se; 1325 } 1326 } 1327 conn.checkXact(); 1328 } else { 1329 if ((!conn.getURL().equals(this.expandAttribute(url))) || 1330 (!conn.getUserName().equals(this.expandAttribute(userName)))) { 1331 SQLException se = new SQLException 1332 ( 1333 StringScanner.createParamString 1334 ( 1335 I18NHelper.getMessage(messages, 1336 "connection.connectionmanager.getconnection.mismatch") ), 1338 SQL_NO_CONN ); 1340 throw se; 1341 } 1342 } 1343 conn.setFreePending(false); 1344 conn.setPooled(false); 1345 this.busyList.insertAtTail((Linkable) conn); 1346 return ((Connection ) conn); 1347 } 1348 1349 1356 private synchronized ConnectionImpl checkXact() { 1357 Transaction tran = null; 1358 1359 1371 1372 if (tran == null) 1374 return null; 1375 return (ConnectionImpl) this.xactConnections.get(tran); 1376 } 1377 1378 1388 public void startUp() throws ClassNotFoundException , SQLException { 1389 if (this.initialized == true) return; 1390 1391 this.busyList = new DoubleLinkedList(); 1392 this.xactConnections = new Hashtable (); 1393 this.expandedDriverName = this.expandAttribute(this.driverName); 1394 if (this.expandedDriverName == null) { 1395 SQLException se = new SQLException 1396 ( 1397 StringScanner.createParamString 1398 ( 1399 I18NHelper.getMessage(messages, 1400 "connection.connectionmanager.nulldriver") ), 1402 SQL_INVALID_VALUE ); 1404 throw se; 1405 } 1406 this.expandedUrl = this.expandAttribute(this.url); 1407 if (this.expandedUrl == null) { 1408 SQLException se = new SQLException 1409 ( 1410 StringScanner.createParamString 1411 ( 1412 I18NHelper.getMessage(messages, 1413 "connection.connectionmanager.nullurl") ), 1415 SQL_INVALID_VALUE ); 1417 throw se; 1418 } 1419 this.expandedUserName = this.expandAttribute(this.userName); 1420 if (this.expandedUserName == null) { 1421 this.expandedUserName = ""; } 1423 this.expandedPassword = this.expandAttribute(this.password); 1424 if (this.expandedPassword == null) { 1425 this.expandedPassword = ""; } 1427 try { 1428 Class.forName(this.expandedDriverName); 1429 1430 if ((this.minPool > 0) && (this.maxPool >= this.minPool)) { 1432 this.pooling = true; 1434 this.freeList = new DoubleLinkedList(); 1435 expandPool(this.minPool); 1436 } else if ((this.minPool == 0) && (this.maxPool == 0)) { 1437 this.pooling = false; 1439 } 1440 1441 } catch (SQLException se) { 1442 throw se; 1443 } catch (ClassNotFoundException e) { 1444 throw e; 1445 } 1446 this.initialized = true; 1447 } 1448 1449 1461 public synchronized void shutDown() throws SQLException { 1462 this.shutDownPending = true; 1463 1464 if (this.pooling == true) { 1465 ConnectionImpl conn; 1466 this.connectionBlocking = false; 1467 this.pooling = false; 1468 this.initialized = false; 1469 for 1470 ( 1471 conn = (ConnectionImpl) this.freeList.getHead(); 1472 conn != null; 1473 conn = (ConnectionImpl) conn.getNext() 1474 ) { 1475 try { 1476 conn.close(); 1477 } catch (SQLException e) { 1478 throw e; 1479 } 1480 } 1481 this.freeList = null; 1482 } 1483 } 1484 1485 1486 1498 protected void finalize() { 1499 try { 1500 shutDown(); 1501 } catch (SQLException se) { 1502 ; } 1504 } 1505 1506 1508 1513 public synchronized String getDriverName() { 1514 return (this.driverName); 1515 } 1516 1517 1518 1524 public synchronized void setDriverName(String driverName) throws SQLException { 1525 if (driverName == null) { 1526 SQLException se = new SQLException 1527 ( 1528 StringScanner.createParamString 1529 ( 1530 I18NHelper.getMessage(messages, 1531 "connection.connectionmanager.nulldriver") ), 1533 SQL_INVALID_VALUE ); 1535 throw se; 1536 } 1537 this.driverName = driverName; 1538 } 1539 1540 1551 public synchronized String getURL() { 1552 return (this.url); 1553 } 1554 1555 1565 public synchronized void setURL(String url) throws SQLException { 1566 if (url == null) { 1567 SQLException se = new SQLException 1568 ( 1569 StringScanner.createParamString 1570 ( 1571 I18NHelper.getMessage(messages, 1572 "connection.connectionmanager.nullurl") ), 1574 SQL_INVALID_VALUE ); 1576 throw se; 1577 } 1578 this.url = url; 1579 } 1580 1581 1592 public synchronized String getUserName() { 1593 return (this.userName); 1594 } 1595 1596 1603 public synchronized void setUserName(String userName) throws SQLException { 1604 this.userName = userName; 1605 } 1606 1607 1618 public synchronized String getPassword() { 1619 return (this.password); 1620 } 1621 1622 1627 public synchronized void setPassword(String password) throws SQLException { 1628 this.password = password; 1629 } 1630 1631 1644 public synchronized int getMinPool() { 1645 return (this.minPool); 1646 } 1647 1648 1668 public synchronized void setMinPool(int minPool) throws SQLException { 1669 if (shutDownPending == true) { 1670 SQLException se = new SQLException 1671 ( 1672 StringScanner.createParamString 1673 ( 1674 I18NHelper.getMessage(messages, 1675 "connection.connectionmanager.isdown") ), 1677 SQL_CONN_FAIL ); 1679 1680 throw se; 1681 } 1682 if (minPool < 0) { 1683 SQLException se = new SQLException 1684 ( 1685 StringScanner.createParamString 1686 ( 1687 I18NHelper.getMessage(messages, 1688 "connection.connectionmanager.zero") ), 1690 SQL_INVAL_PARAM_VALUE ); 1692 throw se; 1693 } 1694 if (minPool < this.minPool) { 1695 SQLException se = new SQLException 1696 ( 1697 StringScanner.createParamString 1698 ( 1699 I18NHelper.getMessage(messages, 1700 "connection.connectionmanager.badnew"), Integer.toString(minPool), 1702 Integer.toString(minPool) 1703 ), 1704 SQL_INVAL_PARAM_VALUE ); 1706 throw se; 1707 } 1708 if (pooling == true) { 1709 if (minPool > maxPool) { 1710 SQLException se = new SQLException 1711 ( 1712 StringScanner.createParamString 1713 ( 1714 I18NHelper.getMessage(messages, 1715 "connection.connectionmanager.poolsize") ), 1717 SQL_INVAL_PARAM_VALUE ); 1719 throw se; 1720 } 1721 } 1722 this.minPool = minPool; 1723 } 1724 1725 1740 public synchronized int getMaxPool() { 1741 return (this.maxPool); 1742 } 1743 1744 1765 public synchronized void setMaxPool(int maxPool) throws SQLException { 1766 if (shutDownPending == true) { 1767 SQLException se = new SQLException 1768 ( 1769 StringScanner.createParamString 1770 ( 1771 I18NHelper.getMessage(messages, 1772 "connection.connectionmanager.isdown") ), 1774 SQL_CONN_FAIL ); 1776 throw se; 1777 } 1778 if (maxPool < 0) { 1779 SQLException se = new SQLException 1780 ( 1781 StringScanner.createParamString 1782 ( 1783 I18NHelper.getMessage(messages, 1784 "connection.connectionmanager.zero"), Integer.toString(maxPool) 1786 ), 1787 SQL_INVAL_PARAM_VALUE ); 1789 throw se; 1790 } 1791 if (pooling == true) { 1792 if (maxPool < this.maxPool) { 1793 SQLException se = new SQLException 1794 ( 1795 StringScanner.createParamString 1796 ( 1797 I18NHelper.getMessage(messages, 1798 "connection.connectionmanager.badnew"), Integer.toString(maxPool), 1800 Integer.toString(maxPool) 1801 ), 1802 SQL_INVAL_PARAM_VALUE ); 1804 throw se; 1805 } 1806 } 1807 1808 if (maxPool < this.minPool) { 1809 SQLException se = new SQLException 1810 ( 1811 StringScanner.createParamString 1812 ( 1813 I18NHelper.getMessage(messages, 1814 "connection.connectionmanager.poolsize") ), 1816 SQL_INVAL_PARAM_VALUE ); 1818 throw se; 1819 } 1820 this.maxPool = maxPool; 1821 } 1822 1823 1834 public synchronized int getMsWait() { 1835 return (this.msWait); 1836 } 1837 1838 1868 public synchronized void setMsWait(int msWait) throws SQLException { 1869 if (msWait < 0) { 1870 SQLException se = new SQLException 1871 ( 1872 StringScanner.createParamString 1873 ( 1874 I18NHelper.getMessage(messages, 1875 "connection.connectionmanager.badvalue"), Integer.toString(msWait) 1877 ), 1878 SQL_INVAL_PARAM_VALUE ); 1880 throw se; 1881 } else if (msWait > 0) { 1882 this.msWait = msWait; 1883 this.connectionBlocking = true; 1884 } else { 1885 this.msWait = msWait; 1886 this.connectionBlocking = false; 1887 } 1888 } 1889 1890 1900 public synchronized int getMsInterval() { 1901 return (this.msInterval); 1902 } 1903 1904 1933 public synchronized void setMsInterval(int msInterval) throws SQLException { 1934 if ((msInterval < 0) || (this.msWait < msInterval)) { 1935 SQLException se = new SQLException 1936 ( 1937 StringScanner.createParamString 1938 ( 1939 I18NHelper.getMessage(messages, 1940 "connection.connectionmanager.badnew"), "MsInterval", "MsWait" ), 1944 SQL_INVAL_PARAM_VALUE ); 1946 throw se; 1947 } else { 1948 this.msInterval = msInterval; 1949 } 1950 } 1951 1952 1958 public synchronized String toString() { 1959 2009 2010 return null; 2011 } 2012 2013 2015 2021 synchronized void associateXact(Transaction tran, ConnectionImpl conn) { 2022 if (tran != null) 2023 this.xactConnections.put((Object ) tran, (Object ) conn); 2024 } 2025 2026 2034 synchronized void disassociateXact 2035 ( 2036 Transaction tran, 2037 ConnectionImpl conn, 2038 boolean free 2039 ) throws SQLException { 2040 ConnectionImpl xactConn = null; 2041 2042 if (tran != null) 2043 xactConn = (ConnectionImpl) this.xactConnections.remove((Object ) tran); 2044 2045 if (tran == null || xactConn.equals((Object ) conn)) { 2046 if (free == true) { 2047 if (conn.connectionManager.shutDownPending == false) { 2048 this.freeList.insertAtTail((Linkable) conn); 2049 } else { 2050 conn.close(); 2051 } 2052 } 2053 } else { 2054 SQLException se = new SQLException 2055 ( 2056 StringScanner.createParamString 2057 ( 2058 "Internal Error: transaction mismatch" ), 2061 SQL_TRANS_UNK ); 2063 throw se; 2064 } 2065 } 2066 2067 2074 private synchronized void expandPool(int connections) throws SQLException { 2075 ConnectionImpl conn = null; 2076 2077 if (this.shutDownPending == true) { 2078 SQLException se = new SQLException 2079 ( 2080 StringScanner.createParamString 2081 ( 2082 I18NHelper.getMessage(messages, 2083 "connection.connectionmanager.isdown") ), 2085 SQL_CONN_FAIL 2086 ); 2087 throw se; 2088 } 2089 2090 for (int i = 0; i < connections; i++) { 2091 if (this.poolSize >= this.maxPool) { 2092 SQLException se = new SQLException 2094 ( 2095 StringScanner.createParamString 2096 ( 2097 I18NHelper.getMessage(messages, 2098 "connection.connectionmanager.maxpool") ), 2100 SQL_CONN_FAIL 2101 ); 2102 throw se; 2103 } else { 2105 try { 2106 conn = new ConnectionImpl 2107 ( 2108 DriverManager.getConnection 2109 ( 2110 this.expandedUrl, 2111 this.expandedUserName, 2112 this.expandedPassword 2113 ), 2114 this.expandedUrl, 2115 this.expandedUserName, 2116 this 2117 ); 2118 conn.setPooled(true); 2119 this.freeList.insertAtTail((Linkable) conn); 2120 this.poolSize++; 2121 } catch (SQLException e) { 2122 throw e; 2123 } 2124 } 2125 } 2126 } 2127 2128 2136 private String expandAttribute(String envname) throws SQLException { 2137 String attribute = null; 2138 2158 if (attribute != null) { 2159 return attribute; 2160 } else { 2161 return envname; 2162 } 2163 } 2164 2165 2174 private synchronized void waitForConnection() throws SQLException { 2175 int interval = this.msInterval; 2176 int wait = this.msWait; 2177 int totalTime = 0; 2178 boolean done = false; 2179 Thread t = Thread.currentThread(); 2180 do { 2181 if (this.freeList.size > 0) { 2183 done = true; 2184 } else { 2186 if (this.poolSize < this.maxPool) { 2188 try { 2190 expandPool(1); 2191 done = true; 2192 } catch (SQLException se) { 2193 throw se; 2194 } 2195 } else { 2197 if (totalTime >= wait) { 2199 SQLException se = new SQLException 2200 ( 2201 StringScanner.createParamString 2202 ( 2203 I18NHelper.getMessage(messages, 2204 "connection.connectionmanager.conntimeout") ), 2206 SQL_CONN_FAIL 2207 ); 2208 throw se; 2209 } else { 2211 try { 2212 this.wait(interval); 2213 } catch (InterruptedException ie) { 2214 SQLException se = new SQLException 2215 ( 2216 StringScanner.createParamString 2217 ( 2218 I18NHelper.getMessage(messages, 2219 "connection.connectionmanager.threaditerupted") ), 2221 SQL_CONN_FAIL 2222 ); 2223 throw se; 2224 } 2225 } 2226 totalTime += interval; 2227 } 2228 } 2229 } while (!done); 2230 } 2231 2232 2233 public void setLoginTimeout(int seconds) 2234 throws SQLException { 2235 loginTimeout = seconds; 2236 } 2237 2238 public int getLoginTimeout() 2239 throws SQLException { 2240 return loginTimeout; 2241 } 2242 2243 2247 protected synchronized void replaceFreeConnection(ConnectionImpl c) { 2248 boolean debug = logger.isLoggable(Logger.FINEST); 2249 2250 if (debug) { 2251 logger.finest("sqlstore.connection.conncectiomgr.replacefreeconn",freeConn); } 2253 if (freeConn != null) { 2254 freeConn.release(); 2256 } 2257 freeConn = c; 2258 } 2259} 2260 | Popular Tags |