Open (if necessary) and return a database connection for use by this Store. @return database connection ready to use @exception SQLException if a database error occurs
()
| 557 | * @exception SQLException if a database error occurs |
| 558 | */ |
| 559 | protected Connection open() throws SQLException { |
| 560 | if (dataSourceName != null && dataSource == null) { |
| 561 | org.apache.catalina.Context context = getManager().getContext(); |
| 562 | if (getLocalDataSource()) { |
| 563 | ClassLoader oldThreadContextCL = context.bind(null); |
| 564 | try { |
| 565 | Context envCtx = (Context) (new InitialContext()).lookup("java:comp/env"); |
| 566 | this.dataSource = (DataSource) envCtx.lookup(this.dataSourceName); |
| 567 | } catch (NamingException e) { |
| 568 | context.getLogger().error(sm.getString("dataSourceStore.wrongDataSource", this.dataSourceName), e); |
| 569 | } finally { |
| 570 | context.unbind(oldThreadContextCL); |
| 571 | } |
| 572 | } else { |
| 573 | try { |
| 574 | // This should be the normal way to lookup for the global in the global context (no comp/env) |
| 575 | Service service = Container.getService(context); |
| 576 | if (service != null) { |
| 577 | Server server = service.getServer(); |
| 578 | if (server != null) { |
| 579 | Context namingContext = server.getGlobalNamingContext(); |
| 580 | this.dataSource = (DataSource) namingContext.lookup(dataSourceName); |
| 581 | } |
| 582 | } |
| 583 | } catch (NamingException e) { |
| 584 | // Ignore, try another way for compatibility |
| 585 | } |
| 586 | if (this.dataSource == null) { |
| 587 | try { |
| 588 | Context envCtx = (Context) (new InitialContext()).lookup("java:comp/env"); |
| 589 | this.dataSource = (DataSource) envCtx.lookup(this.dataSourceName); |
| 590 | } catch (NamingException e) { |
| 591 | context.getLogger().error(sm.getString("dataSourceStore.wrongDataSource", this.dataSourceName), |
| 592 | e); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | } |
| 598 | |
| 599 | if (dataSource != null) { |
| 600 | return dataSource.getConnection(); |
| 601 | } else { |
| 602 | throw new IllegalStateException(sm.getString("dataSourceStore.missingDataSource")); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Close the specified database connection. |
no test coverage detected