0

I have static context defined in server.xml

<Context path="" reloadable="true" docBase="my-module"/>

I need to specify context for this application. I tried to add my-module.xml and also ROOT.xml to conf/Catalina/localhost but that didn't solved my issue.

I'm still getting error

Unexpected exception: Name [jdbc/my-module] is not bound in this Context. Unable to find [jdbc]

my context.xml

<Context>    
<Resource 
        name="jdbc/my-module" 
        auth="Container" 
        type="javax.sql.DataSource" 
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000" 
        username="AFE" 
        password="AFE" 
        driverClassName="oracle.jdbc.driver.OracleDriver" 
        url="jdbc:oracle:thin:@my-server:1537:DB" 
        defaultAutoCommit="false" />
</Context>

How can I then specify context for root application?

bilak
  • 4,526
  • 3
  • 35
  • 75
  • This post has the correct configuration http://stackoverflow.com/questions/7276989/howto-set-the-context-path-of-a-web-application-in-tomcat-7-0 – John Kuhns Sep 08 '15 at 14:37
  • it should be `java:/comp/env/jdbc/my-module` –  Sep 08 '15 at 15:36
  • @JohnKuhns that looks promising however not working for me. I have static context in server.xml `````` and also that same in ROOT.xml in conf/Catalina/localhost but have still same problem :( – bilak Sep 10 '15 at 09:00
  • @JohnKuhns OK I resolved that. Link you posted is ok, when you don't specify Context in server.xml. Thank you – bilak Sep 10 '15 at 09:24

1 Answers1

0

Actually you mustn't manually edit server.xml. After serever pool execution it must add it automatically. Context.xml must be in META-INF folder.Server tomcat.

<Context>
<Resource name="jdbc/DATABASE"
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000"
        username="test" password="test" 
        driverClassName="org.apache.derby.jdbc.ClientDriver"
        defaultAutoCommit="false"
        defaultTransactionIsolation="READ_COMMITTED"
        connectionProperties="useUnicode=yes;characterEncoding=utf8;"
        url="jdbc:derby://localhost/DataBase"/>
</Context>

and here connection code

private static DBConnector instance;

public static synchronized DBConnector getInstance() {
    if (instance == null) {
        instance = new DBConnector();
    }
    return instance;
}

private DBConnector(){
    try {
        Context initContext = new InitialContext();
        Context envContext = (Context) initContext.lookup("java:/comp/env");
        // DATABASE - the name of data source
        ds = (DataSource) envContext.lookup("jdbc/DATABASE");
    } catch (NamingException ex) {
        System.out.println(ex);
    }
}

private DataSource ds;
KofHein
  • 59
  • 10
  • I know about how this works for any application. But I'm asking about default application. Lets say that I have myapp.war which is statically defined in server.xml as root . But when I create myapp.xml context config, jdbc is not picked up. – bilak Sep 09 '15 at 13:09
  • Try to follow my steps 1)in eclipse create war file. 2) put it into your tomcat directory. 3) run tomcat server. 4) shutdown it and in webapps folder rename your project name to ROOT. 5) in tomcat/conf/context.xml add resource as mentioned above.6) start tomcat ... Must work – KofHein Sep 09 '15 at 20:26
  • Yes this way it will work. But I don't want to have global resource as you wrote (tomcat/conf/context.xml). I just want to have appliation resources conf/Catalina/localhost/appname.xml and this will be my default webapp. – bilak Sep 10 '15 at 08:38
  • there exists folder with name work and it contains your app name folder, you must put context there, but it don't contain ROOT. – KofHein Sep 10 '15 at 13:28