You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Søren Blidorf <so...@nolas.dk> on 2007/06/22 10:44:13 UTC

Realm works; db connection does not?

Hi.

My realm is working and is able to login to my app. When I try to do anything in the application that need connection to db, I get a white screen.

Can anybody tell me the differance between the realm connection to the db and my applications connection to the db.

I use: com.microsoft.sqlserver.jdbc.SQLServerDriver and jdbc:sqlserver

Any idears is appriciated

Soren

Re: Realm works; db connection does not?

Posted by Johnny Kewl <jo...@kewlstuff.co.za>.
----- Original Message ----- 
From: "Søren Blidorf" <so...@nolas.dk>
To: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Friday, June 22, 2007 1:32 PM
Subject: Re: Realm works; db connection does not?


> Hi again.
>
> I do mean realm and the reason why I mention it is that I can validate 
> users
> via my users and user_roles in my db, so the connection should work I 
> guess?
>
>
>        <Context path="/xx" docBase="webapp" reloadable="true">
>        <Manager pathname="xx" />
>         <Realm  name="UMRealm"
> className="org.apache.catalina.realm.JDBCRealm"
>         debug="99"
> driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
>
> connectionURL="jdbc:sqlserver://127.0.0.1;DatabaseName=xx;ProgramName=xx;Sel
> ectMethod=cursor"
>         connectionName="xx" connectionPassword="xx" userTable="users"
> userNameCol="username" userCredCol="password" userRoleTable="user_roles"
> roleNameCol="rolename" />
>        </Context>
>

Ok... see you using a simple connection...... think its just missing this

  Class.forName("THE JDBC DRIVER");


> Get the connection with: DriverManager.getConnection(    connectionUrl,
> connectionUser,    connectionPass)
>
> I also just upgrade my struts could it be a taglib thing?
>
> In my JSP:
>
> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
>
> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
>
> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
>
> <%@ taglib uri="http://struts.apache.org/tags-nested" prefix="logic" %>
>
> In my web.xml:
>
>  <taglib>
>    <taglib-uri>http://struts.apache.org/tags-bean</taglib-uri>
>    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
>  </taglib>
>  <taglib>
>    <taglib-uri>http://struts.apache.org/tags-html</taglib-uri>
>    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
>  </taglib>
>  <taglib>
>    <taglib-uri>http://struts.apache.org/tags-logic</taglib-uri>
>    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
>  </taglib>
>  <taglib>
>    <taglib-uri>http://struts.apache.org/tags-nested</taglib-uri>
>    <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
>  </taglib>
>
> ----- Original Message -----
> From: "Johnny Kewl" <jo...@kewlstuff.co.za>
> To: "Tomcat Users List" <us...@tomcat.apache.org>
> Sent: Friday, June 22, 2007 1:00 PM
> Subject: Re: Realm works; db connection does not?
>
>
>>
>> Hi Søren,
>>
>> Not sure what you saying but arnt you confusing Realm and JNDI.
>> ie the Realm is for your user and password dB
>> and JNDI/DBCP is for you to do other stuff with your dB....
>>
>> Think you almost there just check out
>> http://localhost:8080/tomcat-docs/jndi-resources-howto.html
>> on your system
>>
>> If this is not the problem, please post your config and code you 
>> using....
>>
>> ----- Original Message -----
>> From: "Søren Blidorf" <so...@nolas.dk>
>> To: <us...@tomcat.apache.org>
>> Sent: Friday, June 22, 2007 10:44 AM
>> Subject: Realm works; db connection does not?
>>
>>
>> Hi.
>>
>> My realm is working and is able to login to my app. When I try to do
>> anything in the application that need connection to db, I get a white
>> screen.
>>
>> Can anybody tell me the differance between the realm connection to the db
>> and my applications connection to the db.
>>
>> I use: com.microsoft.sqlserver.jdbc.SQLServerDriver and jdbc:sqlserver
>>
>> Any idears is appriciated
>>
>> Soren
>>
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
> 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Realm works; db connection does not?

Posted by Søren Blidorf <so...@nolas.dk>.
Thanks. I will look at that.

This is my connection:

package um.persistence;

import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.http.HttpServlet;
import java.sql.DriverManager;

public class PersistenceFacade {

  //----------- fields ---------//

 public static final String CONNECTION_DRIVER =
"com.microsoft.sqlserver.jdbc.SQLServerDriver";
 public static final String CONNECTION_USER   = "username";
 public static final String CONNECTION_PASS   = "password";
 public static final String CONNECTION_URL    = "url";

 static {
    try {
      Class.forName(CONNECTION_DRIVER);
    } catch (ClassNotFoundException ex) {
   ex.printStackTrace();
    }
 }

 private String connectionUrl  = null;
 private String connectionUser = null;
 private String connectionPass = null;



 //------------ constructors -----------//

 //public PersistenceFacade() {}

 public PersistenceFacade(HttpServlet servlet) {
  connectionUser = servlet.getInitParameter(CONNECTION_USER);
  connectionPass = servlet.getInitParameter(CONNECTION_PASS);
  connectionUrl  = servlet.getInitParameter(CONNECTION_URL);
 }

 //------------- public methods -----------//

 public Connection getConnection() throws SQLException {

        System.err.println("getConnection startet!");

  if(connectionUser == null) {
   throw new SQLException("Missing parameter "+CONNECTION_USER);
  } else if(connectionPass == null) {
   throw new SQLException("Missing parameter "+CONNECTION_PASS);
  } else if(connectionUrl == null) {
   throw new SQLException("Missing parameter "+CONNECTION_URL);
  }
  return DriverManager.getConnection(
    connectionUrl,
    connectionUser,
    connectionPass
  );
 }

 public void closeConnection(Connection conn) {
    try {
      if(!conn.isClosed()) {
        conn.close();
      }
    } catch (SQLException ex) {
   ex.printStackTrace();
    }
 }

 //------------ private methods ----------//
}


And I call the PersistenceFacade like this:

PersistenceFacade persistenceFacade = new PersistenceFacade(getServlet());

conn = persistenceFacade.getConnection();

I get no errors only a white screen!!!!!



----- Original Message -----
From: "Christopher Schultz" <ch...@christopherschultz.net>
To: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Friday, June 22, 2007 2:26 PM
Subject: Re: Realm works; db connection does not?


> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Søren,
>
> Søren Blidorf wrote:
> > I do mean realm and the reason why I mention it is that I can validate
users
> > via my users and user_roles in my db, so the connection should work I
guess?
>
> That depends upon your connection settings.
>
> >         <Context path="/xx" docBase="webapp" reloadable="true">
> >          <Realm  name="UMRealm"
>
> This looks good... and it should, since it apparently works.
>
> > Get the connection with: DriverManager.getConnection(    connectionUrl,
> > connectionUser,    connectionPass)
>
> This is just about the simplest way to get a connection. Are you using
> the same URL, username, and password? I don't see why this wouldn't
> work. On the other hand, I'd never do it this way: I'd use a connection
> pool (see below).
>
> > I also just upgrade my struts could it be a taglib thing?
>
> This should have nothing to do with either Struts or your taglibs
> (unless you use taglibs for SQL queries... <shiver>).
>
> My recommendation is to use a JNDI datasource and then use that for both
> your "in-app" database connections as well as the <Realm>. Basically,
> you change your <Context> element to include:
>
> <Resource [see documentation for JNDI DataSource] />
> <Realm [see documentation for DataSourceRealm;
>         don't forget to set localDataSource="true"] />
>
> Then, in your code, you'll need a slightly more complicated way of
> getting a database connection. But, you already have your "get
> connection" logic in a method used everywhere, right?
>
> import javax.naming.Context;
> import javax.naming.InitialContext;
> import javax.naming.NamingException;
> import javax.swl.DataSource;
>
> public Connection getConnection()
>     throws SQLException, NamingException
> {
>     Context ctx = new InitialContext();
>
>     DataSource ds = (DataSource)ctx.lookup(DATASOURCE_PATH);
>
>     if(null == ds)
> throw new NamingException("Cannot obtain DataSource");
>
>     return ds.getConnection();
> }
>
> This way, both the Realm and your application are using the same pool of
> connections. This avoids weird situations where your realm works but not
> your application (and vice versa).
>
> Hope that helps,
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.7 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFGe8AQ9CaO5/Lv0PARAmz5AJoCusXnwWzOobA8UaJxaLp0iJfdMwCguf9S
> OpmaxB78ll5KigAJdUB4At4=
> =Q5C4
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Realm works; db connection does not?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Søren,

Søren Blidorf wrote:
> I do mean realm and the reason why I mention it is that I can validate users
> via my users and user_roles in my db, so the connection should work I guess?

That depends upon your connection settings.

>         <Context path="/xx" docBase="webapp" reloadable="true">
>          <Realm  name="UMRealm"

This looks good... and it should, since it apparently works.

> Get the connection with: DriverManager.getConnection(    connectionUrl,
> connectionUser,    connectionPass)

This is just about the simplest way to get a connection. Are you using
the same URL, username, and password? I don't see why this wouldn't
work. On the other hand, I'd never do it this way: I'd use a connection
pool (see below).

> I also just upgrade my struts could it be a taglib thing?

This should have nothing to do with either Struts or your taglibs
(unless you use taglibs for SQL queries... <shiver>).

My recommendation is to use a JNDI datasource and then use that for both
your "in-app" database connections as well as the <Realm>. Basically,
you change your <Context> element to include:

<Resource [see documentation for JNDI DataSource] />
<Realm [see documentation for DataSourceRealm;
        don't forget to set localDataSource="true"] />

Then, in your code, you'll need a slightly more complicated way of
getting a database connection. But, you already have your "get
connection" logic in a method used everywhere, right?

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.swl.DataSource;

public Connection getConnection()
    throws SQLException, NamingException
{
    Context ctx = new InitialContext();

    DataSource ds = (DataSource)ctx.lookup(DATASOURCE_PATH);

    if(null == ds)
	throw new NamingException("Cannot obtain DataSource");

    return ds.getConnection();
}

This way, both the Realm and your application are using the same pool of
connections. This avoids weird situations where your realm works but not
your application (and vice versa).

Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGe8AQ9CaO5/Lv0PARAmz5AJoCusXnwWzOobA8UaJxaLp0iJfdMwCguf9S
OpmaxB78ll5KigAJdUB4At4=
=Q5C4
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Realm works; db connection does not?

Posted by Søren Blidorf <so...@nolas.dk>.
Hi again.

I do mean realm and the reason why I mention it is that I can validate users
via my users and user_roles in my db, so the connection should work I guess?


        <Context path="/xx" docBase="webapp" reloadable="true">
        <Manager pathname="xx" />
         <Realm  name="UMRealm"
className="org.apache.catalina.realm.JDBCRealm"
         debug="99"
driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"

connectionURL="jdbc:sqlserver://127.0.0.1;DatabaseName=xx;ProgramName=xx;Sel
ectMethod=cursor"
         connectionName="xx" connectionPassword="xx" userTable="users"
userNameCol="username" userCredCol="password" userRoleTable="user_roles"
roleNameCol="rolename" />
        </Context>

Get the connection with: DriverManager.getConnection(    connectionUrl,
connectionUser,    connectionPass)

I also just upgrade my struts could it be a taglib thing?

In my JSP:

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="logic" %>

In my web.xml:

  <taglib>
    <taglib-uri>http://struts.apache.org/tags-bean</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>http://struts.apache.org/tags-html</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>http://struts.apache.org/tags-logic</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>http://struts.apache.org/tags-nested</taglib-uri>
    <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
  </taglib>

----- Original Message -----
From: "Johnny Kewl" <jo...@kewlstuff.co.za>
To: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Friday, June 22, 2007 1:00 PM
Subject: Re: Realm works; db connection does not?


>
> Hi Søren,
>
> Not sure what you saying but arnt you confusing Realm and JNDI.
> ie the Realm is for your user and password dB
> and JNDI/DBCP is for you to do other stuff with your dB....
>
> Think you almost there just check out
> http://localhost:8080/tomcat-docs/jndi-resources-howto.html
> on your system
>
> If this is not the problem, please post your config and code you using....
>
> ----- Original Message -----
> From: "Søren Blidorf" <so...@nolas.dk>
> To: <us...@tomcat.apache.org>
> Sent: Friday, June 22, 2007 10:44 AM
> Subject: Realm works; db connection does not?
>
>
> Hi.
>
> My realm is working and is able to login to my app. When I try to do
> anything in the application that need connection to db, I get a white
> screen.
>
> Can anybody tell me the differance between the realm connection to the db
> and my applications connection to the db.
>
> I use: com.microsoft.sqlserver.jdbc.SQLServerDriver and jdbc:sqlserver
>
> Any idears is appriciated
>
> Soren
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Realm works; db connection does not?

Posted by Johnny Kewl <jo...@kewlstuff.co.za>.
Hi Søren,

Not sure what you saying but arnt you confusing Realm and JNDI.
ie the Realm is for your user and password dB
and JNDI/DBCP is for you to do other stuff with your dB....

Think you almost there just check out
http://localhost:8080/tomcat-docs/jndi-resources-howto.html
on your system

If this is not the problem, please post your config and code you using....

----- Original Message ----- 
From: "Søren Blidorf" <so...@nolas.dk>
To: <us...@tomcat.apache.org>
Sent: Friday, June 22, 2007 10:44 AM
Subject: Realm works; db connection does not?


Hi.

My realm is working and is able to login to my app. When I try to do 
anything in the application that need connection to db, I get a white 
screen.

Can anybody tell me the differance between the realm connection to the db 
and my applications connection to the db.

I use: com.microsoft.sqlserver.jdbc.SQLServerDriver and jdbc:sqlserver

Any idears is appriciated

Soren


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org