How do I lookup a JNDI Datasource from outside a web container?

后端 未结 5 1165
半阙折子戏
半阙折子戏 2020-12-23 10:47

I have the following environment set up:

  • Java 1.5
  • Sun Application Server 8.2
  • Oracle 10 XE
  • Struts 2
  • Hibernate

5条回答
  •  感情败类
    2020-12-23 11:11

    Try Simple-JNDI. It gives you an in-memory implementation of a JNDI Service and allows you to populate the JNDI environment with objects defined in property files. There is also support for loading datasources or connection pools configured in a file.

    To get a connection pool you have to create a file like this:

    type=javax.sql.DataSource
    driver=com.sybase.jdbc3.jdbc.SybDriver
    pool=myDataSource
    url=jdbc:sybase:Tds:servername:5000
    user=user
    password=password
    

    In your application you can access the pool via

    Context ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup("path/to/your/connectionPool");
    

    I haved used Simple-JNDI for this purpose for years now. But it is not under active development anymore. Because I found some issues concerning shared contexts (especially using datasources), I decided to branch the original project and to add some new features. Now there is a 0.13.0. You can find more about it at https://github.com/h-thurow/Simple-JNDI.

提交回复
热议问题