dcsimg
JDBC CONNECTIONS RECURSIVE WAY
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, April 13, 2010 05:10 PM

JGURU TEAM Experts JDBC Programming I request your help in the following topic, i am doing a java program that store a complete file system in an Oracle Database 11GR2. So i use a recursive algorithm to do that but i don't know how to handle the JDBC connections in order to not overload the total of physical connections available to the database. Thanks for your help. Regards from Colombia - South America JOHN JAIRO GOMEZ LAVERDE I am using the following code: ********************* import java.io.* ; import java.sql.*; import java.lang.String; import   More>>

JGURU TEAM

Experts JDBC Programming





I request your help in the following topic,
i am doing a java program that store a
complete file system in an Oracle Database 11GR2.


So i use a recursive algorithm to do that
but i don't know how to handle the JDBC
connections in order to not overload the total of
physical connections available to the database.


Thanks for your help.

Regards from Colombia - South America

JOHN JAIRO GOMEZ LAVERDE


I am using the following code:

			
*********************
import java.io.* ;
import java.sql.*;
import java.lang.String;
import java.util.Date;
import java.util.Calendar;
import java.util.*;
import java.text.SimpleDateFormat;
import oracle.jdbc.*;
import oracle.jdbc.pool.*;
import javax.naming.*;
import javax.naming.spi.*;

public class InsertArcDir
{
public void AddFiles( File file )
{
try
{
Calendar fechamod=Calendar.getInstance();
String SQL;
PreparedStatement pstmt = null;
//
java.util.Properties prop = new java.util.Properties();
prop.setProperty("MinLimit", "2");
prop.setProperty("MaxLimit", "10");
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:@//oracle:1521/xe");
ods.setUser("hr");
ods.setPassword("hr");
ods.setConnectionCachingEnabled(true);
ods.setConnectionCacheProperties (prop);
//ods.getConnectionCacheName();
Connection conn = ods.getConnection ("hr","hr",prop);

//
Statement stmt = conn.createStatement();


fechamod.setTimeInMillis(file.lastModified());
SQL="INSERT INTO tbarchivos(nombrearc,fechaarc,direcarc) VALUES(?,?,?)";
pstmt = conn.prepareStatement(SQL);
java.sql.Timestamp sqlDate = new java.sql.Timestamp(fechamod.getTimeInMillis());
pstmt.setString(1, file.getName());
pstmt.setTimestamp(2, sqlDate);
pstmt.setString(3, file.getParent());
pstmt.executeUpdate();
stmt.close();
conn.close();
}
catch(Exception e)
{
System.out.println("Exepcion"+e.getMessage());
System.out.println("OTHER TEST");
}
}
/**
* Works on a single file system entry and
* calls itself recursively if it turns out
* to be a directory.
* @param file A file or a directory to process
*/
public void traverse( File file )
{

// Check if it is a directory

if( file.isDirectory() )
{
// Get a list of all the entries in the directory
String entries[] = file.list() ;

// Ensure that the list is not null
if( entries != null )
{
// Loop over all the entries
for( String entry : entries )
{
// Recursive call to traverse
traverse( new File(file,entry) ) ;
}
}
}
else
{
if(file.isFile())
{
InsertArcDir rta = new InsertArcDir() ;
rta.AddFiles(file);


}
else
{
System.out.println("*** VALOR INCORRECTO ***");
}
}
//try
//{
//stmt.close();
//conn.close();
//}
//catch(Exception e)
//{
// System.out.println("Exepcion"+e.getMessage());
//}
}

/**
* The program starts here.
* @param args The arguments from the command line
*/
public static void main( String args[] )
{
try
{
java.util.Properties prop = new java.util.Properties();
prop.setProperty("MinLimit", "2");
prop.setProperty("MaxLimit", "10");
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:@//oracle:1521/xe");
ods.setUser("hr");
ods.setPassword("hr");
ods.setConnectionCachingEnabled(true);
ods.setConnectionCacheProperties (prop);
ods.setConnectionCacheName("ImplicitCache01");
Connection conn = ods.getConnection ();
conn.close();


//Statement stmt = conn.createStatement();
// Create an object of this class
InsertArcDir rt = new InsertArcDir() ;

if( args.length == 0 )
{
// If there are no arguments, traverse the current directory
rt.traverse( new File(".") ) ;
}
else
{
// Else process every argument sequentially
for( String arg : args )
{
rt.traverse( new File(arg) ) ;
}
}
//stmt.close();
//conn.close();
}
catch(Exception e)
{
System.out.println("Exepcion"+e.getMessage());
}
}
}
*********************
   <<Less

Re: JDBC CONNECTIONS RECURSIVE WAY

Posted By:   tim_fox  
Posted On:   Tuesday, May 11, 2010 11:14 AM

You could use a connection pool instead of explicit connections. This way, each login done by your application will use an existing, pooled connection instead of creating a new one. Since your application is releasing each connection when it finishes, you should only occupy one (up to a few) connection at any time.


If you are using Tomcat 5.x or later, Apache's DBCP is built in and just needs to be configured. Check the Tomcat docs under JDBC and it will explain how to get Connection Pooling set up.


Good Luck!


TGF

About | Sitemap | Contact