• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1401
  • Last Modified:

JNDI Datasources from Glassfish to Tomcat (plesk)

Hello,
I have a JSP application which was developed in netbeans for glassfish. It works 100% and has no problems. Recently, I was told that we were scrapping glassfish for tomcat, and to migrate our glassfish app over. Simple enough I thought.

So far nothing works. I have changed the project type to tomcat under netbeans, it  compiles and uploads to tomcat just fine, however, NONE OF THE DATASOURCES WORK.

I did some more reading on tomcat, and realized that the datasources have to be configured using server.xml. I made the changes, and saved. The app still doesnt work.

Upon investigating on google I found otu that since we are running tomcat on plesk, the datasources cannot be stored in server.xml, but need to be put somewhere else. It didnt however tell me where else to put it.

I am sick of looking, and figured it would be easier to just ask. Im sure someone out there has already gone through this headache in one form or another.

So my question is: "Can someone out there give me an example of how to create JNDI datasources under Tomcat in plesk?"

As an example I refer to the datasources in my servlets like this:

context = new InitialContext();
ds = (DataSource) context.lookup("jdbc/tracking_pool");

The database is MySQL 5.

A working example is worth 500 points.

Thanks,
Rick
0
richardsimnett
Asked:
richardsimnett
  • 13
  • 6
  • 4
  • +1
5 Solutions
 
for_yanCommented:
Perhaps this will help
(from http://www.esds.co.in/forum/f4/how-create-dsn-using-plesk-control-panel-1946/)


Hello,

You can follow the below mentioned procedure to create DSN in Plesk Control panel,

# Login to Plesk control panel.
Go to the domain for which you want to add your DSN.
# Put Database .mdb file on the server
Upload your database to your web site via FTP.
IE: C:\Inetpub\vhosts\domainname\private
# Add ODBC DSN
Choose ODBC Data Sources icon under Services.
Choose Add New ODBC DSN icon.
Fill in the connection name and connection description fields.
IE: Connection Name: domain name
# Choose what type of driver you'd like to use.
IE: Microsoft Access Driver (*.mdb)
[DBQ]Database File Path: find the actual .mdb file you've uploaded.
Click on OK.
# Fill out the preferences form for the DSN
[DBQ] * Database File Path
IE: C:\Inetpub\vhosts\domainname\private\*.mdb
Note: Full path is required.
[UID]Login ID:
[PWD] Password:
Note: User id and password are only required for MSAccess if you have the file protected with a login/password.
All remaining fields can stay as they are.
Click on Test, you should see the below message
Info: Connection successful
Note: if you do not, Correct your settings and try again.
# Click Next and Plesk will create the DSN for you.
# Click finish
You should see your file listed under ODBC DSN Connections.
0
 
for_yanCommented:
It looks like it is PC oriented with ODBC control panel stuff
0
 
for_yanCommented:

This manual has a full chapter about external databases.
Do you have this version of Plesk?

http://www.hardhathosting.com/guides/plesk-9.0-domain-administrators-guide.pdf
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
for_yanCommented:
So you have both Tomcat and Plesk running on windows?
0
 
richardsimnettAuthor Commented:
for_yan,

I guess I should've been more clear. I am not running windows at all. It is plesk 10, running on Ubuntu Linux. Everything is installed and working properly. I just cannot for the life of me get the datasources to work in tomcat.

JNDI datasources are not DSN. They are a way of calling a resource (in this case a database, but could be anything) in Java, but the ideas are similiar.

Thanks,
Rick


0
 
for_yanCommented:

Look at that, maybe it will help.
This guy uses struts, but otherwise he looks to be close to you and he solved his problem:

http://forum.parallels.com/showthread.php?t=58495
0
 
for_yanCommented:

Also llook at that:

http://pearlin.info/plesk-tips-and-fixes/

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

When I try to configure connection to client’s MySQL server in Plesk I get an error:
Failed to connect to database: Access denied for user ‘admin’@'localhost’.
===============================================================================================

RESOLUTION

This problem occurs when Plesk connection to MySQL server, which is used for clients’ databases storing, has gotten misconfigured. To configure it back you need perform the following steps:

1. Edit MySQL configuration file my.ini located within %plesk_dir%databases\mysql\data.
2. Add parameter skip-grant-tables into the [MySQLD] section.
3. Restart SQL Server.
4. Log into MySQL.

%plesk_dir%\mysql\bin\mysql.exe mysql
5. Execute the following query:

mysql> update user set password=password(‘current_password’) where user=’admin’;
6. Remove skip-grant-tables from within the [MySQLD] section within

%plesk_dir%\databases\mysql\data\my.ini
7. Restart SQL Server.

Where `current_password` is the password for admin in client’s MySQL which is stored in Plesk. It is used by Plesk to control client’s MySQL server. If you do know what is the current password you can fetch it from Plesk database using this query:

%plesk_dir%\mysql\bin\mysql -uadmin -p`pleskadmin_password` -P8306 psa
mysql> select val from misc where param=’mysql_admin_passwd’;

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0
 
rrzCommented:
I am not an expert at this. But maybe I can help you.  
What version of Tomcat are you using ?  
Have you seen these pages ?
http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html 
http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html     

>I did some more reading on tomcat, and realized that the datasources have to be configured using server.xml.
That is not  true in the recent versions of Tomcat.  Look at  
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html   
In the recent versions you can create a  context.xml file to hold your <context> element.  
0
 
for_yanCommented:


Well, this was in 2007, but still looks not very enciouraging:

http://www.junlu.com/msg/393560.html


-none-
2007-10-03       - By Charl Gerber

 Back
Turns out datasource configuration is not possible
using Plesk as a shared tomcat server, the guys at
Plesk themselves told me.

That sucks. Means I have to create and manage the
Datasource in my app... minor refactoring, but not a
nice way of doing it. I also use hibernate and could
only get that working by defining its own connection
pooling. So now I have two pools :((
0
 
objectsCommented:
What version of tomcat?
are you using your own context file or defining it in server.xml?

0
 
for_yanCommented:
If you go without Plesk this is the information for configuring jdbc-pool
in tomcat with mySQL, either global for for particular applications
with lots of details:
http://www.tomcatexpert.com/blog/2010/04/01/configuring-jdbc-pool-high-concurrency
0
 
richardsimnettAuthor Commented:
Hello All,
It is Tomcat 6. I initially tried to do it in server.xml with no luck. I didnt realize that there was a context.xml file that could be included in the web-inf.

I am currently reading through the posts that rrz put up regarding the context.xml. I think this is right on the money, however, from the examples I still havent quite pieced together how to setup the JNDI interface to JDBC. I get the XML portion, but I still havent come across where the actual datasource configuration occurs. Where do the database ip, username, password, database name, port, etc go?

How do I define the connection as a pool vs a single connection? How do I set the size of the pool?

Can someone show me an example I can duplicate and test with?

Thanks,
Rick
0
 
richardsimnettAuthor Commented:
Objects,
I think that it must go in context.xml, but I still havent worked out what the actual configuration exactly is or how it works.

Thanks,
Rick
0
 
for_yanCommented:
0
 
objectsCommented:
context.xml is the best way to handle it
connect details are defined in the resource eg.


<Context ...>
  ...
  <Resource name="jdbc/EmployeeDB"
            auth="Container"
            type="javax.sql.DataSource"
            username="dbusername"
            password="dbpassword"
            driverClassName="org.hsql.jdbcDriver"
            url="jdbc:HypersonicSQL:database"
            maxActive="8"
            maxIdle="4"/>
  ...
</Context>

will grab you an example
0
 
for_yanCommented:
Context initContext = new
 InitialContext();
   Context envContext  = (Context)initContext.lookup("java:/comp/env");
   DataSource datasource = (DataSource)envContext.lookup("jdbc/LocalTestDB");
   Connection con = datasource.getConnection();

Open in new window

0
 
for_yanCommented:
Sizing the connection pool
We will work with the following attributes to size the connection pool

initialSize
maxActive
maxIdle
minIdle
It's important to understand these attributes, as they do seem quite obvious but there are some secrets. Let's nail it down.

<Resource type="javax.sql.DataSource"
            name="jdbc/TestDB"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/mysql"
            username="mysql_user"
            password="mypassword123"
            initialSize="10"
            maxActive="100"
            maxIdle="50"
            minIdle="10"
            />
0
 
objectsCommented:
by default you use dbcp with tomcat for connection pooling
0
 
rrzCommented:
Wow! You are getting a lot of information.
I think it really comes down to making a  
/META-INF/context.xml
file in your  web app. It will contain your <context> element that contains a <Resource> element.
and adding a
<resource-ref>
element to your web app's web.xml file.  
This is covered in the links I posted.
0
 
richardsimnettAuthor Commented:
Hey everyone,
Just an update. This questio is not abandoned... I am simple still working through this issue. Hoping to have this all working today.

Thanks,
Rick
0
 
richardsimnettAuthor Commented:
Hello All,
Ok. So I am having a bit of trouble after reading through all the posts and links...

I keep getting this error (everywhere I try to access a datasource):

 Name jdbc is not bound in this Context

I think I have everything set up right. I have attached my context.xml, web.xml, and a snippet of the code generating the error.

Thanks,
Rick
Code throwing the errors:

/* Get the connection */
context = new InitialContext();
ds = (DataSource) context.lookup("jdbc/tracking_pool");
con = ds.getConnection();
stmt = con.createStatement();

/* Query and Get Result */
tmp = stmt.executeQuery(query.toString());

Open in new window

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="">

<Resource type="javax.sql.DataSource"
            name="jdbc/coop"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            driverClassName="com.mysql.jdbc.Driver"
            url="cough"
            username="cough"
            password="cough"
            initialSize="8"
            maxActive="32"
            maxIdle="32"
            minIdle="8"
            />
<Resource type="javax.sql.DataSource"
            name="jdbc/tracking_pool"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            driverClassName="com.mysql.jdbc.Driver"
            url="cough"
            username="cough"
            password="cough"
            initialSize="8"
            maxActive="32"
            maxIdle="32"
            minIdle="8"
            />
<Resource type="javax.sql.DataSource"
            name="jdbc/coop"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            driverClassName="com.mysql.jdbc.Driver"
            url="cough"
            username="cough"
            password="cough"
            initialSize="8"
            maxActive="32"
            maxIdle="32"
            minIdle="8"
            />
<Resource type="javax.sql.DataSource"
            name="jdbc/mpn"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            driverClassName="com.mysql.jdbc.Driver"
            url="cough"
            username="cough"
            password="cough"
            initialSize="8"
            maxActive="32"
            maxIdle="32"
            minIdle="8"
            />
</Context>

Open in new window

web.xml (resource refs)

<resource-ref>
        <res-ref-name>jdbc/coop</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <resource-ref>
        <res-ref-name>jdbc/tracking_pool</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <resource-ref>
        <res-ref-name>jdbc/data_pool</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <resource-ref>
        <res-ref-name>jdbc/mpn</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>

Open in new window

0
 
for_yanCommented:
Shouldn't you aslo have this line:
after creating initial context:
Context envContext  = (Context)initContext.lookup("java:/comp/env");
0
 
for_yanCommented:
I'm coparing your code with the example
and error message seems to indicate that you need
to do smth with context before gettting datasour out of it:

Context initContext = new
 InitialContext();
   Context envContext  = (Context)initContext.lookup("java:/comp/env");
   DataSource datasource = (DataSource)envContext.lookup("jdbc/LocalTestDB");
   Connection con = datasource.getConnection();

Open in new window

0
 
richardsimnettAuthor Commented:
Got it! Thanks to everyone for the help with this issue!

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 13
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now