JDBC Driver for Snowflake

Build 23.0.8839

JNDI

Connection Pooling with JNDI

The Java Naming and Directory Service (JNDI) is an API which allows distributed application to look up services. JNDI can be used to easily set up connection pools.

To set up a connection pool using JNDI, you will need to initialize the JNDI File System Service Provider, as shown in the example code below. To run the example, you need to add the fscontext.jar and providerutil.jar files to your classpath. You can download these files from the Oracle Java Archive: Under the Java SE section, select Java Platform Technologies > Java Naming and Directory Interface.

 
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:///tmp");

Context ctx = new InitialContext(env); 
DataSource ds = null;
Connection conn = null;
The following code registers the SnowflakeDataSource with the JNDI naming service, gets an instance of the DataSource from the service, and creates pooled connections from that instance.
try {
  SnowflakeConnectionPoolDataSource snowflakeDataSource = new SnowflakeConnectionPoolDataSource();
  snowflakeDataSource.setURL("jdbc:snowflake:UseConnectionPooling=true;url=https://myaccount.region.snowflakecomputing.com;user=Admin;password=test123;Database=Northwind;Warehouse=TestWarehouse;Account=Tester1;");
  ctx.bind("jdbc/snowflake", snowflakeDataSource);
  ds = (DataSource) ctx.lookup("jdbc/snowflake");

  conn = ds.getConnection();
  Statement stat = conn.createStatement();
  boolean ret = stat.execute("SELECT 1");
  ResultSet rs=stat.getResultSet(); 
} catch(Exception ex) { } finally {
  if(conn != null) conn.close();
}

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 23.0.8839