Data Source for Pooled Connection and Distributed Transactions.
Tutorial-1: http://www.java2s.com/Code/Java/Database-SQL-JDBC/OracleConnectionPoolDataSource.htm
Tutorial-2: http://docs.oracle.com/javase/tutorial/jdbc/basics/sqldatasources.html
Tutorial-3: http://docs.oracle.com/cd/B14117_01/java.101/b10979/urls.htm
Tutorial-4: http://www.orafaq.com/wiki/JDBC
package com.struts1.tutorials.datasource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.PooledConnection;
import oracle.jdbc.pool.OracleConnectionPoolDataSource;
public class DataSourceConnPoolTest {
private static final String SQL_QUERY1 = "SELECT * fROM customers";
private static final String QUERY_FIELD1 = "CUSTOMER_NAME";
private static final String SQL_QUERY2 = "SELECT count(*) FROM v$session WHERE username = 'SYS'";
public static void main(String[] args) {
PooledConnection pc_1 = null;
PooledConnection pc_2 = null;
Connection conn_1 = null;
Connection conn_2 = null;
Connection conn_3 = null;
Statement stmt = null;
ResultSet rset = null;
try {
OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource();
ocpds.setURL("jdbc:oracle:thin:@localhost:1521:XE");
ocpds.setUser("lakshman01");
ocpds.setPassword("passwd");
pc_1 = ocpds.getPooledConnection();
conn_1 = pc_1.getConnection();
stmt = conn_1.createStatement();
rset = stmt.executeQuery(SQL_QUERY1);
rset.next();
String msg = "Data = ";
System.out.println(msg + "pc-1 - conn_1: " + rset.getString(QUERY_FIELD1));
if (stmt != null) {
stmt.close();
}
if (rset != null) {
rset.close();
}
conn_2 = pc_1.getConnection();
stmt = conn_2.createStatement();
rset = stmt.executeQuery(SQL_QUERY1);
rset.next();
System.out.println(msg + "pc-1 - conn_2: " + rset.getString(QUERY_FIELD1));
if (stmt != null) {
stmt.close();
}
if (rset != null) {
rset.close();
}
pc_2 = ocpds.getPooledConnection();
conn_3 = pc_2.getConnection();
stmt = conn_3.createStatement();
rset = stmt.executeQuery(SQL_QUERY1);
rset.next();
System.out.println(msg + "pc_2 - conn_3: " + rset.getString(QUERY_FIELD1));
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
if (stmt != null) {
stmt.close();
}
}
catch (Exception ignore) {}
try {
if (rset != null) {
rset.close();
}
}
catch (Exception ignore) {}
try {
if (conn_1 != null)
conn_1.close();
}
catch (Exception ignore) {}
try {
if (conn_2 != null)
conn_2.close();
}
catch (Exception ignore) {}
try {
if (conn_3 != null)
conn_3.close();
}
catch (Exception ignore) {}
try {
if (pc_1 != null)
pc_1.close();
}
catch (Exception ignore) {}
try {
if (pc_2 != null)
pc_2.close();
}
catch (Exception ignore) {}
}
}
}
Sample Code to use OracleDataSource instead of old DriverManager.
(Remember this is not pooled.)
************
package com.struts1.tutorials.datasource;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.*;
import oracle.jdbc.pool.OracleDataSource;
public class DataSourceTest {
private static final String SQL_QUERY1 = "SELECT * fROM customers";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
OracleDataSource ods = new OracleDataSource();
ods.setDriverType("thin");
ods.setDatabaseName("XE");
ods.setServerName("localhost");
ods.setPortNumber(1521);
ods.setUser("lakshman01");
ods.setPassword("passwd");
/*
* Not an EJB - App. So don't bother about context
Context ctx = new InitialContext();
ctx.bind("jdbc/tutorialdb", ods);
OracleDataSource odsconn = (OracleDataSource)ctx.lookup("jdbc/sampledb");
*/
conn = ods.getConnection();
stmt = conn.createStatement();
rset = stmt.executeQuery(SQL_QUERY1);
while (rset.next()) {
System.out.println ("Customer ID = " + rset.getInt("CUSTOMER_ID"));
System.out.println ("Customer Name = " + rset.getString("CUSTOMER_NAME"));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
if (rset != null)
rset.close();
}
catch (Exception ignore) {}
try {
if (stmt != null)
stmt.close();
}
catch (Exception ignore) {}
try {
if (conn != null)
conn.close();
}
catch (Exception ignore) {}
}
}
}
Tutorial-1: http://www.java2s.com/Code/Java/Database-SQL-JDBC/OracleConnectionPoolDataSource.htm
Tutorial-2: http://docs.oracle.com/javase/tutorial/jdbc/basics/sqldatasources.html
Tutorial-3: http://docs.oracle.com/cd/B14117_01/java.101/b10979/urls.htm
Tutorial-4: http://www.orafaq.com/wiki/JDBC
package com.struts1.tutorials.datasource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.PooledConnection;
import oracle.jdbc.pool.OracleConnectionPoolDataSource;
public class DataSourceConnPoolTest {
private static final String SQL_QUERY1 = "SELECT * fROM customers";
private static final String QUERY_FIELD1 = "CUSTOMER_NAME";
private static final String SQL_QUERY2 = "SELECT count(*) FROM v$session WHERE username = 'SYS'";
public static void main(String[] args) {
PooledConnection pc_1 = null;
PooledConnection pc_2 = null;
Connection conn_1 = null;
Connection conn_2 = null;
Connection conn_3 = null;
Statement stmt = null;
ResultSet rset = null;
try {
OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource();
ocpds.setURL("jdbc:oracle:thin:@localhost:1521:XE");
ocpds.setUser("lakshman01");
ocpds.setPassword("passwd");
pc_1 = ocpds.getPooledConnection();
conn_1 = pc_1.getConnection();
stmt = conn_1.createStatement();
rset = stmt.executeQuery(SQL_QUERY1);
rset.next();
String msg = "Data = ";
System.out.println(msg + "pc-1 - conn_1: " + rset.getString(QUERY_FIELD1));
if (stmt != null) {
stmt.close();
}
if (rset != null) {
rset.close();
}
conn_2 = pc_1.getConnection();
stmt = conn_2.createStatement();
rset = stmt.executeQuery(SQL_QUERY1);
rset.next();
System.out.println(msg + "pc-1 - conn_2: " + rset.getString(QUERY_FIELD1));
if (stmt != null) {
stmt.close();
}
if (rset != null) {
rset.close();
}
pc_2 = ocpds.getPooledConnection();
conn_3 = pc_2.getConnection();
stmt = conn_3.createStatement();
rset = stmt.executeQuery(SQL_QUERY1);
rset.next();
System.out.println(msg + "pc_2 - conn_3: " + rset.getString(QUERY_FIELD1));
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
if (stmt != null) {
stmt.close();
}
}
catch (Exception ignore) {}
try {
if (rset != null) {
rset.close();
}
}
catch (Exception ignore) {}
try {
if (conn_1 != null)
conn_1.close();
}
catch (Exception ignore) {}
try {
if (conn_2 != null)
conn_2.close();
}
catch (Exception ignore) {}
try {
if (conn_3 != null)
conn_3.close();
}
catch (Exception ignore) {}
try {
if (pc_1 != null)
pc_1.close();
}
catch (Exception ignore) {}
try {
if (pc_2 != null)
pc_2.close();
}
catch (Exception ignore) {}
}
}
}
Sample Code to use OracleDataSource instead of old DriverManager.
(Remember this is not pooled.)
************
package com.struts1.tutorials.datasource;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.*;
import oracle.jdbc.pool.OracleDataSource;
public class DataSourceTest {
private static final String SQL_QUERY1 = "SELECT * fROM customers";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
OracleDataSource ods = new OracleDataSource();
ods.setDriverType("thin");
ods.setDatabaseName("XE");
ods.setServerName("localhost");
ods.setPortNumber(1521);
ods.setUser("lakshman01");
ods.setPassword("passwd");
/*
* Not an EJB - App. So don't bother about context
Context ctx = new InitialContext();
ctx.bind("jdbc/tutorialdb", ods);
OracleDataSource odsconn = (OracleDataSource)ctx.lookup("jdbc/sampledb");
*/
conn = ods.getConnection();
stmt = conn.createStatement();
rset = stmt.executeQuery(SQL_QUERY1);
while (rset.next()) {
System.out.println ("Customer ID = " + rset.getInt("CUSTOMER_ID"));
System.out.println ("Customer Name = " + rset.getString("CUSTOMER_NAME"));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
if (rset != null)
rset.close();
}
catch (Exception ignore) {}
try {
if (stmt != null)
stmt.close();
}
catch (Exception ignore) {}
try {
if (conn != null)
conn.close();
}
catch (Exception ignore) {}
}
}
}
No comments:
Post a Comment