Reputation: 4394
I'm trying to configure the connection pool 2 web applications using .xml context descriptor for 2 separate context paths and databases, but with the same logical code. The problem is that when using JNDI to lookup the datasource, the resources are overridden when jetty loads the context, and the apps end up using the same database. Here is my configuration and code:
Configuration for MyApp1.xml:
<New id="myDataBase" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/myDataBase</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://host:3306/mydb</Set>
<Set name="User">...</Set>
<Set name="Password">...</Set>
</New>
</Arg>
</New>
Configuration for MyApp2.xml
<New id="myDataBase" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/myDataBase</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://host:3306/mydb-test2</Set>
<Set name="User">...</Set>
<Set name="Password">...</Set>
</New>
</Arg>
</New>
Code used by the apps:
public class MyDb {
...
static {
try {
datasource = (DataSource) new InitialContext().lookup("jdbc/myDataBase");
} catch (NamingException e) {
// ...
}
...
}
So, jetty loads context for MyApp1.xml, then MyApp2.xml. When I try to access the apps, they both use the same database: mydb-test2.
How can I properly resolve this problem?
EDIT:
I have tried adding the argument in the configuration but it is not working; it is strange that now the contexts both use the first configured database: mydb
This is the xml for each app:
<Configure id='wac1' class="org.eclipse.jetty.webapp.WebAppContext">
...
<New id="myDataBase" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid='wac1'/></Arg>
<Arg>jdbc/myDataBase</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://host:3306/mydb</Set>
<Set name="User">...</Set>
<Set name="Password">...</Set>
</New>
</Arg>
</New>
...
<Configure id='wac2' class="org.eclipse.jetty.webapp.WebAppContext">
...
<New id="myDataBase" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid='wac2'/></Arg>
<Arg>jdbc/myDataBase</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://host:3306/mydb-test2</Set>
<Set name="User">...</Set>
<Set name="Password">...</Set>
</New>
</Arg>
</New>
...
What am I missing? Do I have to change the code (way in which I make the lookup)?
I've tried
datasource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/myDataBase");
but I get a javax.naming.NameNotFoundException - probably because I dont use env variables, but I don't think I need that, just per context scope lookup)
EDIT 2:
Ok, so I was actually missing a web.xml config which solved the issue (see http://www.eclipse.org/jetty/documentation/current/jndi-configuration.html#configuring-datasources)
<resource-ref>
<res-ref-name>jdbc/HugaDataBase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Upvotes: 0
Views: 1009