MCPcopy Create free account
hub / github.com/apache/tomcat / open

Method open

java/org/apache/catalina/session/DataSourceStore.java:559–604  ·  view source on GitHub ↗

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

()

Source from the content-addressed store, hash-verified

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.

Callers 1

getConnectionMethod · 0.95

Calls 13

getLocalDataSourceMethod · 0.95
getServiceMethod · 0.95
getServerMethod · 0.95
getContextMethod · 0.65
getManagerMethod · 0.65
bindMethod · 0.65
lookupMethod · 0.65
errorMethod · 0.65
getLoggerMethod · 0.65
getStringMethod · 0.65
unbindMethod · 0.65

Tested by

no test coverage detected