Link to home
Start Free TrialLog in
Avatar of richardsimnett
richardsimnett

asked on

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
Avatar of for_yan
for_yan
Flag of United States of America image

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.
It looks like it is PC oriented with ODBC control panel stuff

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
So you have both Tomcat and Plesk running on windows?
Avatar of richardsimnett
richardsimnett

ASKER

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



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

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’;

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


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 :((
What version of tomcat?
are you using your own context file or defining it in server.xml?

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
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
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
by default you use dbcp with tomcat for connection pooling
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
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

Shouldn't you aslo have this line:
after creating initial context:
Context envContext  = (Context)initContext.lookup("java:/comp/env");
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

Got it! Thanks to everyone for the help with this issue!