tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Søren Blidorf <so...@nolas.dk>
Subject Re: Realm works; db connection does not?
Date Fri, 22 Jun 2007 16:58:01 GMT
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" <chris@christopherschultz.net>
To: "Tomcat Users List" <users@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


Mime
View raw message