You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Holger Veltrup <ve...@sitepark.com> on 2011/02/07 10:37:40 UTC

dbcp with embedded tomcat7

Hey,

how can i use dbcp with embedded tomcat7

this example doesn't work:


public static void main(String[] args) throws Exception {

  String appBase = args[0];
  Integer port = Integer.valueOf(args[1]);

  Tomcat tomcat = new Tomcat();
  tomcat.setPort(port);

  tomcat.setBaseDir(".");
  tomcat.getHost().setAppBase(appBase);

  String contextPath = "/myapp";
 
  // Add AprLifecycleListener
  StandardServer server = (StandardServer)tomcat.getServer();
  AprLifecycleListener listener = new AprLifecycleListener();
  server.addLifecycleListener(listener);

  tomcat.addWebapp(contextPath, appBase);
  tomcat.enableNaming();


  tomcat.start();

  // From http://commons.apache.org/dbcp/guide/jndi-howto.html
  Reference ref = new Reference("javax.sql.DataSource",
                    "org.apache.commons.dbcp.BasicDataSourceFactory",
                    null);
  ref.add(new StringRefAddr("auth", "Container"));
  ref.add(new StringRefAddr("factory",
"org.apache.tomcat.jdbc.pool.DataSourceFactory"));
  ...
 
  InitialContext ic = new InitialContext();
  ic.rebind("jdbc/basic", ref);

  InitialContext ic2 = new InitialContext();
  DataSource ds = (DataSource) ic2.lookup("jdbc/basic");

  tomcat.getServer().await();
}


what's wrong? Can anybody help me?

Regards,
Holger

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


Re: dbcp with embedded tomcat7

Posted by Konstantin Kolinko <kn...@gmail.com>.
2011/2/7 Holger Veltrup <ve...@sitepark.com>:
>  InitialContext ic = new InitialContext();

In short:
1) InitialContext depends on classloader:  the context you created
here and the one that your webapp will create with "new
InitialContext()" are different.
2) Usually binding a pool after tomcat.start() will be too late if
webapp obtains the reference to the pool during its startup. Though if
it does jdbc lookup only when needed you will be fine.

Best regards,
Konstantin Kolinko

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