1 24 25 package org.objectweb.cjdbc.controller.loadbalancer.singledb; 26 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.sql.Savepoint ; 30 31 import org.objectweb.cjdbc.common.exceptions.BadConnectionException; 32 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException; 33 import org.objectweb.cjdbc.common.i18n.Translate; 34 import org.objectweb.cjdbc.common.log.Trace; 35 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest; 36 import org.objectweb.cjdbc.common.sql.ParsingGranularities; 37 import org.objectweb.cjdbc.common.sql.SelectRequest; 38 import org.objectweb.cjdbc.common.sql.StoredProcedure; 39 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 40 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 41 import org.objectweb.cjdbc.controller.cache.metadata.MetadataCache; 42 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager; 43 import org.objectweb.cjdbc.controller.loadbalancer.AbstractLoadBalancer; 44 import org.objectweb.cjdbc.controller.loadbalancer.AllBackendsFailedException; 45 import org.objectweb.cjdbc.controller.requestmanager.RAIDbLevels; 46 import org.objectweb.cjdbc.controller.requestmanager.TransactionMarkerMetaData; 47 import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet; 48 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase; 49 50 63 public class SingleDB extends AbstractLoadBalancer 64 { 65 75 private DatabaseBackend backend; 76 77 private static Trace logger = Trace 78 .getLogger("org.objectweb.cjdbc.controller.loadbalancer.SingleDB"); 79 80 83 84 91 public SingleDB(VirtualDatabase vdb) throws Exception 92 { 93 super(vdb, RAIDbLevels.SingleDB, ParsingGranularities.NO_PARSING); 95 } 96 97 100 101 110 public ControllerResultSet execReadRequest(SelectRequest request, 111 MetadataCache metadataCache) throws SQLException 112 { 113 if (backend == null) 114 throw new SQLException (Translate.get( 115 "loadbalancer.execute.no.backend.available", request.getId())); 116 117 try 118 { 119 AbstractConnectionManager cm = backend.getConnectionManager(request 120 .getLogin()); 121 if (request.isAutoCommit()) 122 { 123 ControllerResultSet rs = null; 124 boolean badConnection; 125 do 126 { 127 badConnection = false; 128 Connection c = null; 130 try 131 { 132 c = cm.getConnection(); 133 } 134 catch (UnreachableBackendException e1) 135 { 136 String backendName = backend.getName(); 137 logger.error(Translate.get( 138 "loadbalancer.backend.disabling.unreachable", backendName)); 139 disableBackend(backend); 140 backend = null; 141 throw new SQLException (Translate.get( 142 "loadbalancer.backend.unreacheable", backendName)); 143 } 144 145 if (c == null) 147 throw new SQLException (Translate.get( 148 "loadbalancer.backend.no.connection", backend.getName())); 149 150 try 152 { 153 rs = executeSelectRequestOnBackend(request, backend, c, 154 metadataCache); 155 cm.releaseConnection(c); 156 } 157 catch (SQLException e) 158 { 159 cm.releaseConnection(c); 160 throw new SQLException (Translate.get( 161 "loadbalancer.request.failed.on.backend", new String []{ 162 request.getSQLShortForm(vdb.getSQLShortFormLength()), 163 backend.getName(), e.getMessage()})); 164 } 165 catch (BadConnectionException e) 166 { cm.deleteConnection(c); 168 badConnection = true; 169 } 170 } 171 while (badConnection); 172 return rs; 173 } 174 else 175 { 176 long tid = request.getTransactionId(); 177 Connection c = cm.retrieveConnection(tid); 179 180 if (c == null) 182 throw new SQLException (Translate.get( 183 "loadbalancer.unable.retrieve.connection", new String []{ 184 String.valueOf(tid), backend.getName()})); 185 186 ControllerResultSet rs = null; 188 try 189 { 190 rs = executeSelectRequestOnBackend(request, backend, c, metadataCache); 191 } 192 catch (SQLException e) 193 { 194 throw new SQLException (Translate.get( 195 "loadbalancer.request.failed.on.backend", new String []{ 196 request.getSQLShortForm(vdb.getSQLShortFormLength()), 197 backend.getName(), e.getMessage()})); 198 } 199 catch (BadConnectionException e) 200 { cm.deleteConnection(tid); 202 throw new SQLException (Translate.get( 203 "loadbalancer.connection.failed", new String []{ 204 String.valueOf(tid), backend.getName(), e.getMessage()})); 205 } 206 return rs; 207 } 208 } 209 catch (RuntimeException e) 210 { 211 String msg = "Request '" 212 + request.getSQLShortForm(vdb.getSQLShortFormLength()) 213 + "' failed on backend " + backend.getURL() + " (" + e + ")"; 214 logger.fatal(msg, e); 215 throw new SQLException (msg); 216 } 217 } 218 219 226 public int execWriteRequest(AbstractWriteRequest request) throws SQLException 227 { 228 if (backend == null) 229 throw new SQLException (Translate.get( 230 "loadbalancer.execute.no.backend.available", request.getId())); 231 232 try 233 { 234 AbstractConnectionManager cm = backend.getConnectionManager(request 235 .getLogin()); 236 if (request.isAutoCommit()) 237 { 238 if (backend.isDisabling()) 241 throw new SQLException (Translate.get( 242 "loadbalancer.backend.is.disabling", new String []{ 243 request.getSQLShortForm(vdb.getSQLShortFormLength()), 244 backend.getName()})); 245 246 Connection c = null; 248 try 249 { 250 c = cm.getConnection(); 251 } 252 catch (UnreachableBackendException e1) 253 { 254 String backendName = backend.getName(); 255 logger.error(Translate.get( 256 "loadbalancer.backend.disabling.unreachable", backendName)); 257 disableBackend(backend); 258 backend = null; 259 throw new SQLException (Translate.get( 260 "loadbalancer.backend.unreacheable", backendName)); 261 } 262 263 if (c == null) 265 throw new SQLException (Translate.get( 266 "loadbalancer.backend.no.connection", backend.getName())); 267 268 int result; 270 try 271 { 272 result = executeUpdateRequestOnBackend(request, backend, c); 273 } 274 catch (Exception e) 275 { 276 throw new SQLException (Translate.get( 277 "loadbalancer.request.failed.on.backend", new String []{ 278 request.getSQLShortForm(vdb.getSQLShortFormLength()), 279 backend.getName(), e.getMessage()})); 280 } 281 finally 282 { 283 cm.releaseConnection(c); 284 } 285 return result; 286 } 287 else 288 { Connection c = cm.retrieveConnection(request.getTransactionId()); 290 291 if (c == null) 293 throw new SQLException (Translate.get( 294 "loadbalancer.unable.retrieve.connection", 295 new String []{String.valueOf(request.getTransactionId()), 296 backend.getName()})); 297 298 try 300 { 301 return executeUpdateRequestOnBackend(request, backend, c); 302 } 303 catch (Exception e) 304 { 305 throw new SQLException (Translate.get( 306 "loadbalancer.request.failed.on.backend", new String []{ 307 request.getSQLShortForm(vdb.getSQLShortFormLength()), 308 backend.getName(), e.getMessage()})); 309 } 310 } 311 } 312 catch (RuntimeException e) 313 { 314 String msg = Translate.get("loadbalancer.request.failed.on.backend", 315 new String []{request.getSQLShortForm(vdb.getSQLShortFormLength()), 316 backend.getName(), e.getMessage()}); 317 logger.fatal(msg, e); 318 throw new SQLException (msg); 319 } 320 } 321 322 326 public ControllerResultSet execWriteRequestWithKeys( 327 AbstractWriteRequest request, MetadataCache metadataCache) 328 throws SQLException 329 { 330 if (backend == null) 331 throw new SQLException (Translate.get( 332 "loadbalancer.execute.no.backend.available", request.getId())); 333 334 if (!backend.getDriverCompliance().supportGetGeneratedKeys()) 335 throw new SQLException (Translate.get( 336 "loadbalancer.backend.autogeneratedkeys.unsupported", backend 337 .getName())); 338 339 try 340 { 341 AbstractConnectionManager cm = backend.getConnectionManager(request 342 .getLogin()); 343 if (request.isAutoCommit()) 344 { 345 if (backend.isDisabling()) 348 throw new SQLException (Translate.get( 349 "loadbalancer.backend.is.disabling", new String []{ 350 request.getSQLShortForm(vdb.getSQLShortFormLength()), 351 backend.getName()})); 352 353 Connection c = null; 355 try 356 { 357 c = cm.getConnection(); 358 } 359 catch (UnreachableBackendException e1) 360 { 361 String backendName = backend.getName(); 362 logger.error(Translate.get( 363 "loadbalancer.backend.disabling.unreachable", backendName)); 364 disableBackend(backend); 365 backend = null; 366 throw new SQLException (Translate.get( 367 "loadbalancer.backend.unreacheable", backendName)); 368 } 369 370 if (c == null) 372 throw new SQLException (Translate.get( 373 "loadbalancer.backend.no.connection", backend.getName())); 374 375 ControllerResultSet result; 377 try 378 { 379 result = executeUpdateRequestOnBackendWithKeys(request, backend, c, 380 metadataCache); 381 } 382 catch (Exception e) 383 { 384 throw new SQLException (Translate.get( 385 "loadbalancer.request.failed.on.backend", new String []{ 386 request.getSQLShortForm(vdb.getSQLShortFormLength()), 387 backend.getName(), e.getMessage()})); 388 } 389 finally 390 { 391 cm.releaseConnection(c); 392 } 393 return result; 394 } 395 else 396 { 397 Connection c = cm.retrieveConnection(request.getTransactionId()); 399 400 if (c == null) 402 throw new SQLException (Translate.get( 403 "loadbalancer.unable.retrieve.connection", 404 new String []{String.valueOf(request.getTransactionId()), 405 backend.getName()})); 406 407 try 409 { 410 return executeUpdateRequestOnBackendWithKeys(request, backend, c, 411 metadataCache); 412 } 413 catch (Exception e) 414 { 415 throw new SQLException (Translate.get( 416 "loadbalancer.request.failed.on.backend", new String []{ 417 request.getSQLShortForm(vdb.getSQLShortFormLength()), 418 backend.getName(), e.getMessage()})); 419 } 420 finally 421 { 422 backend.removePendingRequest(request); 423 } 424 } 425 } 426 catch (RuntimeException e) 427 { 428 String msg = Translate.get("loadbalancer.request.failed.on.backend", 429 new String []{request.getSQLShortForm(vdb.getSQLShortFormLength()), 430 backend.getName(), e.getMessage()}); 431 logger.fatal(msg, e); 432 throw new SQLException (msg); 433 } 434 } 435 436 440 public ControllerResultSet execReadOnlyReadStoredProcedure( 441 StoredProcedure proc, MetadataCache metadataCache) throws SQLException 442 { 443 return execReadStoredProcedure(proc, metadataCache); 444 } 445 446 450 public ControllerResultSet execReadStoredProcedure(StoredProcedure proc, 451 MetadataCache metadataCache) throws SQLException 452 { 453 if (backend == null) 454 throw new SQLException ( 455 "No available backend to execute stored procedure " + proc.getId()); 456 457 try 458 { 459 AbstractConnectionManager cm = backend.getConnectionManager(proc 460 .getLogin()); 461 if (proc.isAutoCommit()) 462 { Connection c = null; 464 try 465 { 466 c = cm.getConnection(); 467 } 468 catch (UnreachableBackendException e1) 469 { 470 String backendName = backend.getName(); 471 logger.error(Translate.get( 472 "loadbalancer.backend.disabling.unreachable", backendName)); 473 disableBackend(backend); 474 backend = null; 475 throw new SQLException (Translate.get( 476 "loadbalancer.backend.unreacheable", backendName)); 477 } 478 479 if (c == null) 481 throw new SQLException (Translate.get( 482 "loadbalancer.backend.no.connection", backend.getName())); 483 484 ControllerResultSet rs = null; 486 try 487 { 488 rs = AbstractLoadBalancer.executeReadStoredProcedureOnBackend(proc, 489 backend, c, metadataCache); 490 } 491 catch (Exception e) 492 { 493 throw new SQLException (Translate.get( 494 "loadbalancer.storedprocedure.failed.on.backend", new String []{ 495 proc.getSQLShortForm(vdb.getSQLShortFormLength()), 496 backend.getName(), e.getMessage()})); 497 } 498 finally 499 { 500 cm.releaseConnection(c); 501 } 502 return rs; 503 } 504 else 505 { Connection c = cm.retrieveConnection(proc.getTransactionId()); 507 508 if (c == null) 510 throw new SQLException (Translate.get( 511 "loadbalancer.unable.retrieve.connection", new String []{ 512 String.valueOf(proc.getTransactionId()), backend.getName()})); 513 514 try 516 { 517 return AbstractLoadBalancer.executeReadStoredProcedureOnBackend(proc, 518 backend, c, metadataCache); 519 } 520 catch (Exception e) 521 { 522 throw new SQLException (Translate.get( 523 "loadbalancer.storedprocedure.failed.on.backend", new String []{ 524 proc.getSQLShortForm(vdb.getSQLShortFormLength()), 525 backend.getName(), e.getMessage()})); 526 } 527 } 528 } 529 catch (RuntimeException e) 530 { 531 String msg = Translate.get( 532 "loadbalancer.storedprocedure.failed.on.backend", new String []{ 533 proc.getSQLShortForm(vdb.getSQLShortFormLength()), 534 backend.getName(), e.getMessage()}); 535 logger.fatal(msg, e); 536 throw new SQLException (msg); 537 } 538 } 539 540 543 public int execWriteStoredProcedure(StoredProcedure proc) throws SQLException 544 { 545 if (backend == null) 546 throw new SQLException ( 547 "No available backend to execute stored procedure " + proc.getId()); 548 549 try 550 { 551 AbstractConnectionManager cm = backend.getConnectionManager(proc 552 .getLogin()); 553 if (proc.isAutoCommit()) 554 { Connection c = null; 556 try 557 { 558 c = cm.getConnection(); 559 } 560 catch (UnreachableBackendException e1) 561 { 562 String backendName = backend.getName(); 563 logger.error(Translate.get( 564 "loadbalancer.backend.disabling.unreachable", backendName)); 565 disableBackend(backend); 566 backend = null; 567 throw new SQLException (Translate.get( 568 "loadbalancer.backend.unreacheable", backendName)); 569 } 570 571 if (c == null) 573 throw new SQLException (Translate.get( 574 "loadbalancer.backend.no.connection", backend.getName())); 575 576 int result; 578 try 579 { 580 result = AbstractLoadBalancer.executeWriteStoredProcedureOnBackend( 581 proc, backend, c); 582 } 583 catch (Exception e) 584 { 585 throw new SQLException (Translate.get( 586 "loadbalancer.storedprocedure.failed.on.backend", new String []{ 587 proc.getSQLShortForm(vdb.getSQLShortFormLength()), 588 backend.getName(), e.getMessage()})); 589 } 590 finally 591 { 592 cm.releaseConnection(c); 593 } 594 return result; 595 } 596 else 597 { Connection c = cm.retrieveConnection(proc.getTransactionId()); 599 600 if (c == null) 602 throw new SQLException (Translate.get( 603 "loadbalancer.unable.retrieve.connection", new String []{ 604 String.valueOf(proc.getTransactionId()), backend.getName()})); 605 606 try 608 { 609 return AbstractLoadBalancer.executeWriteStoredProcedureOnBackend( 610 proc, backend, c); 611 } 612 catch (Exception e) 613 { 614 throw new SQLException (Translate.get( 615 "loadbalancer.storedprocedure.failed.on.backend", new String []{ 616 proc.getSQLShortForm(vdb.getSQLShortFormLength()), 617 backend.getName(), e.getMessage()})); 618 } 619 } 620 } 621 catch (RuntimeException e) 622 { 623 String msg = Translate.get( 624 "loadbalancer.storedprocedure.failed.on.backend", new String []{ 625 proc.getSQLShortForm(vdb.getSQLShortFormLength()), 626 backend.getName(), e.getMessage()}); 627 logger.fatal(msg, e); 628 throw new SQLException (msg); 629 } 630 } 631 632 635 636 642 public void begin(TransactionMarkerMetaData tm) throws SQLException 643 { 644 if (backend == null) 645 throw new SQLException ("No available backend to begin transaction " 646 + tm.getTransactionId()); 647 648 if (backend.isDisabling()) 650 throw new SQLException (Translate.get("loadbalancer.backend.is.disabling", 651 new String []{"begin transaction " + tm.getTransactionId(), 652 backend.getName()})); 653 654 try 655 { 656 Connection c = backend.getConnectionManager(tm.getLogin()).getConnection( 657 tm.getTransactionId()); 658 659 if (c == null) 660 throw new SQLException (Translate.get( 661 "loadbalancer.backend.no.connection", backend.getName())); 662 663 c.setAutoCommit(false); 664 } 665 catch (Exception e) 666 { 667 throw new SQLException ("Begin of transaction " + tm.getTransactionId() 668 + " failed on backend " + backend.getURL() + " (" + e + ")"); 669 } 670 } 671 672 678 public void commit(TransactionMarkerMetaData tm) throws SQLException 679 { 680 if (backend == null) 681 throw new SQLException ("No available backend to commit transaction " 682 + tm.getTransactionId()); 683 684 try 685 { 686 AbstractConnectionManager cm = backend 687 .getConnectionManager(tm.getLogin()); 688 Connection c = cm.retrieveConnection(tm.getTransactionId()); 689 690 if (c == null) 691 throw new SQLException ("No connection found for transaction " 692 + tm.getTransactionId()); 693 694 try 695 { 696 c.commit(); 697 c.setAutoCommit(true); 698 } 699 catch (SQLException e) 700 { 701 throw new SQLException (Translate.get("loadbalancer.commit.failed", 702 new String []{String.valueOf(tm.getTransactionId()), 703 backend.getName(), e.getMessage()})); 704 } 705 finally 706 { 707 cm.releaseConnection(tm.getTransactionId()); 708 } 709 } 710 catch (RuntimeException e) 711 { 712 String msg = Translate.get("loadbalancer.commit.failed", new String []{ 713 String.valueOf(tm.getTransactionId()), backend.getName(), 714 e.getMessage()}); 715 logger.fatal(msg, e); 716 throw new SQLException (msg); 717 } 718 } 719 720 726 public void rollback(TransactionMarkerMetaData tm) throws SQLException 727 { 728 if (backend == null) 729 throw new SQLException ("No available backend to rollback transaction " 730 + tm.getTransactionId()); 731 732 try 733 { 734 AbstractConnectionManager cm = backend 735 .getConnectionManager(tm.getLogin()); 736 Connection c = cm.retrieveConnection(tm.getTransactionId()); 737 738 if (c == null) 739 throw new SQLException ("No connection found for transaction " 740 + tm.getTransactionId()); 741 742 try 743 { 744 c.rollback(); 745 c.setAutoCommit(true); 746 } 747 catch (SQLException e) 748 { 749 throw new SQLException (Translate.get("loadbalancer.rollback.failed", 750 new String []{String.valueOf(tm.getTransactionId()), 751 backend.getName(), e.getMessage()})); 752 } 753 finally 754 { 755 cm.releaseConnection(tm.getTransactionId()); 756 } 757 } 758 catch (RuntimeException e) 759 { 760 String msg = Translate.get("loadbalancer.rollback.failed", new String []{ 761 String.valueOf(tm.getTransactionId()), backend.getName(), 762 e.getMessage()}); 763 logger.fatal(msg, e); 764 throw new SQLException (msg); 765 } 766 } 767 768 775 public void rollback(TransactionMarkerMetaData tm, String savepointName) 776 throws SQLException 777 { 778 if (backend == null) 779 throw new SQLException ("No available backend to rollback transaction " 780 + tm.getTransactionId()); 781 782 try 783 { 784 AbstractConnectionManager cm = backend 785 .getConnectionManager(tm.getLogin()); 786 Connection c = cm.retrieveConnection(tm.getTransactionId()); 787 788 if (c == null) 789 throw new SQLException ("No connection found for transaction " 790 + tm.getTransactionId()); 791 792 Savepoint savepoint = backend.getSavepoint(new Long ( 793 tm.getTransactionId()), savepointName); 794 795 if (savepoint == null) 796 throw new SQLException ("No savepoint with name " + savepointName 797 + " has been found for transaction " + tm.getTransactionId()); 798 799 try 800 { 801 c.rollback(savepoint); 802 } 803 catch (SQLException e) 804 { 805 throw new SQLException (Translate.get( 806 "loadbalancer.rollbacksavepoint.failed", new String []{ 807 savepointName, String.valueOf(tm.getTransactionId()), 808 backend.getName(), e.getMessage()})); 809 } 810 } 811 catch (RuntimeException e) 812 { 813 String msg = Translate.get("loadbalancer.rollbacksavepoint.failed", 814 new String []{savepointName, String.valueOf(tm.getTransactionId()), 815 backend.getName(), e.getMessage()}); 816 logger.fatal(msg, e); 817 throw new SQLException (msg); 818 } 819 } 820 821 828 public void releaseSavepoint(TransactionMarkerMetaData tm, String name) 829 throws SQLException 830 { 831 if (backend == null) 832 throw new SQLException ("No available backend to release savepoint from " 833 + " transaction " + tm.getTransactionId()); 834 835 try 836 { 837 AbstractConnectionManager cm = backend.getConnectionManager( 838 tm.getLogin()); 839 Connection c = cm.retrieveConnection(tm.getTransactionId()); 840 841 if (c == null) 842 throw new SQLException ("No connection found for transaction " 843 + tm.getTransactionId()); 844 845 Savepoint savepoint = backend.getSavepoint(new Long ( 846 tm.getTransactionId()), name); 847 848 if (savepoint == null) 849 throw new SQLException ("No savepoint with name " + name + " has been " 850 + "found for transaction " + tm.getTransactionId()); 851 852 try 853 { 854 c.releaseSavepoint(savepoint); 855 } 856 catch (SQLException e) 857 { 858 throw new SQLException (Translate.get( 859 "loadbalancer.releasesavepoint.failed", new String []{name, 860 String.valueOf(tm.getTransactionId()), backend.getName(), 861 e.getMessage()})); 862 } 863 finally 864 { 865 backend.removeSavepoint(new Long (tm.getTransactionId()), savepoint); 866 } 867 } 868 catch (RuntimeException e) 869 { 870 String msg = Translate.get("loadbalancer.releasesavepoint.failed", 871 new String []{name, String.valueOf(tm.getTransactionId()), 872 backend.getName(), e.getMessage()}); 873 logger.fatal(msg, e); 874 throw new SQLException (msg); 875 } 876 } 877 878 887 public void setSavepoint(TransactionMarkerMetaData tm, String name) 888 throws AllBackendsFailedException, SQLException 889 { 890 if (backend == null) 891 throw new SQLException ("No available backend to set savepoint to " 892 + " transaction " + tm.getTransactionId()); 893 894 try 895 { 896 AbstractConnectionManager cm = backend 897 .getConnectionManager(tm.getLogin()); 898 Connection c = cm.retrieveConnection(tm.getTransactionId()); 899 900 if (c == null) 901 throw new SQLException ("No connection found for transaction " 902 + tm.getTransactionId()); 903 904 Savepoint savepoint = null; 905 try 906 { 907 savepoint = c.setSavepoint(name); 908 } 909 catch (SQLException e) 910 { 911 throw new SQLException (Translate.get( 912 "loadbalancer.setsavepoint.failed", new String []{ 913 name, String.valueOf(tm.getTransactionId()), backend.getName(), 914 e.getMessage()})); 915 } 916 finally 917 { 918 if (savepoint != null) 919 backend.addSavepoint(new Long (tm.getTransactionId()), savepoint); 920 } 921 } 922 catch (RuntimeException e) 923 { 924 String msg = Translate.get("loadbalancer.setsavepoint.failed", 925 new String []{name, String.valueOf(tm.getTransactionId()), 926 backend.getName(), e.getMessage()}); 927 logger.fatal(msg, e); 928 throw new SQLException (msg); 929 } 930 } 931 932 935 936 944 public void enableBackend(DatabaseBackend db, boolean writeEnabled) 945 throws SQLException 946 { 947 if (backend != null) 948 { 949 if (backend.isReadEnabled()) 950 throw new SQLException ( 951 "SingleDB load balancer accepts only one backend and " 952 + backend.getName() + " is already enabled. Skipping " 953 + db.getName() + " initialization."); 954 } 955 backend = db; 956 logger.info(Translate.get("loadbalancer.backend.enabling", db.getName())); 957 if (!backend.isInitialized()) 958 backend.initializeConnections(); 959 backend.enableRead(); 960 if (writeEnabled) 961 backend.enableWrite(); 962 } 963 964 971 public void disableBackend(DatabaseBackend db) throws SQLException 972 { 973 if (backend.equals(db)) 974 { 975 logger 976 .info(Translate.get("loadbalancer.backend.disabling", db.getName())); 977 backend.disable(); 978 if (backend.isInitialized()) 979 backend.finalizeConnections(); 980 backend = null; 981 } 982 else 983 { 984 String msg = "Trying to disable a non-existing backend " + db.getName(); 985 logger.warn(msg); 986 throw new SQLException (msg); 987 } 988 } 989 990 994 public void setWeight(String name, int w) throws SQLException 995 { 996 throw new SQLException ("Weight is not supported with this load balancer"); 997 } 998 999 1002 public int getNumberOfEnabledBackends() 1003 { 1004 if (backend == null) 1005 return 0; 1006 else 1007 return 1; 1008 } 1009 1010 1013 1014 1019 public String getInformation() 1020 { 1021 if (backend == null) 1022 return "SingleDB Request load balancer: !!!Warning!!! No enabled backend node found\n"; 1023 else 1024 return "SingleDB Request load balancer using " + backend.getURL() + "\n"; 1025 } 1026 1027 1030 public String getXmlImpl() 1031 { 1032 return "<" + DatabasesXmlTags.ELT_SingleDB + "/>"; 1033 } 1034 1035} | Popular Tags |