1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.dbcp.dbcp2;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.logging.Logger;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.StandardMBean;
import javax.sql.DataSource;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.dbcp.pool2.PooledObject;
import org.apache.tomcat.dbcp.pool2.impl.AbandonedConfig;
import org.apache.tomcat.dbcp.pool2.impl.BaseObjectPoolConfig;
import org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig;
import org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool;
import org.apache.tomcat.dbcp.pool2.impl.GenericObjectPoolConfig;
/**
* Basic implementation of {@code javax.sql.DataSource} that is configured via JavaBeans properties.
* <p>
* This is not the only way to combine the <em>commons-dbcp2</em> and <em>commons-pool2</em> packages, but provides a
* one-stop solution for basic requirements.
* </p>
*
* @since 2.0
*/
public class BasicDataSource implements DataSource, BasicDataSourceMXBean, MBeanRegistration, AutoCloseable {
private static final Log log = LogFactory.getLog(BasicDataSource.class);
static {
// Attempt to prevent deadlocks - see DBCP-272
DriverManager.getDrivers(); // NOPMD
}
/**
* Validates the given factory.
*
* @param connectionFactory the factory
* @throws SQLException Thrown by one of the factory methods while managing a temporary pooled object.
*/
protected static void validateConnectionFactory(final PoolableConnectionFactory connectionFactory) throws SQLException {
PoolableConnection conn = null;
PooledObject<PoolableConnection> p = null;
try {
p = connectionFactory.makeObject();
conn = p.getObject();
connectionFactory.activateObject(p);
connectionFactory.validateConnection(conn);
connectionFactory.passivateObject(p);
} finally {
if (p != null) {
connectionFactory.destroyObject(p);
}
}
}
/**
* The default auto-commit state of connections created by this pool.
*/
private volatile Boolean defaultAutoCommit;
/**
* The default read-only state of connections created by this pool.
*/
private transient Boolean defaultReadOnly;
/**
* The default TransactionIsolation state of connections created by this pool.
*/
private volatile int defaultTransactionIsolation = PoolableConnectionFactory.UNKNOWN_TRANSACTION_ISOLATION;
private Duration defaultQueryTimeoutDuration;
/**
* The default "catalog" of connections created by this pool.
*/
private volatile String defaultCatalog;
/**
* The default "schema" of connections created by this pool.
*/
private volatile String defaultSchema;
/**
* The property that controls if the pooled connections cache some state rather than query the database for current
* state to improve performance.
*/
private volatile boolean cacheState = true;
/**
* The instance of the JDBC Driver to use.
*/
private Driver driver;
/**
* The fully qualified Java class name of the JDBC driver to be used.
*/
private String driverClassName;
/**
* The class loader instance to use to load the JDBC driver. If not specified, {@link Class#forName(String)} is used
* to load the JDBC driver. If specified, {@link Class#forName(String, boolean, ClassLoader)} is used.
*/
private ClassLoader driverClassLoader;
/**
* True means that borrowObject returns the most recently used ("last in") connection in the pool (if there are idle
* connections available). False means that the pool behaves as a FIFO queue - connections are taken from the idle
* instance pool in the order that they are returned to the pool.
*/
private boolean lifo = BaseObjectPoolConfig.DEFAULT_LIFO;
/**
* The maximum number of active connections that can be allocated from this pool at the same time, or negative for
* no limit.
*/
private int maxTotal = GenericObjectPoolConfig.DEFAULT_MAX_TOTAL;
/**
* The maximum number of connections that can remain idle in the pool, without extra ones being destroyed, or
* negative for no limit. If maxIdle is set too low on heavily loaded systems it is possible you will see
* connections being closed and almost immediately new connections being opened. This is a result of the active
* threads momentarily closing connections faster than they are opening them, causing the number of idle connections
* to rise above maxIdle. The best value for maxIdle for heavily loaded system will vary but the default is a good
* starting point.
*/
private int maxIdle = GenericObjectPoolConfig.DEFAULT_MAX_IDLE;
/**
* The minimum number of active connections that can remain idle in the pool, without extra ones being created when
* the evictor runs, or 0 to create none. The pool attempts to ensure that minIdle connections are available when
* the idle object evictor runs. The value of this property has no effect unless
* {@link #durationBetweenEvictionRuns} has a positive value.
*/
private int minIdle = GenericObjectPoolConfig.DEFAULT_MIN_IDLE;
/**
* The initial number of connections that are created when the pool is started.
*/
private int initialSize;
/**
* The maximum Duration that the pool will wait (when there are no available connections) for a
* connection to be returned before throwing an exception, or <= 0 to wait indefinitely.
*/
private Duration maxWaitDuration = BaseObjectPoolConfig.DEFAULT_MAX_WAIT;
/**
* Prepared statement pooling for this pool. When this property is set to {@code true} both PreparedStatements
* and CallableStatements are pooled.
*/
private boolean poolPreparedStatements;
private volatile boolean clearStatementPoolOnReturn;
/**
* <p>
* The maximum number of open statements that can be allocated from the statement pool at the same time, or negative
* for no limit. Since a connection usually only uses one or two statements at a time, this is mostly used to help
* detect resource leaks.
* </p>
* <p>
* Note: As of version 1.3, CallableStatements (those produced by {@link Connection#prepareCall}) are pooled along
* with PreparedStatements (produced by {@link Connection#prepareStatement}) and
* {@code maxOpenPreparedStatements} limits the total number of prepared or callable statements that may be in
* use at a given time.
* </p>
*/
private int maxOpenPreparedStatements = GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL;
/**
* The indication of whether objects will be validated as soon as they have been created by the pool. If the object
* fails to validate, the borrow operation that triggered the creation will fail.
*/
private boolean testOnCreate;
/**
* The indication of whether objects will be validated before being borrowed from the pool. If the object fails to
* validate, it will be dropped from the pool, and we will attempt to borrow another.
*/
private boolean testOnBorrow = true;
/**
* The indication of whether objects will be validated before being returned to the pool.
*/
private boolean testOnReturn;
/**
* The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle
* object evictor thread will be run.
*/
private Duration durationBetweenEvictionRuns = BaseObjectPoolConfig.DEFAULT_DURATION_BETWEEN_EVICTION_RUNS;
/**
* The number of objects to examine during each run of the idle object evictor thread (if any).
*/
private int numTestsPerEvictionRun = BaseObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
/**
* The minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle
* object evictor (if any).
*/
private Duration minEvictableIdleDuration = BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_DURATION;
/**
* The minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by the idle
* object evictor, with the extra condition that at least "minIdle" connections remain in the pool. Note that
* {@code minEvictableIdleTimeMillis} takes precedence over this parameter. See
* {@link #getSoftMinEvictableIdleDuration()}.
*/
private Duration softMinEvictableIdleDuration = BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_DURATION;
private String evictionPolicyClassName = BaseObjectPoolConfig.DEFAULT_EVICTION_POLICY_CLASS_NAME;
/**
* The indication of whether objects will be validated by the idle object evictor (if any). If an object fails to
* validate, it will be dropped from the pool.
*/
private boolean testWhileIdle;
/**
* The connection password to be passed to our JDBC driver to establish a connection.
*/
private volatile String password;
/**
* The connection string to be passed to our JDBC driver to establish a connection.
*/
private String connectionString;
/**
* The connection user name to be passed to our JDBC driver to establish a connection.
*/
private String userName;
/**
* The SQL query that will be used to validate connections from this pool before returning them to the caller. If
* specified, this query <strong>MUST</strong> be an SQL SELECT statement that returns at least one row. If not
* specified, {@link Connection#isValid(int)} will be used to validate connections.
*/
private volatile String validationQuery;
/**
* Timeout in seconds before connection validation queries fail.
*/
private volatile Duration validationQueryTimeoutDuration = Duration.ofSeconds(-1);
/**
* The fully qualified Java class name of a {@link ConnectionFactory} implementation.
*/
private String connectionFactoryClassName;
/**
* These SQL statements run once after a Connection is created.
* <p>
* This property can be used for example to run ALTER SESSION SET NLS_SORT=XCYECH in an Oracle Database only once
* after connection creation.
* </p>
*/
private volatile List<String> connectionInitSqls;
/**
* Controls access to the underlying connection.
*/
private volatile boolean accessToUnderlyingConnectionAllowed;
private Duration maxConnDuration = Duration.ofMillis(-1);
private volatile boolean logExpiredConnections = true;
private String jmxName;
private volatile boolean registerConnectionMBean = true;
private volatile boolean autoCommitOnReturn = true;
private volatile boolean rollbackOnReturn = true;
private volatile Set<String> disconnectionSqlCodes;
/**
* A collection of SQL State codes that are not considered fatal disconnection codes.
*
* @since 2.13.0
*/
private volatile Set<String> disconnectionIgnoreSqlCodes;
private volatile boolean fastFailValidation;
/**
* The object pool that internally manages our connections.
*/
private volatile GenericObjectPool<PoolableConnection> connectionPool;
/**
* The connection properties that will be sent to our JDBC driver when establishing new connections.
* <strong>NOTE</strong> - The "user" and "password" properties will be passed explicitly, so they do not need to be
* included here.
*/
private Properties connectionProperties = new Properties();
/**
* The data source we will use to manage connections. This object should be acquired <strong>ONLY</strong> by calls
* to the {@code createDataSource()} method.
*/
private volatile DataSource dataSource;
/**
* The PrintWriter to which log messages should be directed.
*/
private volatile PrintWriter logWriter = new PrintWriter(
new OutputStreamWriter(System.out, StandardCharsets.UTF_8));
private AbandonedConfig abandonedConfig;
private boolean closed;
/**
* Actual name under which this component has been registered.
*/
private ObjectNameWrapper registeredJmxObjectName;
/**
* Constructs a new instance.
*/
public BasicDataSource() {
// empty
}
/**
* Adds a custom connection property to the set that will be passed to our JDBC driver. This <strong>MUST</strong>
* be called before the first connection is retrieved (along with all the other configuration property setters).
* Calls to this method after the connection pool has been initialized have no effect.
*
* @param name Name of the custom connection property
* @param value Value of the custom connection property
*/
public void addConnectionProperty(final String name, final String value) {
connectionProperties.put(name, value);
}
/**
* Closes and releases all idle connections that are currently stored in the connection pool associated with this
* data source.
* <p>
* Connections that are checked out to clients when this method is invoked are not affected. When client
* applications subsequently invoke {@link Connection#close()} to return these connections to the pool, the
* underlying JDBC connections are closed.
* </p>
* <p>
* Attempts to acquire connections using {@link #getConnection()} after this method has been invoked result in
* SQLExceptions. To reopen a datasource that has been closed using this method, use {@link #start()}.
* </p>
* <p>
* This method is idempotent - i.e., closing an already closed BasicDataSource has no effect and does not generate
* exceptions.
* </p>
*
* @throws SQLException if an error occurs closing idle connections
*/
@Override
public synchronized void close() throws SQLException {
if (registeredJmxObjectName != null) {
registeredJmxObjectName.unregisterMBean();
registeredJmxObjectName = null;
}
closed = true;
final GenericObjectPool<?> oldPool = connectionPool;
connectionPool = null;
dataSource = null;
try {
if (oldPool != null) {
oldPool.close();
}
} catch (final RuntimeException e) {
throw e;
} catch (final Exception e) {
throw new SQLException(Utils.getMessage("pool.close.fail"), e);
}
}
/**
* Closes the connection pool, silently swallowing any exception that occurs.
*/
private void closeConnectionPool() {
final GenericObjectPool<?> oldPool = connectionPool;
connectionPool = null;
Utils.closeQuietly(oldPool);
}
/**
* Creates a JDBC connection factory for this data source. The JDBC driver is loaded using the following algorithm:
* <ol>
* <li>If a Driver instance has been specified via {@link #setDriver(Driver)} use it</li>
* <li>If no Driver instance was specified and {code driverClassName} is specified that class is loaded using the
* {@link ClassLoader} of this class or, if {code driverClassLoader} is set, {code driverClassName} is loaded
* with the specified {@link ClassLoader}.</li>
* <li>If {code driverClassName} is specified and the previous attempt fails, the class is loaded using the
* context class loader of the current thread.</li>
* <li>If a driver still isn't loaded one is loaded via the {@link DriverManager} using the specified {code connectionString}.</li>
* </ol>
* <p>
* This method exists so subclasses can replace the implementation class.
* </p>
*
* @return A new connection factory.
* @throws SQLException If the connection factory cannot be created
*/
protected ConnectionFactory createConnectionFactory() throws SQLException {
// Load the JDBC driver class
return ConnectionFactoryFactory.createConnectionFactory(this, DriverFactory.createDriver(this));
}
/**
* Creates a connection pool for this datasource. This method only exists so subclasses can replace the
* implementation class.
* <p>
* This implementation configures all pool properties other than timeBetweenEvictionRunsMillis. Setting that
* property is deferred to {@link #startPoolMaintenance()}, since setting timeBetweenEvictionRunsMillis to a
* positive value causes {@link GenericObjectPool}'s eviction timer to be started.
* </p>
*
* @param factory The factory to use to create new connections for this pool.
*/
protected void createConnectionPool(final PoolableConnectionFactory factory) {
// Create an object pool to contain our active connections
final GenericObjectPoolConfig<PoolableConnection> config = new GenericObjectPoolConfig<>();
updateJmxName(config);
// Disable JMX on the underlying pool if the DS is not registered:
config.setJmxEnabled(registeredJmxObjectName != null);
// Set up usage tracking if enabled
if (getAbandonedUsageTracking() && abandonedConfig != null) {
abandonedConfig.setUseUsageTracking(true);
}
final GenericObjectPool<PoolableConnection> gop = createObjectPool(factory, config, abandonedConfig);
gop.setMaxTotal(maxTotal);
gop.setMaxIdle(maxIdle);
gop.setMinIdle(minIdle);
gop.setMaxWait(maxWaitDuration);
gop.setTestOnCreate(testOnCreate);
gop.setTestOnBorrow(testOnBorrow);
gop.setTestOnReturn(testOnReturn);
gop.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
gop.setMinEvictableIdleDuration(minEvictableIdleDuration);
gop.setSoftMinEvictableIdleDuration(softMinEvictableIdleDuration);
gop.setTestWhileIdle(testWhileIdle);
gop.setLifo(lifo);
gop.setSwallowedExceptionListener(new SwallowedExceptionLogger(log, logExpiredConnections));
gop.setEvictionPolicyClassName(evictionPolicyClassName);
factory.setPool(gop);
connectionPool = gop;
}
/**
* Creates (if necessary) and return the internal data source we are using to manage our connections.
*
* @return The current internal DataSource or a newly created instance if it has not yet been created.
* @throws SQLException if the object pool cannot be created.
*/
protected synchronized DataSource createDataSource() throws SQLException {
if (closed) {
throw new SQLException("Data source is closed");
}
// Return the pool if we have already created it
// This is double-checked locking. This is safe since dataSource is
// volatile and the code is targeted at Java 5 onwards.
if (dataSource != null) {
return dataSource;
}
synchronized (this) {
if (dataSource != null) {
return dataSource;
}
jmxRegister();
// create factory which returns raw physical connections
final ConnectionFactory driverConnectionFactory = createConnectionFactory();
// Set up the poolable connection factory
final PoolableConnectionFactory poolableConnectionFactory;
try {
poolableConnectionFactory = createPoolableConnectionFactory(driverConnectionFactory);
poolableConnectionFactory.setPoolStatements(poolPreparedStatements);
poolableConnectionFactory.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
// create a pool for our connections
createConnectionPool(poolableConnectionFactory);
final DataSource newDataSource = createDataSourceInstance();
newDataSource.setLogWriter(logWriter);
connectionPool.addObjects(initialSize);
// If timeBetweenEvictionRunsMillis > 0, start the pool's evictor
// task
startPoolMaintenance();
dataSource = newDataSource;
} catch (final SQLException | RuntimeException se) {
closeConnectionPool();
throw se;
} catch (final Exception ex) {
closeConnectionPool();
throw new SQLException("Error creating connection factory", ex);
}
return dataSource;
}
}
/**
* Creates the actual data source instance. This method only exists so that subclasses can replace the
* implementation class.
*
* @throws SQLException if unable to create a datasource instance
* @return A new DataSource instance
*/
protected DataSource createDataSourceInstance() throws SQLException {
final PoolingDataSource<PoolableConnection> pds = new PoolingDataSource<>(connectionPool);
pds.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
return pds;
}
/**
* Creates an object pool used to provide pooling support for {@link Connection JDBC connections}.
*
* @param factory the object factory
* @param poolConfig the object pool configuration
* @param abandonedConfig the abandoned objects configuration
* @return a non-null instance
*/
protected GenericObjectPool<PoolableConnection> createObjectPool(final PoolableConnectionFactory factory,
final GenericObjectPoolConfig<PoolableConnection> poolConfig, final AbandonedConfig abandonedConfig) {
final GenericObjectPool<PoolableConnection> gop;
if (abandonedConfig != null && (abandonedConfig.getRemoveAbandonedOnBorrow()
|| abandonedConfig.getRemoveAbandonedOnMaintenance())) {
gop = new GenericObjectPool<>(factory, poolConfig, abandonedConfig);
} else {
gop = new GenericObjectPool<>(factory, poolConfig);
}
return gop;
}
/**
* Creates the PoolableConnectionFactory and attaches it to the connection pool. This method only exists so
* subclasses can replace the default implementation.
*
* @param driverConnectionFactory JDBC connection factory
* @throws SQLException if an error occurs creating the PoolableConnectionFactory
* @return A new PoolableConnectionFactory configured with the current configuration of this BasicDataSource
*/
protected PoolableConnectionFactory createPoolableConnectionFactory(final ConnectionFactory driverConnectionFactory)
throws SQLException {
PoolableConnectionFactory connectionFactory = null;
try {
if (registerConnectionMBean) {
connectionFactory = new PoolableConnectionFactory(driverConnectionFactory, ObjectNameWrapper.unwrap(registeredJmxObjectName));
} else {
connectionFactory = new PoolableConnectionFactory(driverConnectionFactory, null);
}
connectionFactory.setValidationQuery(validationQuery);
connectionFactory.setValidationQueryTimeout(validationQueryTimeoutDuration);
connectionFactory.setConnectionInitSql(connectionInitSqls);
connectionFactory.setDefaultReadOnly(defaultReadOnly);
connectionFactory.setDefaultAutoCommit(defaultAutoCommit);
connectionFactory.setDefaultTransactionIsolation(defaultTransactionIsolation);
connectionFactory.setDefaultCatalog(defaultCatalog);
connectionFactory.setDefaultSchema(defaultSchema);
connectionFactory.setCacheState(cacheState);
connectionFactory.setPoolStatements(poolPreparedStatements);
connectionFactory.setClearStatementPoolOnReturn(clearStatementPoolOnReturn);
connectionFactory.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
connectionFactory.setMaxConn(maxConnDuration);
connectionFactory.setRollbackOnReturn(getRollbackOnReturn());
connectionFactory.setAutoCommitOnReturn(getAutoCommitOnReturn());
connectionFactory.setDefaultQueryTimeout(getDefaultQueryTimeoutDuration());
connectionFactory.setFastFailValidation(fastFailValidation);
connectionFactory.setDisconnectionSqlCodes(disconnectionSqlCodes);
connectionFactory.setDisconnectionIgnoreSqlCodes(disconnectionIgnoreSqlCodes);
validateConnectionFactory(connectionFactory);
} catch (final RuntimeException e) {
throw e;
} catch (final Exception e) {
throw new SQLException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
}
return connectionFactory;
}
/**
* Manually evicts idle connections
*
* @throws Exception when there is a problem evicting idle objects.
*/
public void evict() throws Exception {
if (connectionPool != null) {
connectionPool.evict();
}
}
/**
* Gets the print writer used by this configuration to log information on abandoned objects.
*
* @return The print writer used by this configuration to log information on abandoned objects.
*/
public PrintWriter getAbandonedLogWriter() {
return abandonedConfig == null ? null : abandonedConfig.getLogWriter();
}
/**
* If the connection pool implements {@link org.apache.tomcat.dbcp.pool2.UsageTracking UsageTracking}, should the
* connection pool record a stack trace every time a method is called on a pooled connection and retain the most
* recent stack trace to aid debugging of abandoned connections?
*
* @return {@code true} if usage tracking is enabled
*/
@Override
public boolean getAbandonedUsageTracking() {
return abandonedConfig != null && abandonedConfig.getUseUsageTracking();
}
/**
* Gets the value of the flag that controls whether or not connections being returned to the pool will be checked
* and configured with {@link Connection#setAutoCommit(boolean) Connection.setAutoCommit(true)} if the auto commit
* setting is {@code false} when the connection is returned. It is {@code true} by default.
*
* @return Whether or not connections being returned to the pool will be checked and configured with auto-commit.
*/
public boolean getAutoCommitOnReturn() {
return autoCommitOnReturn;
}
/**
* Gets the state caching flag.
*
* @return the state caching flag
*/
@Override
public boolean getCacheState() {
return cacheState;
}
/**
* Creates (if necessary) and return a connection to the database.
*
* @throws SQLException if a database access error occurs
* @return a database connection
*/
@Override
public Connection getConnection() throws SQLException {
return createDataSource().getConnection();
}
/**
* <strong>BasicDataSource does NOT support this method.</strong>
*
* @param user Database user on whose behalf the Connection is being made
* @param pass The database user's password
* @throws UnsupportedOperationException always thrown.
* @throws SQLException if a database access error occurs
* @return nothing - always throws UnsupportedOperationException
*/
@Override
public Connection getConnection(final String user, final String pass) throws SQLException {
// This method isn't supported by the PoolingDataSource returned by the
// createDataSource
throw new UnsupportedOperationException("Not supported by BasicDataSource");
}
/**
* Gets the ConnectionFactoryClassName that has been configured for use by this pool.
* <p>
* Note: This getter only returns the last value set by a call to {@link #setConnectionFactoryClassName(String)}.
* </p>
*
* @return the ConnectionFactoryClassName that has been configured for use by this pool.
* @since 2.7.0
*/
public String getConnectionFactoryClassName() {
return this.connectionFactoryClassName;
}
/**
* Gets the list of SQL statements executed when a physical connection is first created. Returns an empty list if
* there are no initialization statements configured.
*
* @return initialization SQL statements
*/
public List<String> getConnectionInitSqls() {
final List<String> result = connectionInitSqls;
return result == null ? Collections.emptyList() : result;
}
/**
* Provides the same data as {@link #getConnectionInitSqls()} but in an array so it is accessible via JMX.
*/
@Override
public String[] getConnectionInitSqlsAsArray() {
return getConnectionInitSqls().toArray(Utils.EMPTY_STRING_ARRAY);
}
/**
* Gets the underlying connection pool.
*
* @return the underlying connection pool.
* @since DBCP 2.10.0
*/
public GenericObjectPool<PoolableConnection> getConnectionPool() {
return connectionPool;
}
Properties getConnectionProperties() {
return connectionProperties;
}
/**
* Gets the default auto-commit property.
*
* @return true if default auto-commit is enabled
*/
@Override
public Boolean getDefaultAutoCommit() {
return defaultAutoCommit;
}
/**
* Gets the default catalog.
*
* @return the default catalog
*/
@Override
public String getDefaultCatalog() {
return this.defaultCatalog;
}
/**
* Gets the default query timeout that will be used for {@link java.sql.Statement Statement}s created from this
* connection. {@code null} means that the driver default will be used.
*
* @return The default query timeout in seconds.
* @deprecated Use {@link #getDefaultQueryTimeoutDuration()}.
*/
@Deprecated
public Integer getDefaultQueryTimeout() {
return defaultQueryTimeoutDuration == null ? null : Integer.valueOf((int) defaultQueryTimeoutDuration.getSeconds());
}
/**
* Gets the default query timeout that will be used for {@link java.sql.Statement Statement}s created from this
* connection. {@code null} means that the driver default will be used.
*
* @return The default query timeout Duration.
* @since 2.10.0
*/
public Duration getDefaultQueryTimeoutDuration() {
return defaultQueryTimeoutDuration;
}
/**
* Gets the default readOnly property.
*
* @return true if connections are readOnly by default
*/
@Override
public Boolean getDefaultReadOnly() {
return defaultReadOnly;
}
/**
* Gets the default schema.
*
* @return the default schema.
* @since 2.5.0
*/
@Override
public String getDefaultSchema() {
return this.defaultSchema;
}
/**
* Gets the default transaction isolation state of returned connections.
*
* @return the default value for transaction isolation state
* @see Connection#getTransactionIsolation
*/
@Override
public int getDefaultTransactionIsolation() {
return this.defaultTransactionIsolation;
}
/**
* Gets the set of SQL State codes that are not considered fatal disconnection codes.
* <p>
* This method returns the set of SQL State codes that have been specified to be ignored
* when determining if a {@link SQLException} signals a disconnection. These codes will not
* trigger a disconnection even if they match other disconnection criteria.
* </p>
*
* @return a set of SQL State codes that should be ignored for disconnection checks, or an empty set if none have been specified.
* @since 2.13.0
*/
public Set<String> getDisconnectionIgnoreSqlCodes() {
final Set<String> result = disconnectionIgnoreSqlCodes;
return result == null ? Collections.emptySet() : result;
}
/**
* Provides the same data as {@link #getDisconnectionIgnoreSqlCodes()} but in an array, so it is accessible via JMX.
*
* @since 2.13.0
*/
@Override
public String[] getDisconnectionIgnoreSqlCodesAsArray() {
return getDisconnectionIgnoreSqlCodes().toArray(Utils.EMPTY_STRING_ARRAY);
}
/**
* Gets the set of SQL State codes considered to signal fatal conditions.
*
* @return fatal disconnection state codes
* @see #setDisconnectionSqlCodes(Collection)
* @since 2.1
*/
public Set<String> getDisconnectionSqlCodes() {
final Set<String> result = disconnectionSqlCodes;
return result == null ? Collections.emptySet() : result;
}
/**
* Provides the same data as {@link #getDisconnectionSqlCodes} but in an array so it is accessible via JMX.
*
* @since 2.1
*/
@Override
public String[] getDisconnectionSqlCodesAsArray() {
return getDisconnectionSqlCodes().toArray(Utils.EMPTY_STRING_ARRAY);
}
/**
* Gets the JDBC Driver that has been configured for use by this pool.
* <p>
* Note: This getter only returns the last value set by a call to {@link #setDriver(Driver)}. It does not return any
* driver instance that may have been created from the value set via {@link #setDriverClassName(String)}.
* </p>
*
* @return the JDBC Driver that has been configured for use by this pool
*/
public synchronized Driver getDriver() {
return driver;
}
/**
* Gets the class loader specified for loading the JDBC driver. Returns {@code null} if no class loader has
* been explicitly specified.
* <p>
* Note: This getter only returns the last value set by a call to {@link #setDriverClassLoader(ClassLoader)}. It
* does not return the class loader of any driver that may have been set via {@link #setDriver(Driver)}.
* </p>
*
* @return The class loader specified for loading the JDBC driver.
*/
public synchronized ClassLoader getDriverClassLoader() {
return this.driverClassLoader;
}
/**
* Gets the JDBC driver class name.
* <p>
* Note: This getter only returns the last value set by a call to {@link #setDriverClassName(String)}. It does not
* return the class name of any driver that may have been set via {@link #setDriver(Driver)}.
* </p>
*
* @return the JDBC driver class name
*/
@Override
public synchronized String getDriverClassName() {
return this.driverClassName;
}
/**
* Gets the value of the {code durationBetweenEvictionRuns} property.
*
* @return the time (in milliseconds) between evictor runs
* @see #setDurationBetweenEvictionRuns(Duration)
* @since 2.10.0
*/
public synchronized Duration getDurationBetweenEvictionRuns() {
return this.durationBetweenEvictionRuns;
}
/**
* Gets the value of the flag that controls whether or not connections being returned to the pool will be checked
* and configured with {@link Connection#setAutoCommit(boolean) Connection.setAutoCommit(true)} if the auto commit
* setting is {@code false} when the connection is returned. It is {@code true} by default.
*
* @return Whether or not connections being returned to the pool will be checked and configured with auto-commit.
* @deprecated Use {@link #getAutoCommitOnReturn()}.
*/
@Deprecated
public boolean getEnableAutoCommitOnReturn() {
return autoCommitOnReturn;
}
/**
* Gets the EvictionPolicy implementation in use with this connection pool.
*
* @return The EvictionPolicy implementation in use with this connection pool.
*/
public synchronized String getEvictionPolicyClassName() {
return evictionPolicyClassName;
}
/**
* True means that validation will fail immediately for connections that have previously thrown SQLExceptions with
* SQL State indicating fatal disconnection errors.
*
* @return true if connections created by this datasource will fast fail validation.
* @see #setDisconnectionSqlCodes(Collection)
* @see #setDisconnectionIgnoreSqlCodes(Collection)
* @since 2.1
*/
@Override
public boolean getFastFailValidation() {
return fastFailValidation;
}
/**
* Gets the initial size of the connection pool.
*
* @return the number of connections created when the pool is initialized
*/
@Override
public synchronized int getInitialSize() {
return this.initialSize;
}
/**
* Gets the JMX name that has been requested for this DataSource. If the requested name is not valid, an
* alternative may be chosen.
*
* @return The JMX name that has been requested for this DataSource.
*/
public String getJmxName() {
return jmxName;
}
/**
* Gets the LIFO property.
*
* @return true if connection pool behaves as a LIFO queue.
*/
@Override
public synchronized boolean getLifo() {
return this.lifo;
}
/**
* Flag to log stack traces for application code which abandoned a Statement or Connection.
* <p>
* Defaults to false.
* </p>
* <p>
* Logging of abandoned Statements and Connections adds overhead for every Connection open or new Statement because
* a stack trace has to be generated.
* </p>
*/
@Override
public boolean getLogAbandoned() {
return abandonedConfig != null && abandonedConfig.getLogAbandoned();
}
/**
* When {@link #getMaxConnDuration()} is set to limit connection lifetime, this property determines whether or
* not log messages are generated when the pool closes connections due to maximum lifetime exceeded.
*
* @since 2.1
*/
@Override
public boolean getLogExpiredConnections() {
return logExpiredConnections;
}
/**
* <strong>BasicDataSource does NOT support this method.</strong>
*
* <p>
* Gets the login timeout (in seconds) for connecting to the database.
* </p>
* <p>
* Calls {@link #createDataSource()}, so has the side effect of initializing the connection pool.
* </p>
*
* @throws SQLException if a database access error occurs
* @throws UnsupportedOperationException If the DataSource implementation does not support the login timeout
* feature.
* @return login timeout in seconds
*/
@Override
public int getLoginTimeout() throws SQLException {
// This method isn't supported by the PoolingDataSource returned by the createDataSource
throw new UnsupportedOperationException("Not supported by BasicDataSource");
}
/**
* Gets the log writer being used by this data source.
* <p>
* Calls {@link #createDataSource()}, so has the side effect of initializing the connection pool.
* </p>
*
* @throws SQLException if a database access error occurs
* @return log writer in use
*/
@Override
public PrintWriter getLogWriter() throws SQLException {
return createDataSource().getLogWriter();
}
/**
* Gets the maximum permitted duration of a connection. A value of zero or less indicates an
* infinite lifetime.
* @return the maximum permitted duration of a connection.
* @since 2.10.0
*/
public Duration getMaxConnDuration() {
return maxConnDuration;
}
/**
* Gets the maximum permitted lifetime of a connection in milliseconds. A value of zero or less indicates an
* infinite lifetime.
* @deprecated Use {@link #getMaxConnDuration()}.
*/
@Override
@Deprecated
public long getMaxConnLifetimeMillis() {
return maxConnDuration.toMillis();
}
/**
* Gets the maximum number of connections that can remain idle in the pool. Excess idle connections are destroyed
* on return to the pool.
* <p>
* A negative value indicates that there is no limit
* </p>
*
* @return the maximum number of idle connections
*/
@Override
public synchronized int getMaxIdle() {
return this.maxIdle;
}
/**
* Gets the value of the {@code maxOpenPreparedStatements} property.
*
* @return the maximum number of open statements
*/
@Override
public synchronized int getMaxOpenPreparedStatements() {
return this.maxOpenPreparedStatements;
}
/**
* Gets the maximum number of active connections that can be allocated at the same time.
* <p>
* A negative number means that there is no limit.
* </p>
*
* @return the maximum number of active connections
*/
@Override
public synchronized int getMaxTotal() {
return this.maxTotal;
}
/**
* Gets the maximum Duration that the pool will wait for a connection to be returned before throwing an exception. A
* value less than or equal to zero means the pool is set to wait indefinitely.
*
* @return the maxWaitDuration property value.
* @since 2.10.0
*/
public synchronized Duration getMaxWaitDuration() {
return this.maxWaitDuration;
}
/**
* Gets the maximum number of milliseconds that the pool will wait for a connection to be returned before
* throwing an exception. A value less than or equal to zero means the pool is set to wait indefinitely.
*
* @return the maxWaitMillis property value.
* @deprecated Use {@link #getMaxWaitDuration()}.
*/
@Deprecated
@Override
public synchronized long getMaxWaitMillis() {
return this.maxWaitDuration.toMillis();
}
/**
* Gets the {code minEvictableIdleDuration} property.
*
* @return the value of the {code minEvictableIdleDuration} property
* @see #setMinEvictableIdle(Duration)
* @since 2.10.0
*/
public synchronized Duration getMinEvictableIdleDuration() {
return this.minEvictableIdleDuration;
}
/**
* Gets the {code minEvictableIdleDuration} property.
*
* @return the value of the {code minEvictableIdleDuration} property
* @see #setMinEvictableIdle(Duration)
* @deprecated Use {@link #getMinEvictableIdleDuration()}.
*/
@Deprecated
@Override
public synchronized long getMinEvictableIdleTimeMillis() {
return this.minEvictableIdleDuration.toMillis();
}
/**
* Gets the minimum number of idle connections in the pool. The pool attempts to ensure that minIdle connections
* are available when the idle object evictor runs. The value of this property has no effect unless
* {code durationBetweenEvictionRuns} has a positive value.
*
* @return the minimum number of idle connections
* @see GenericObjectPool#getMinIdle()
*/
@Override
public synchronized int getMinIdle() {
return this.minIdle;
}
/**
* [Read Only] The current number of active connections that have been allocated from this data source.
*
* @return the current number of active connections
*/
@Override
public int getNumActive() {
// Copy reference to avoid NPE if close happens after null check
final GenericObjectPool<PoolableConnection> pool = connectionPool;
return pool == null ? 0 : pool.getNumActive();
}
/**
* [Read Only] The current number of idle connections that are waiting to be allocated from this data source.
*
* @return the current number of idle connections
*/
@Override
public int getNumIdle() {
// Copy reference to avoid NPE if close happens after null check
final GenericObjectPool<PoolableConnection> pool = connectionPool;
return pool == null ? 0 : pool.getNumIdle();
}
/**
* Gets the value of the {code numTestsPerEvictionRun} property.
*
* @return the number of objects to examine during idle object evictor runs
* @see #setNumTestsPerEvictionRun(int)
*/
@Override
public synchronized int getNumTestsPerEvictionRun() {
return this.numTestsPerEvictionRun;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException();
}
/**
* Gets the password passed to the JDBC driver to establish connections.
*
* @return the connection password
* @deprecated Exposing passwords via JMX is an Information Exposure issue.
*/
@Deprecated
@Override
public String getPassword() {
return this.password;
}
/**
* Gets the registered JMX ObjectName.
*
* @return the registered JMX ObjectName.
*/
protected ObjectName getRegisteredJmxName() {
return ObjectNameWrapper.unwrap(registeredJmxObjectName);
}
/**
* Flag to remove abandoned connections if they exceed the removeAbandonedTimeout when borrowObject is invoked.
* <p>
* The default value is false.
* </p>
* <p>
* If set to true a connection is considered abandoned and eligible for removal if it has not been used for more
* than {@link #getRemoveAbandonedTimeoutDuration() removeAbandonedTimeout} seconds.
* </p>
* <p>
* Abandoned connections are identified and removed when {@link #getConnection()} is invoked and all of the
* following conditions hold:
* </p>
* <ul>
* <li>{@link #getRemoveAbandonedOnBorrow()}</li>
* <li>{@link #getNumActive()} > {@link #getMaxTotal()} - 3</li>
* <li>{@link #getNumIdle()} < 2</li>
* </ul>
*
* @see #getRemoveAbandonedTimeoutDuration()
*/
@Override
public boolean getRemoveAbandonedOnBorrow() {
return abandonedConfig != null && abandonedConfig.getRemoveAbandonedOnBorrow();
}
/**
* Flag to remove abandoned connections if they exceed the removeAbandonedTimeout during pool maintenance.
* <p>
* The default value is false.
* </p>
* <p>
* If set to true a connection is considered abandoned and eligible for removal if it has not been used for more
* than {@link #getRemoveAbandonedTimeoutDuration() removeAbandonedTimeout} seconds.
* </p>
*
* @see #getRemoveAbandonedTimeoutDuration()
*/
@Override
public boolean getRemoveAbandonedOnMaintenance() {
return abandonedConfig != null && abandonedConfig.getRemoveAbandonedOnMaintenance();
}
/**
* Gets the timeout in seconds before an abandoned connection can be removed.
* <p>
* Creating a Statement, PreparedStatement or CallableStatement or using one of these to execute a query (using one
* of the execute methods) resets the lastUsed property of the parent connection.
* </p>
* <p>
* Abandoned connection cleanup happens when:
* </p>
* <ul>
* <li>{@link #getRemoveAbandonedOnBorrow()} or {@link #getRemoveAbandonedOnMaintenance()} = true</li>
* <li>{@link #getNumIdle() numIdle} < 2</li>
* <li>{@link #getNumActive() numActive} > {@link #getMaxTotal() maxTotal} - 3</li>
* </ul>
* <p>
* The default value is 300 seconds.
* </p>
* @deprecated Use {@link #getRemoveAbandonedTimeoutDuration()}.
*/
@Deprecated
@Override
public int getRemoveAbandonedTimeout() {
return (int) getRemoveAbandonedTimeoutDuration().getSeconds();
}
/**
* Gets the timeout before an abandoned connection can be removed.
* <p>
* Creating a Statement, PreparedStatement or CallableStatement or using one of these to execute a query (using one
* of the execute methods) resets the lastUsed property of the parent connection.
* </p>
* <p>
* Abandoned connection cleanup happens when:
* </p>
* <ul>
* <li>{@link #getRemoveAbandonedOnBorrow()} or {@link #getRemoveAbandonedOnMaintenance()} = true</li>
* <li>{@link #getNumIdle() numIdle} < 2</li>
* <li>{@link #getNumActive() numActive} > {@link #getMaxTotal() maxTotal} - 3</li>
* </ul>
* <p>
* The default value is 300 seconds.
* </p>
* @return Timeout before an abandoned connection can be removed.
* @since 2.10.0
*/
public Duration getRemoveAbandonedTimeoutDuration() {
return abandonedConfig == null ? Duration.ofSeconds(300) : abandonedConfig.getRemoveAbandonedTimeoutDuration();
}
/**
* Gets the current value of the flag that controls whether a connection will be rolled back when it is returned to
* the pool if auto commit is not enabled and the connection is not read only.
*
* @return whether a connection will be rolled back when it is returned to the pool.
*/
public boolean getRollbackOnReturn() {
return rollbackOnReturn;
}
/**
* Gets the minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by
* the idle object evictor, with the extra condition that at least "minIdle" connections remain in the pool.
* <p>
* When {@link #getMinEvictableIdleTimeMillis() minEvictableIdleTimeMillis} is set to a positive value,
* minEvictableIdleTimeMillis is examined first by the idle connection evictor - i.e. when idle connections are
* visited by the evictor, idle time is first compared against {@code minEvictableIdleTimeMillis} (without
* considering the number of idle connections in the pool) and then against {@code softMinEvictableIdleTimeMillis},
* including the {@code minIdle}, constraint.
* </p>
*
* @return minimum amount of time a connection may sit idle in the pool before it is eligible for eviction, assuming
* there are minIdle idle connections in the pool
* @since 2.10.0
*/
public synchronized Duration getSoftMinEvictableIdleDuration() {
return softMinEvictableIdleDuration;
}
/**
* Gets the minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by
* the idle object evictor, with the extra condition that at least "minIdle" connections remain in the pool.
* <p>
* When {@link #getMinEvictableIdleTimeMillis() minEvictableIdleTimeMillis} is set to a positive value,
* minEvictableIdleTimeMillis is examined first by the idle connection evictor - i.e. when idle connections are
* visited by the evictor, idle time is first compared against {@code minEvictableIdleTimeMillis} (without
* considering the number of idle connections in the pool) and then against {@code softMinEvictableIdleTimeMillis},
* including the {@code minIdle}, constraint.
* </p>
*
* @return minimum amount of time a connection may sit idle in the pool before it is eligible for eviction, assuming
* there are minIdle idle connections in the pool
* @deprecated Use {@link #getSoftMinEvictableIdleDuration()}.
*/
@Deprecated
@Override
public synchronized long getSoftMinEvictableIdleTimeMillis() {
return softMinEvictableIdleDuration.toMillis();
}
/**
* Gets the {code testOnBorrow} property.
*
* @return true if objects are validated before being borrowed from the pool
* @see #setTestOnBorrow(boolean)
*/
@Override
public synchronized boolean getTestOnBorrow() {
return this.testOnBorrow;
}
/**
* Gets the {code testOnCreate} property.
*
* @return true if objects are validated immediately after they are created by the pool
* @see #setTestOnCreate(boolean)
*/
@Override
public synchronized boolean getTestOnCreate() {
return this.testOnCreate;
}
/**
* Gets the value of the {code testOnReturn} property.
*
* @return true if objects are validated before being returned to the pool
* @see #setTestOnReturn(boolean)
*/
public synchronized boolean getTestOnReturn() {
return this.testOnReturn;
}
/**
* Gets the value of the {code testWhileIdle} property.
*
* @return true if objects examined by the idle object evictor are validated
* @see #setTestWhileIdle(boolean)
*/
@Override
public synchronized boolean getTestWhileIdle() {
return this.testWhileIdle;
}
/**
* Gets the value of the {code durationBetweenEvictionRuns} property.
*
* @return the time (in milliseconds) between evictor runs
* @see #setDurationBetweenEvictionRuns(Duration)
* @deprecated Use {@link #getDurationBetweenEvictionRuns()}.
*/
@Deprecated
@Override
public synchronized long getTimeBetweenEvictionRunsMillis() {
return this.durationBetweenEvictionRuns.toMillis();
}
/**
* Gets the JDBC connection {code connectionString} property.
*
* @return the {code connectionString} passed to the JDBC driver to establish connections
*/
@Override
public synchronized String getUrl() {
return this.connectionString;
}
/**
* Gets the JDBC connection {code userName} property.
*
* @return the {code userName} passed to the JDBC driver to establish connections
* @deprecated Use {@link #getUserName()}.
*/
@Deprecated
@Override
public String getUsername() {
return this.userName;
}
/**
* Gets the validation query used to validate connections before returning them.
*
* @return the SQL validation query
* @see #setValidationQuery(String)
*/
@Override
public String getValidationQuery() {
return this.validationQuery;
}
/**
* Gets the validation query timeout.
*
* @return the timeout in seconds before connection validation queries fail.
* @deprecated Use {@link #getValidationQueryTimeoutDuration()}.
*/
@Deprecated
@Override
public int getValidationQueryTimeout() {
return (int) validationQueryTimeoutDuration.getSeconds();
}
/**
* Gets the validation query timeout.
*
* @return the timeout in seconds before connection validation queries fail.
*/
public Duration getValidationQueryTimeoutDuration() {
return validationQueryTimeoutDuration;
}
/**
* Manually invalidates a connection, effectively requesting the pool to try to close it, remove it from the pool
* and reclaim pool capacity.
*
* @param connection The Connection to invalidate.
* @throws IllegalStateException if invalidating the connection failed.
* @since 2.1
*/
public void invalidateConnection(final Connection connection) throws IllegalStateException {
if (connection == null) {
return;
}
if (connectionPool == null) {
throw new IllegalStateException("Cannot invalidate connection: ConnectionPool is null.");
}
final PoolableConnection poolableConnection;
try {
poolableConnection = connection.unwrap(PoolableConnection.class);
if (poolableConnection == null) {
throw new IllegalStateException(
"Cannot invalidate connection: Connection is not a poolable connection.");
}
} catch (final SQLException e) {
throw new IllegalStateException("Cannot invalidate connection: Unwrapping poolable connection failed.", e);
}
try {
connectionPool.invalidateObject(poolableConnection);
} catch (final Exception e) {
throw new IllegalStateException("Invalidating connection threw unexpected exception", e);
}
}
/**
* Gets the value of the accessToUnderlyingConnectionAllowed property.
*
* @return true if access to the underlying connection is allowed, false otherwise.
*/
@Override
public synchronized boolean isAccessToUnderlyingConnectionAllowed() {
return this.accessToUnderlyingConnectionAllowed;
}
/**
* Returns true if the statement pool is cleared when the connection is returned to its pool.
*
* @return true if the statement pool is cleared at connection return
* @since 2.8.0
*/
@Override
public boolean isClearStatementPoolOnReturn() {
return clearStatementPoolOnReturn;
}
/**
* If true, this data source is closed and no more connections can be retrieved from this data source.
*
* @return true, if the data source is closed; false otherwise
*/
@Override
public synchronized boolean isClosed() {
return closed;
}
/**
* Delegates in a null-safe manner to {@link String#isEmpty()}.
*
* @param value the string to test, may be null.
* @return boolean false if value is null, otherwise {@link String#isEmpty()}.
*/
private boolean isEmpty(final String value) {
return value == null || value.trim().isEmpty();
}
/**
* Returns true if we are pooling statements.
*
* @return true if prepared and callable statements are pooled
*/
@Override
public synchronized boolean isPoolPreparedStatements() {
return this.poolPreparedStatements;
}
@Override
public boolean isWrapperFor(final Class<?> iface) throws SQLException {
return iface != null && iface.isInstance(this);
}
private void jmxRegister() {
// Return immediately if this DataSource has already been registered
if (registeredJmxObjectName != null) {
return;
}
// Return immediately if no JMX name has been specified
final String requestedName = getJmxName();
if (requestedName == null) {
return;
}
registeredJmxObjectName = registerJmxObjectName(requestedName, null);
try {
final StandardMBean standardMBean = new StandardMBean(this, DataSourceMXBean.class);
registeredJmxObjectName.registerMBean(standardMBean);
} catch (final NotCompliantMBeanException e) {
log.warn("The requested JMX name [" + requestedName + "] was not valid and will be ignored.");
}
}
/**
* Logs the given message.
*
* @param message the message to log.
*/
protected void log(final String message) {
if (logWriter != null) {
logWriter.println(message);
}
}
/**
* Logs the given message and throwable.
*
* @param message value to be log.
* @param throwable the throwable.
* @since 2.7.0
*/
protected void log(final String message, final Throwable throwable) {
if (logWriter != null) {
logWriter.println(message);
throwable.printStackTrace(logWriter);
}
}
@Override
public void postDeregister() {
// NO-OP
}
@Override
public void postRegister(final Boolean registrationDone) {
// NO-OP
}
@Override
public void preDeregister() throws Exception {
// NO-OP
}
@Override
public ObjectName preRegister(final MBeanServer server, final ObjectName objectName) {
registeredJmxObjectName = registerJmxObjectName(getJmxName(), objectName);
return ObjectNameWrapper.unwrap(registeredJmxObjectName);
}
private ObjectNameWrapper registerJmxObjectName(final String requestedName, final ObjectName objectName) {
ObjectNameWrapper objectNameWrapper = null;
if (requestedName != null) {
try {
objectNameWrapper = ObjectNameWrapper.wrap(requestedName);
} catch (final MalformedObjectNameException e) {
log.warn("The requested JMX name '" + requestedName + "' was not valid and will be ignored.");
}
}
if (objectNameWrapper == null) {
objectNameWrapper = ObjectNameWrapper.wrap(objectName);
}
return objectNameWrapper;
}
/**
* Removes a custom connection property.
*
* @param name Name of the custom connection property to remove
* @see #addConnectionProperty(String, String)
*/
public void removeConnectionProperty(final String name) {
connectionProperties.remove(name);
}
/**
* Restarts the datasource.
* <p>
* This method calls {@link #close()} and {@link #start()} in sequence within synchronized scope so any
* connection requests that come in while the datasource is shutting down will be served by the new pool.
* <p>
* Idle connections that are stored in the connection pool when this method is invoked are closed, but
* connections that are checked out to clients when this method is invoked are not affected. When client
* applications subsequently invoke {@link Connection#close()} to return these connections to the pool, the
* underlying JDBC connections are closed. These connections do not count in {@link #getMaxTotal()} or
* {@link #getNumActive()} after invoking this method. For example, if there are 3 connections checked out by
* clients when {@link #restart()} is invoked, after this method is called, {@link #getNumActive()} will
* return 0 and up to {@link #getMaxTotal()} + 3 connections may be open until the connections sourced from
* the original pool are returned.
* <p>
* The new connection pool created by this method is initialized with currently set configuration properties.
*
* @throws SQLException if an error occurs initializing the datasource
*/
@Override
public synchronized void restart() throws SQLException {
close();
start();
}
private <T> void setAbandoned(final BiConsumer<AbandonedConfig, T> consumer, final T object) {
if (abandonedConfig == null) {
abandonedConfig = new AbandonedConfig();
}
consumer.accept(abandonedConfig, object);
final GenericObjectPool<?> gop = this.connectionPool;
if (gop != null) {
gop.setAbandonedConfig(abandonedConfig);
}
}
/**
* Sets the print writer to be used by this configuration to log information on abandoned objects.
*
* @param logWriter The new log writer
*/
public void setAbandonedLogWriter(final PrintWriter logWriter) {
setAbandoned(AbandonedConfig::setLogWriter, logWriter);
}
/**
* If the connection pool implements {@link org.apache.tomcat.dbcp.pool2.UsageTracking UsageTracking}, configure whether
* the connection pool should record a stack trace every time a method is called on a pooled connection and retain
* the most recent stack trace to aid debugging of abandoned connections.
*
* @param usageTracking A value of {@code true} will enable the recording of a stack trace on every use of a
* pooled connection
*/
public void setAbandonedUsageTracking(final boolean usageTracking) {
setAbandoned(AbandonedConfig::setUseUsageTracking, Boolean.valueOf(usageTracking));
}
/**
* Sets the value of the accessToUnderlyingConnectionAllowed property. It controls if the PoolGuard allows access to
* the underlying connection. (Default: false)
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param allow Access to the underlying connection is granted when true.
*/
public synchronized void setAccessToUnderlyingConnectionAllowed(final boolean allow) {
this.accessToUnderlyingConnectionAllowed = allow;
}
/**
* Sets the value of the flag that controls whether or not connections being returned to the pool will be checked
* and configured with {@link Connection#setAutoCommit(boolean) Connection.setAutoCommit(true)} if the auto commit
* setting is {@code false} when the connection is returned. It is {@code true} by default.
*
* @param autoCommitOnReturn Whether or not connections being returned to the pool will be checked and configured
* with auto-commit.
* @since 2.6.0
*/
public void setAutoCommitOnReturn(final boolean autoCommitOnReturn) {
this.autoCommitOnReturn = autoCommitOnReturn;
}
/**
* Sets the state caching flag.
*
* @param cacheState The new value for the state caching flag
*/
public void setCacheState(final boolean cacheState) {
this.cacheState = cacheState;
}
/**
* Sets whether the pool of statements (which was enabled with {@link #setPoolPreparedStatements(boolean)}) should
* be cleared when the connection is returned to its pool. Default is false.
*
* @param clearStatementPoolOnReturn clear or not
* @since 2.8.0
*/
public void setClearStatementPoolOnReturn(final boolean clearStatementPoolOnReturn) {
this.clearStatementPoolOnReturn = clearStatementPoolOnReturn;
}
/**
* Sets the ConnectionFactory class name.
*
* @param connectionFactoryClassName A class name.
* @since 2.7.0
*/
public void setConnectionFactoryClassName(final String connectionFactoryClassName) {
this.connectionFactoryClassName = isEmpty(connectionFactoryClassName) ? null : connectionFactoryClassName;
}
/**
* Sets the collection of SQL statements to be executed when a physical connection is first created.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param connectionInitSqls Collection of SQL statements to execute on connection creation
*/
public void setConnectionInitSqls(final Collection<String> connectionInitSqls) {
final List<String> collect = Utils.isEmpty(connectionInitSqls) ? null
: connectionInitSqls.stream().filter(s -> !isEmpty(s)).collect(Collectors.toList());
this.connectionInitSqls = Utils.isEmpty(collect) ? null : collect;
}
/**
* Sets the list of SQL statements to be executed when a physical connection is first created.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param connectionInitSqls List of SQL statements to execute on connection creation
* @since 2.12.0
*/
public void setConnectionInitSqls(final List<String> connectionInitSqls) {
setConnectionInitSqls((Collection<String>) connectionInitSqls);
}
private <T> void setConnectionPool(final BiConsumer<GenericObjectPool<PoolableConnection>, T> consumer, final T object) {
if (connectionPool != null) {
consumer.accept(connectionPool, object);
}
}
/**
* Sets the connection properties passed to driver.connect(...).
* <p>
* Format of the string must be [propertyName=property;]*
* </p>
* <p>
* NOTE - The "user" and "password" properties will be added explicitly, so they do not need to be included here.
* </p>
*
* @param connectionProperties the connection properties used to create new connections
*/
public void setConnectionProperties(final String connectionProperties) {
Objects.requireNonNull(connectionProperties, "connectionProperties");
final String[] entries = connectionProperties.split(";");
final Properties properties = new Properties();
Stream.of(entries).filter(e -> !e.isEmpty()).forEach(entry -> {
final int index = entry.indexOf('=');
if (index > 0) {
final String name = entry.substring(0, index);
final String value = entry.substring(index + 1);
properties.setProperty(name, value);
} else {
// no value is empty string which is how
// java.util.Properties works
properties.setProperty(entry, "");
}
});
this.connectionProperties = properties;
}
/**
* Sets default auto-commit state of connections returned by this datasource.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param defaultAutoCommit default auto-commit value
*/
public void setDefaultAutoCommit(final Boolean defaultAutoCommit) {
this.defaultAutoCommit = defaultAutoCommit;
}
/**
* Sets the default catalog.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param defaultCatalog the default catalog
*/
public void setDefaultCatalog(final String defaultCatalog) {
this.defaultCatalog = isEmpty(defaultCatalog) ? null : defaultCatalog;
}
/**
* Sets the default query timeout that will be used for {@link java.sql.Statement Statement}s created from this
* connection. {@code null} means that the driver default will be used.
*
* @param defaultQueryTimeoutDuration The default query timeout Duration.
* @since 2.10.0
*/
public void setDefaultQueryTimeout(final Duration defaultQueryTimeoutDuration) {
this.defaultQueryTimeoutDuration = defaultQueryTimeoutDuration;
}
/**
* Sets the default query timeout that will be used for {@link java.sql.Statement Statement}s created from this
* connection. {@code null} means that the driver default will be used.
*
* @param defaultQueryTimeoutSeconds The default query timeout in seconds.
* @deprecated Use {@link #setDefaultQueryTimeout(Duration)}.
*/
@Deprecated
public void setDefaultQueryTimeout(final Integer defaultQueryTimeoutSeconds) {
this.defaultQueryTimeoutDuration = defaultQueryTimeoutSeconds == null ? null : Duration.ofSeconds(defaultQueryTimeoutSeconds.longValue());
}
/**
* Sets defaultReadonly property.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param defaultReadOnly default read-only value
*/
public void setDefaultReadOnly(final Boolean defaultReadOnly) {
this.defaultReadOnly = defaultReadOnly;
}
/**
* Sets the default schema.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param defaultSchema the default catalog
* @since 2.5.0
*/
public void setDefaultSchema(final String defaultSchema) {
this.defaultSchema = isEmpty(defaultSchema) ? null : defaultSchema;
}
/**
* Sets the default transaction isolation state for returned connections.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param defaultTransactionIsolation the default transaction isolation state
* @see Connection#getTransactionIsolation
*/
public void setDefaultTransactionIsolation(final int defaultTransactionIsolation) {
this.defaultTransactionIsolation = defaultTransactionIsolation;
}
/**
* Sets the SQL State codes that should be ignored when determining fatal disconnection conditions.
* <p>
* This method allows you to specify a collection of SQL State codes that will be excluded from
* disconnection checks. These codes will not trigger the "fatally disconnected" status even if they
* match the typical disconnection criteria. This can be useful in scenarios where certain SQL State
* codes (e.g., specific codes starting with "08") are known to be non-fatal in your environment.
* </p>
* <p>
* The effect of this method is similar to the one described in {@link #setDisconnectionSqlCodes(Collection)},
* but instead of setting codes that signal fatal disconnections, it defines codes that should be ignored
* during such checks.
* </p>
* <p>
* Note: This method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@code getConnection, setLogwriter, setLoginTimeout,
* getLoginTimeout, getLogWriter}.
* </p>
*
* @param disconnectionIgnoreSqlCodes SQL State codes that should be ignored in disconnection checks
* @throws IllegalArgumentException if any SQL state codes overlap with those in {@link #disconnectionSqlCodes}.
* @since 2.13.0
*/
public void setDisconnectionIgnoreSqlCodes(final Collection<String> disconnectionIgnoreSqlCodes) {
Utils.checkSqlCodes(disconnectionIgnoreSqlCodes, this.disconnectionSqlCodes);
final Set<String> collect = Utils.isEmpty(disconnectionIgnoreSqlCodes) ? null
: disconnectionIgnoreSqlCodes.stream().filter(s -> !isEmpty(s)).collect(toLinkedHashSet());
this.disconnectionIgnoreSqlCodes = Utils.isEmpty(collect) ? null : collect;
}
/**
* Sets the SQL State codes considered to signal fatal conditions.
* <p>
* Overrides the defaults in {@link Utils#getDisconnectionSqlCodes()} (plus anything starting with
* {@link Utils#DISCONNECTION_SQL_CODE_PREFIX}). If this property is non-null and {@link #getFastFailValidation()}
* is {@code true}, whenever connections created by this datasource generate exceptions with SQL State codes in this
* list, they will be marked as "fatally disconnected" and subsequent validations will fail fast (no attempt at
* isValid or validation query).
* </p>
* <p>
* If {@link #getFastFailValidation()} is {@code false} setting this property has no effect.
* </p>
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@code getConnection, setLogwriter,
* setLoginTimeout, getLoginTimeout, getLogWriter}.
* </p>
*
* @param disconnectionSqlCodes SQL State codes considered to signal fatal conditions
* @throws IllegalArgumentException if any SQL state codes overlap with those in {@link #disconnectionIgnoreSqlCodes}.
* @since 2.1
*/
public void setDisconnectionSqlCodes(final Collection<String> disconnectionSqlCodes) {
Utils.checkSqlCodes(disconnectionSqlCodes, this.disconnectionIgnoreSqlCodes);
final Set<String> collect = Utils.isEmpty(disconnectionSqlCodes) ? null
: disconnectionSqlCodes.stream().filter(s -> !isEmpty(s)).collect(toLinkedHashSet());
this.disconnectionSqlCodes = Utils.isEmpty(collect) ? null : collect;
}
/**
* Sets the JDBC Driver instance to use for this pool.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param driver The JDBC Driver instance to use for this pool.
*/
public synchronized void setDriver(final Driver driver) {
this.driver = driver;
}
/**
* Sets the class loader to be used to load the JDBC driver.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param driverClassLoader the class loader with which to load the JDBC driver
*/
public synchronized void setDriverClassLoader(final ClassLoader driverClassLoader) {
this.driverClassLoader = driverClassLoader;
}
/**
* Sets the JDBC driver class name.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param driverClassName the class name of the JDBC driver
*/
public synchronized void setDriverClassName(final String driverClassName) {
this.driverClassName = isEmpty(driverClassName) ? null : driverClassName;
}
/**
* Sets the {code durationBetweenEvictionRuns} property.
*
* @param timeBetweenEvictionRunsMillis the new time between evictor runs
* @see #setDurationBetweenEvictionRuns(Duration)
* @since 2.10.0
*/
public synchronized void setDurationBetweenEvictionRuns(final Duration timeBetweenEvictionRunsMillis) {
this.durationBetweenEvictionRuns = timeBetweenEvictionRunsMillis;
setConnectionPool(GenericObjectPool::setDurationBetweenEvictionRuns, timeBetweenEvictionRunsMillis);
}
/**
* Sets the value of the flag that controls whether or not connections being returned to the pool will be checked
* and configured with {@link Connection#setAutoCommit(boolean) Connection.setAutoCommit(true)} if the auto commit
* setting is {@code false} when the connection is returned. It is {@code true} by default.
*
* @param autoCommitOnReturn Whether or not connections being returned to the pool will be checked and configured
* with auto-commit.
* @deprecated Use {@link #setAutoCommitOnReturn(boolean)}.
*/
@Deprecated
public void setEnableAutoCommitOnReturn(final boolean autoCommitOnReturn) {
this.autoCommitOnReturn = autoCommitOnReturn;
}
/**
* Sets the EvictionPolicy implementation to use with this connection pool.
*
* @param evictionPolicyClassName The fully qualified class name of the EvictionPolicy implementation
*/
public synchronized void setEvictionPolicyClassName(final String evictionPolicyClassName) {
setConnectionPool(GenericObjectPool::setEvictionPolicyClassName, evictionPolicyClassName);
this.evictionPolicyClassName = evictionPolicyClassName;
}
/**
* Sets whether connections created by this factory will fast fail validation.
*
* @param fastFailValidation true means connections created by this factory will fast fail validation.
* @see #getFastFailValidation()
* @since 2.1
*/
public void setFastFailValidation(final boolean fastFailValidation) {
this.fastFailValidation = fastFailValidation;
}
/**
* Sets the initial size of the connection pool.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param initialSize the number of connections created when the pool is initialized
*/
public synchronized void setInitialSize(final int initialSize) {
this.initialSize = initialSize;
}
/**
* Sets the JMX name that has been requested for this DataSource. If the requested name is not valid, an alternative
* may be chosen. This DataSource will attempt to register itself using this name. If another component registers
* this DataSource with JMX and this name is valid this name will be used in preference to any specified by the
* other component.
*
* @param jmxName The JMX name that has been requested for this DataSource
*/
public void setJmxName(final String jmxName) {
this.jmxName = jmxName;
}
/**
* Sets the LIFO property. True means the pool behaves as a LIFO queue; false means FIFO.
*
* @param lifo the new value for the LIFO property
*/
public synchronized void setLifo(final boolean lifo) {
this.lifo = lifo;
setConnectionPool(GenericObjectPool::setLifo, Boolean.valueOf(lifo));
}
/**
* Sets whether to log abandoned resources.
*
* @param logAbandoned new logAbandoned property value
*/
public void setLogAbandoned(final boolean logAbandoned) {
setAbandoned(AbandonedConfig::setLogAbandoned, Boolean.valueOf(logAbandoned));
}
/**
* When {@link #getMaxConnDuration()} is set to limit connection lifetime, this property determines whether or
* not log messages are generated when the pool closes connections due to maximum lifetime exceeded. Set this
* property to false to suppress log messages when connections expire.
*
* @param logExpiredConnections Whether or not log messages are generated when the pool closes connections due to
* maximum lifetime exceeded.
*/
public void setLogExpiredConnections(final boolean logExpiredConnections) {
this.logExpiredConnections = logExpiredConnections;
}
/**
* <strong>BasicDataSource does NOT support this method. </strong>
*
* <p>
* Sets the login timeout (in seconds) for connecting to the database.
* </p>
* <p>
* Calls {@link #createDataSource()}, so has the side effect of initializing the connection pool.
* </p>
*
* @param loginTimeout The new login timeout, or zero for no timeout
* @throws UnsupportedOperationException If the DataSource implementation does not support the login timeout
* feature.
* @throws SQLException if a database access error occurs
*/
@Override
public void setLoginTimeout(final int loginTimeout) throws SQLException {
// This method isn't supported by the PoolingDataSource returned by the
// createDataSource
throw new UnsupportedOperationException("Not supported by BasicDataSource");
}
/**
* Sets the log writer being used by this data source.
* <p>
* Calls {@link #createDataSource()}, so has the side effect of initializing the connection pool.
* </p>
*
* @param logWriter The new log writer
* @throws SQLException if a database access error occurs
*/
@Override
public void setLogWriter(final PrintWriter logWriter) throws SQLException {
createDataSource().setLogWriter(logWriter);
this.logWriter = logWriter;
}
/**
* Sets the maximum permitted lifetime of a connection. A value of zero or less indicates an
* infinite lifetime.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param maxConnDuration The maximum permitted lifetime of a connection.
* @since 2.10.0
*/
public void setMaxConn(final Duration maxConnDuration) {
this.maxConnDuration = maxConnDuration;
}
/**
* Sets the maximum permitted lifetime of a connection in milliseconds. A value of zero or less indicates an
* infinite lifetime.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param maxConnLifetimeMillis The maximum permitted lifetime of a connection in milliseconds.
* @deprecated Use {@link #setMaxConn(Duration)}.
*/
@Deprecated
public void setMaxConnLifetimeMillis(final long maxConnLifetimeMillis) {
this.maxConnDuration = Duration.ofMillis(maxConnLifetimeMillis);
}
/**
* Sets the maximum number of connections that can remain idle in the pool. Excess idle connections are destroyed on
* return to the pool.
*
* @see #getMaxIdle()
* @param maxIdle the new value for maxIdle
*/
public synchronized void setMaxIdle(final int maxIdle) {
this.maxIdle = maxIdle;
setConnectionPool(GenericObjectPool::setMaxIdle, Integer.valueOf(maxIdle));
}
/**
* Sets the value of the {@code maxOpenPreparedStatements} property.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param maxOpenStatements the new maximum number of prepared statements
*/
public synchronized void setMaxOpenPreparedStatements(final int maxOpenStatements) {
this.maxOpenPreparedStatements = maxOpenStatements;
}
/**
* Sets the maximum total number of idle and borrows connections that can be active at the same time. Use a negative
* value for no limit.
*
* @param maxTotal the new value for maxTotal
* @see #getMaxTotal()
*/
public synchronized void setMaxTotal(final int maxTotal) {
this.maxTotal = maxTotal;
setConnectionPool(GenericObjectPool::setMaxTotal, Integer.valueOf(maxTotal));
}
/**
* Sets the MaxWaitMillis property. Use -1 to make the pool wait indefinitely.
*
* @param maxWaitDuration the new value for MaxWaitMillis
* @see #getMaxWaitDuration()
* @since 2.10.0
*/
public synchronized void setMaxWait(final Duration maxWaitDuration) {
this.maxWaitDuration = maxWaitDuration;
setConnectionPool(GenericObjectPool::setMaxWait, maxWaitDuration);
}
/**
* Sets the MaxWaitMillis property. Use -1 to make the pool wait indefinitely.
*
* @param maxWaitMillis the new value for MaxWaitMillis
* @see #getMaxWaitDuration()
* @deprecated {@link #setMaxWait(Duration)}.
*/
@Deprecated
public synchronized void setMaxWaitMillis(final long maxWaitMillis) {
setMaxWait(Duration.ofMillis(maxWaitMillis));
}
/**
* Sets the {code minEvictableIdleDuration} property.
*
* @param minEvictableIdleDuration the minimum amount of time an object may sit idle in the pool
* @see #setMinEvictableIdle(Duration)
* @since 2.10.0
*/
public synchronized void setMinEvictableIdle(final Duration minEvictableIdleDuration) {
this.minEvictableIdleDuration = minEvictableIdleDuration;
setConnectionPool(GenericObjectPool::setMinEvictableIdleDuration, minEvictableIdleDuration);
}
/**
* Sets the {code minEvictableIdleDuration} property.
*
* @param minEvictableIdleTimeMillis the minimum amount of time an object may sit idle in the pool
* @see #setMinEvictableIdle(Duration)
* @deprecated Use {@link #setMinEvictableIdle(Duration)}.
*/
@Deprecated
public synchronized void setMinEvictableIdleTimeMillis(final long minEvictableIdleTimeMillis) {
setMinEvictableIdle(Duration.ofMillis(minEvictableIdleTimeMillis));
}
/**
* Sets the minimum number of idle connections in the pool. The pool attempts to ensure that minIdle connections are
* available when the idle object evictor runs. The value of this property has no effect unless
* {code durationBetweenEvictionRuns} has a positive value.
*
* @param minIdle the new value for minIdle
* @see GenericObjectPool#setMinIdle(int)
*/
public synchronized void setMinIdle(final int minIdle) {
this.minIdle = minIdle;
setConnectionPool(GenericObjectPool::setMinIdle, Integer.valueOf(minIdle));
}
/**
* Sets the value of the {code numTestsPerEvictionRun} property.
*
* @param numTestsPerEvictionRun the new {code numTestsPerEvictionRun} value
* @see #setNumTestsPerEvictionRun(int)
*/
public synchronized void setNumTestsPerEvictionRun(final int numTestsPerEvictionRun) {
this.numTestsPerEvictionRun = numTestsPerEvictionRun;
setConnectionPool(GenericObjectPool::setNumTestsPerEvictionRun, Integer.valueOf(numTestsPerEvictionRun));
}
/**
* Sets the {code password}.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param password new value for the password
*/
public void setPassword(final String password) {
this.password = password;
}
/**
* Sets whether to pool statements or not.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param poolingStatements pooling on or off
*/
public synchronized void setPoolPreparedStatements(final boolean poolingStatements) {
this.poolPreparedStatements = poolingStatements;
}
/**
* Sets if connection level JMX tracking is requested for this DataSource. If true, each connection will be
* registered for tracking with JMX.
*
* @param registerConnectionMBean connection tracking requested for this DataSource.
*/
public void setRegisterConnectionMBean(final boolean registerConnectionMBean) {
this.registerConnectionMBean = registerConnectionMBean;
}
/**
* Sets abandoned connections may be removed when connections are borrowed from the pool.
*
* @param removeAbandonedOnBorrow true means abandoned connections may be removed when connections are borrowed from the pool.
* @see #getRemoveAbandonedOnBorrow()
*/
public void setRemoveAbandonedOnBorrow(final boolean removeAbandonedOnBorrow) {
setAbandoned(AbandonedConfig::setRemoveAbandonedOnBorrow, Boolean.valueOf(removeAbandonedOnBorrow));
}
/**
* Sets whether abandoned connections may be removed on pool maintenance.
*
* @param removeAbandonedOnMaintenance true means abandoned connections may be removed on pool maintenance.
* @see #getRemoveAbandonedOnMaintenance()
*/
public void setRemoveAbandonedOnMaintenance(final boolean removeAbandonedOnMaintenance) {
setAbandoned(AbandonedConfig::setRemoveAbandonedOnMaintenance, Boolean.valueOf(removeAbandonedOnMaintenance));
}
/**
* Sets the timeout before an abandoned connection can be removed.
* <p>
* Setting this property has no effect if {@link #getRemoveAbandonedOnBorrow()} and
* {code getRemoveAbandonedOnMaintenance()} are false.
* </p>
*
* @param removeAbandonedTimeout new abandoned timeout
* @see #getRemoveAbandonedTimeoutDuration()
* @see #getRemoveAbandonedOnBorrow()
* @see #getRemoveAbandonedOnMaintenance()
* @since 2.10.0
*/
public void setRemoveAbandonedTimeout(final Duration removeAbandonedTimeout) {
setAbandoned(AbandonedConfig::setRemoveAbandonedTimeout, removeAbandonedTimeout);
}
/**
* Sets the timeout in seconds before an abandoned connection can be removed.
* <p>
* Setting this property has no effect if {@link #getRemoveAbandonedOnBorrow()} and
* {@link #getRemoveAbandonedOnMaintenance()} are false.
* </p>
*
* @param removeAbandonedTimeout new abandoned timeout in seconds
* @see #getRemoveAbandonedTimeoutDuration()
* @see #getRemoveAbandonedOnBorrow()
* @see #getRemoveAbandonedOnMaintenance()
* @deprecated Use {@link #setRemoveAbandonedTimeout(Duration)}.
*/
@Deprecated
public void setRemoveAbandonedTimeout(final int removeAbandonedTimeout) {
setAbandoned(AbandonedConfig::setRemoveAbandonedTimeout, Duration.ofSeconds(removeAbandonedTimeout));
}
/**
* Sets the flag that controls if a connection will be rolled back when it is returned to the pool if auto commit is
* not enabled and the connection is not read only.
*
* @param rollbackOnReturn whether a connection will be rolled back when it is returned to the pool.
*/
public void setRollbackOnReturn(final boolean rollbackOnReturn) {
this.rollbackOnReturn = rollbackOnReturn;
}
/**
* Sets the minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by the
* idle object evictor, with the extra condition that at least "minIdle" connections remain in the pool.
*
* @param softMinEvictableIdleTimeMillis minimum amount of time a connection may sit idle in the pool before it is
* eligible for eviction, assuming there are minIdle idle connections in the
* pool.
* @see #getSoftMinEvictableIdleTimeMillis
* @since 2.10.0
*/
public synchronized void setSoftMinEvictableIdle(final Duration softMinEvictableIdleTimeMillis) {
this.softMinEvictableIdleDuration = softMinEvictableIdleTimeMillis;
setConnectionPool(GenericObjectPool::setSoftMinEvictableIdleDuration, softMinEvictableIdleTimeMillis);
}
/**
* Sets the minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by the
* idle object evictor, with the extra condition that at least "minIdle" connections remain in the pool.
*
* @param softMinEvictableIdleTimeMillis minimum amount of time a connection may sit idle in the pool before it is
* eligible for eviction, assuming there are minIdle idle connections in the
* pool.
* @see #getSoftMinEvictableIdleTimeMillis
* @deprecated Use {@link #setSoftMinEvictableIdle(Duration)}.
*/
@Deprecated
public synchronized void setSoftMinEvictableIdleTimeMillis(final long softMinEvictableIdleTimeMillis) {
setSoftMinEvictableIdle(Duration.ofMillis(softMinEvictableIdleTimeMillis));
}
/**
* Sets the {code testOnBorrow} property. This property determines whether or not the pool will validate objects
* before they are borrowed from the pool.
*
* @param testOnBorrow new value for testOnBorrow property
*/
public synchronized void setTestOnBorrow(final boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
setConnectionPool(GenericObjectPool::setTestOnBorrow, Boolean.valueOf(testOnBorrow));
}
/**
* Sets the {code testOnCreate} property. This property determines whether or not the pool will validate objects
* immediately after they are created by the pool
*
* @param testOnCreate new value for testOnCreate property
*/
public synchronized void setTestOnCreate(final boolean testOnCreate) {
this.testOnCreate = testOnCreate;
setConnectionPool(GenericObjectPool::setTestOnCreate, Boolean.valueOf(testOnCreate));
}
/**
* Sets the {@code testOnReturn} property. This property determines whether or not the pool will validate
* objects before they are returned to the pool.
*
* @param testOnReturn new value for testOnReturn property
*/
public synchronized void setTestOnReturn(final boolean testOnReturn) {
this.testOnReturn = testOnReturn;
setConnectionPool(GenericObjectPool::setTestOnReturn, Boolean.valueOf(testOnReturn));
}
/**
* Sets the {@code testWhileIdle} property. This property determines whether or not the idle object evictor
* will validate connections.
*
* @param testWhileIdle new value for testWhileIdle property
*/
public synchronized void setTestWhileIdle(final boolean testWhileIdle) {
this.testWhileIdle = testWhileIdle;
setConnectionPool(GenericObjectPool::setTestWhileIdle, Boolean.valueOf(testWhileIdle));
}
/**
* Sets the {code durationBetweenEvictionRuns} property.
*
* @param timeBetweenEvictionRunsMillis the new time between evictor runs
* @see #setDurationBetweenEvictionRuns(Duration)
* @deprecated Use {@link #setDurationBetweenEvictionRuns(Duration)}.
*/
@Deprecated
public synchronized void setTimeBetweenEvictionRunsMillis(final long timeBetweenEvictionRunsMillis) {
setDurationBetweenEvictionRuns(Duration.ofMillis(timeBetweenEvictionRunsMillis));
}
/**
* Sets the {code connection string}.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param connectionString the new value for the JDBC connection connectionString
*/
public synchronized void setUrl(final String connectionString) {
this.connectionString = connectionString;
}
/**
* Sets the {code userName}.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param userName the new value for the JDBC connection user name
*/
public void setUsername(final String userName) {
this.userName = userName;
}
/**
* Sets the {code validationQuery}.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param validationQuery the new value for the validation query
*/
public void setValidationQuery(final String validationQuery) {
this.validationQuery = isEmpty(validationQuery) ? null : validationQuery;
}
/**
* Sets the validation query timeout, the amount of time, in seconds, that connection validation will wait for a
* response from the database when executing a validation query. Use a value less than or equal to 0 for no timeout.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param validationQueryTimeoutDuration new validation query timeout value in seconds
* @since 2.10.0
*/
public void setValidationQueryTimeout(final Duration validationQueryTimeoutDuration) {
this.validationQueryTimeoutDuration = validationQueryTimeoutDuration;
}
/**
* Sets the validation query timeout, the amount of time, in seconds, that connection validation will wait for a
* response from the database when executing a validation query. Use a value less than or equal to 0 for no timeout.
* <p>
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
* time one of the following methods is invoked: {@link #getConnection()}, {@link #setLogWriter(PrintWriter)},
* {@link #setLoginTimeout(int)}, {@link #getLoginTimeout()}, {@link #getLogWriter()}.
* </p>
*
* @param validationQueryTimeoutSeconds new validation query timeout value in seconds
* @deprecated Use {@link #setValidationQueryTimeout(Duration)}.
*/
@Deprecated
public void setValidationQueryTimeout(final int validationQueryTimeoutSeconds) {
this.validationQueryTimeoutDuration = Duration.ofSeconds(validationQueryTimeoutSeconds);
}
/**
* Starts the datasource.
* <p>
* It is not necessary to call this method before using a newly created BasicDataSource instance, but
* calling it in that context causes the datasource to be immediately initialized (instead of waiting for
* the first {@link #getConnection()} request). Its primary use is to restart and reinitialize a
* datasource that has been closed.
* <p>
* When this method is called after {@link #close()}, connections checked out by clients
* before the datasource was stopped do not count in {@link #getMaxTotal()} or {@link #getNumActive()}.
* For example, if there are 3 connections checked out by clients when {@link #close()} is invoked and they are
* not returned before {@link #start()} is invoked, after this method is called, {@link #getNumActive()} will
* return 0. These connections will be physically closed when they are returned, but they will not count against
* the maximum allowed in the newly started datasource.
*
* @throws SQLException if an error occurs initializing the datasource
*/
@Override
public synchronized void start() throws SQLException {
closed = false;
createDataSource();
}
/**
* Starts the connection pool maintenance task, if configured.
*/
protected void startPoolMaintenance() {
if (connectionPool != null && durationBetweenEvictionRuns.compareTo(Duration.ZERO) > 0) {
connectionPool.setDurationBetweenEvictionRuns(durationBetweenEvictionRuns);
}
}
private Collector<String, ?, LinkedHashSet<String>> toLinkedHashSet() {
return Collectors.toCollection(LinkedHashSet::new);
}
@Override
public <T> T unwrap(final Class<T> iface) throws SQLException {
if (isWrapperFor(iface)) {
return iface.cast(this);
}
throw new SQLException(this + " is not a wrapper for " + iface);
}
private void updateJmxName(final GenericObjectPoolConfig<?> config) {
if (registeredJmxObjectName == null) {
return;
}
final StringBuilder base = new StringBuilder(registeredJmxObjectName.toString());
base.append(Constants.JMX_CONNECTION_POOL_BASE_EXT);
config.setJmxNameBase(base.toString());
config.setJmxNamePrefix(Constants.JMX_CONNECTION_POOL_PREFIX);
}
}
|