views:

26

answers:

1

My existing web application gets database pooling parameters from context. Now, I'm developing a new web service to be deployed as a separate application, but using the previous database.

In this new web service application, I am not using any servlet, as well as JSPs. They'll just be a collection of service classes. In such a case, how do I get the context parameters?

In the earlier case when I'm using a servlet, my context is as below:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiResourceLocking="false" privileged="true" >
 <Resource 
    name="jdbc/NetmarketDB"
    auth="Container"
    type="javax.sql.DataSource"
    removeAbandoned="true"
    removeAbandonedTimeout="30"
    maxActive="100"
    maxIdle="30"
    maxWait="1000"
    username="root"
    password="xxxxxxxx"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/netmarket"/>
</Context>

... and I get the context from a servlet or JSP as below:

Connection conn = MySqlDB.getDBConnection(getServletContext());

But now that I'm not using any servlet/JSP, I'm confused. Please help me out.

Thanks in advance.
James.

+1  A: 

Even if you don't have the servlet context as long as you are running inside your web application with the resource definition in your question you should be able to get it using JNDI:

Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/NetmarketDB");
Connection con = ds.getConnection("root", "xxxxxxxx");

And if you are not running within a web application then do this:

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/netmarket", root", "xxxxxxxx");
bmatthews68
Let's say I want to get this datasource only once, and set it somewhere for later use. Earlier, i was setting this datasource as an attribute to ServletContext. What do I do now?
@bmmatthews68: To reiterate it, how do i make use of session without using Servlet or JSP?
@user416101 This is really a new question, but anyway. There's no such thing as a session if there's no webapp. A session exists between a webapp and a web browser. If you just want to keep using a resource, just keep a reference to it. Maybe you should take a look at Spring and its applicationContext concept.
Joeri Hendrickx