In JBoss one can define a datasource with a *-ds.xml.
Is there an equivalent thing or procedure in Tomcat ?
Tomcat has several possibilities for configuring data sources. The main difference is if the data source should be available globally or just for a specific web application.
You can find all you need in the Tomcat documentation. Why didn't you look there right away?
In you configure the data source in context.xml file. Each webapp has one in META-INF folder. A data source will look like this:
<Resource name="jdbc/MyDS" auth="Container"
type="javax.sql.DataSource" username="root" password=""
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb"
maxActive="8"
/>
You can also define a data source in the conf/context.xml file. In that case the data source will be available in all applications. You can reference a data source (e.g. in persistence.xml) like this:
<jta-data-source>java:comp/env/jdbc/JuddiDS</jta-data-source>
Related
I'm looking for solutions to solve the following problem:
I'm using a single instance of a standalone Tomcat 8 container in which I deploy multiple web services as war files which are Spring Boot 1.3.1 based.
I defined 3 data source resources in Tomcat's context.xml:
<Resource name="DBResource1" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="URL"
username="USERNAME"
password="PASSWORD"
.......
/>
So I have 3 of these: DBResource1, DBResource2, DBResource3 each connecting to a different database with different credentials.
And I have 3 war files deployed in Tomcat's webapps directory: service1.war, service2.war, service3.war
Before having these separated data sources I used to have only one tomcat data source resource which was shared by all services and I used to reference it in the single .properties file which contained all the properties for the 3 services like this:
spring.datasource.jndi-name=java:com/env/jdbc/DBResource1
What are the possibilities for associating the jndi resources to the war files like this:
service1.war to use DBResource1
service2.war to use DBResource2
service3.war to use DBResource3
Thanks in advance!
I'm deploying a simple Java 7 (I used Maven for project set-up, dependencies, etc) web app to Tomcat 8 and I have a META-INF/context.xml that I need to specify my database resource:
project/src/main/resources/META-INF/context.xml
<xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxTotal="100" maxIdle="30" maxWaitMillis="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/javatest"/>
</Context>
When I remove this META-INF/context.xml file from the project, I am able to access my jsps but, of course, they return errors since my datasource is missing. However, when I include META-INF/context.xml back to the project, all resources that I try to access give me a 404. Why does it behave this way?
For reference, I am trying how to use a JNDI data source by following this guide. I did all the steps necessary in that project
Are there any exceptions when the server is starting?
If the jdbc driver is not bundled in WEB-INF/lib and not in CATALINA_BASE/lib then it can't find the class. That would most likely result in a startup failure.
Check catalina.out (if you are on unix) or CATALINA_BASE/logs/localhost/catalina.date.log
Edit
Just noticed you have src/main/resources/META-INF
Try src/main/webapp/META-INF/context.xml ...
Do not remove the META-INF/context.xml because it is the default configuration for the project! Also do not enter production-password into the default META-INF/context.xml!
Use copyXML="true" instead! On first deployment to tomcat the META-INF/context.xml is copied permanently into tomcat/conf/Catalina!
Set to true if you want a context XML descriptor embedded inside the application
(located at /META-INF/context.xml) to be copied to xmlBase when the application is
deployed. On subsequent starts, the copied context XML descriptor will be used in
preference to any context XML descriptor embedded inside the application even if the
descriptor embedded inside the application is more recent. The flag's value defaults to
false. Note if deployXML is false, this attribute will have no effect.
After first deployment update tomcat/conf/Catalina/webappname.xml to productive database informations.
Any redeployment will keep using the tomcat/conf/Catalina/webappname.xml.
How to deploy a Java EE project built using eclipse with tomcat to world wide web? This is my first time that I've built a java EE project and now I need to deploy it into the internet, how can I do it? And my project has a large database that needs to be included, and it is made in mysql workbench
First, you need to create a war file, if you use eclipse, you have to go to File > Export > Web > WAR File and follow the steps. After that, you put your WAR file into [Home Tomcat]/WebApss or you can load your WAR file with the web console of Tomcat. Now, I don't know how you connect to your databse, if you have your connection with jdbc in your code, then you don't need to configure something else, but if you have a JNDI for your connection, you need to configure the context in your [Home Tomcat]/conf/server.xml file, for example:
<Context>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest"/>
</Context>
This is the basic configuration for your server and web application, if you need another kind of configuration like security, I recommend you visit the official site of Tomcat.
I hope this information helps you.
Good Luck.
I would like to specify context for db in xml file.
<Context path="/db3" docBase="C:/my/workspace/db3/">
<Resource name="jdbc/ksidb" auth="Container"
type="javax.sql.DataSource"
description="Books"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/ksidb"
username="root"
password="root"
maxActive="20" />
</Context>
I've read that I should copy that file to /webapps tomcat catalogoue. I did this but tomcat7 doesn't read the file. Do you know why? What to do? Thx.
What you read is wrong. I'd question other advice from that source if it told you something so completely false. Per the Tomcat docs, your options for placing the context configuration are as follows:
In an individual file at /META-INF/context.xml inside the application files. Optionally (based on the Host's copyXML attribute) this may be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to application's base file name plus a ".xml" extension.
In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context path and version will be derived from the base name of the file (the file name less the .xml extension). This file will always take precedence over any context.xml file packaged in the web application's META-INF directory.
Inside a Host element in the main conf/server.xml.
I highly recommend you visit the linked docs to learn more about the correct way to configure Tomcat.
Is it possible to use a relative path to an SQLite database file in the context.xml file of a Java web application?
At present, I have:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mywebapp">
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="myusername" password="mypassword" driverClassName="org.sqlite.JDBC"
url="jdbc:sqlite://Users/me/NetBeansProjects/MyApp/web/my.db"/>
</Context>
As you can see, the url attribute of the Resource tag currently specifies an absolute path to my.db. However, I am working on this project with a small team of developers as a class project. We are sharing the code through Subversion and currently, every time one of us updates our project, we have to manually replace the above path with the correct path for our computer.
Thanks in advance.
UPDATE: It appears that relative paths are not relative to the context.xml file, but to CATALINA_HOME. Unfortunately that directory (where Tomcat is located) is in another user's home folder (1) to which I have ony read access:
(1) /home/myprof/Tomcat/bin/
(2) /home/me/NetBeansProjects/MyApp/web/my.db
I cannot include /me in the relative path because, as stated above, this project will run on multiple computers via a svn repository and the directory in which the project files are contained (2) will change. However, since all of the accounts are on a shared virtual network, CATALINA_HOME (1) will not change.
Also, how can I get the path to the database file my.db from the context.xml file above inside a servlet?
I have found following examples of using JDBC with SQLite:
For memory access:
jdbc:sqlite::memory:
For relative paths:
jdbc:sqlite:relative/path.db
For absolute paths:
jdbc:sqlite:/absolute/path.db
As a different approach: did you try to use jdbc:sqlite:~/myapp.db ? This should be a writable location on the computers of everyone running that application. (Although I've seen the evil that are read-only home directories.)
I'm not exactly sure if SQLite supports ~ in the database URL. I know H2 does, and I can nothing but recommend it as an embedded Java DB. If you don't want to change databases, you might want to see if Tomcat allows some parameter substitution in its configuration files, like ${user.home}. (Some preliminary Googling gives me mailing list results that seem to imply that's be case in at least server.xml, so it could be worth just trying it.)
I'm still not exactly sure what you're trying to achieve though: are you the one running the Tomcat instance, or is it a shared tomcat instance running under some system account?
You could also try to set up another world-writable location on the network to put the database in. Somewhere under /var/ maybe, or just a folder that's set to ug=rwx in one person's home directory.
I am still looking for a way to use a relative path to the database in the context.xml file.
However, I figured out how to get extract the path to the database from the context.xml file. It's not pretty, but it works. I couldn't figure out how to parse the context.xml file as XML so this ugly hack looks for a line beginning with "url=".
URL url = getServletContext().getResource("/META-INF/context.xml");
Properties props = new Properties();
props.load(url.openStream());
String strval = props.getProperty("url");
Pattern pattern = Pattern.compile("\\\W*\"(.\*)\"\\\W*");
Matcher matcher = pattern.matcher(strval);
matcher.find();
String constr = matcher.group(1);
if (constr != null)
this.jdbcConnectionString = constr;
I had the same issue with Spring and jdbc.properties I fix that by removing // and write the relative path
Try this
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mywebapp">
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="myusername" password="mypassword" driverClassName="org.sqlite.JDBC"
url="jdbc:sqlite:web/my.db"/>
</Context>
I found the answer to my question here. The solution is to get the resource rather than try to parse the database URL out of the context.xml file.
This answer is already given by honzas. I am reiterating it to make it more clear (b/c I also didn't understand it at first).
jdbc:sqlite:relative/path.db
That is correct for a relative path. Notice the lack of a / before relative. That makes the path absolute. You can also use this:
jdbc:sqlite:./relative/path.db
The . refers to the current folder.