Junit Testing JNDI InitialContext outside the application server

前端 未结 3 1463
南笙
南笙 2020-12-31 01:47
Context context = new InitialContext();
dataSource = (DataSource) context.lookup(\"java:comp/env/jdbc/multiDS\");
connection = dataSource.getConnection();

3条回答
  •  感情败类
    2020-12-31 02:12

    This is easily done with Simple-JNDI. Create a property file "jdbc/multiDS.properties" in your working directory to configure your datasource with these properties:

    type=javax.sql.DataSource
    driver=org.gjt.mm.mysql.Driver
    url=jdbc:mysql://localhost/testdb
    user=testuser
    password=testing
    

    Then instantiate the context with

    final Hashtable env = new Hashtable();
    env.put("org.osjava.sj.root", "working_dir");
    env.put("org.osjava.sj.jndi.shared", "true");
    env.put("java.naming.factory.initial", "org.osjava.sj.SimpleContextFactory");
    env.put("org.osjava.sj.delimiter", "/");
    env.put("org.osjava.sj.space", "java:comp/env")
    Context ctx = new InitialContext(env);
    

    After that you can call

    dataSource = (DataSource) context.lookup("java:comp/env/jdbc/multiDS");
    

    Find more info about Simple-JNDI here https://github.com/h-thurow/Simple-JNDI

提交回复
热议问题