I have a webservice with Java Servlets on a Tomcat server. In my Servlet I use a database pool like this:
envContext = new InitialContext();
DataSource ds = (DataSource) envContext.lookup( "java:/comp/env/jdbc/Database" );
con = ds.getConnection();
For initialisation I have this in my web.xml:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/Database</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Then there is the context.xml which seems to be the important step here:
<?xml version="1.0" encoding="UTF-8"?>
<!-- The contents of this file will be loaded for each web application -->
<Context crossContext="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/Database" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="user"
password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/configerror_db"/>
</Context>
I read many other questions conserning this error, but I couldn't solve it.
First I want to explain, that I don't get this error when using my linux-machine. Hence I installed the same code in Eclipse on my Windows machine I get this context-environment error.
Other Answers say to do this.
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"<initialContextFactory>");
env.put(Context.PROVIDER_URL, "<url>");
env.put(Context.SECURITY_PRINCIPAL, "<user>");
env.put(Context.SECURITY_CREDENTIALS, "<password>");
ctx = new InitialContext(env);
But I don't know what the initialContextFactory is AND shouldn't the context.xml do exactly this? As I said on linux it works.
Can someone help me out here? What am I missing? I don't want to write the user and password in every file where I use a database connection. I thought hell yeah this is awesome. The credentials are just in the context.xml, but now on windows it is not working.
Thanks for any advice.