Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Database Connection question

Thread view: 
JY - 02 Nov 2005 23:56 GMT
I have 2 applications: one is using JBoss where the database connection is
obtained through JNDI lookup; the other is a standalone app and is using
java.sql.DriverManager for Connection. Every time after obtaining a
Connection, a stored proc is called.
My task is to write generic code for database connection to be used in both
application. Please give some input on what should be the best way.
Thanks
Pete Barrett - 03 Nov 2005 20:19 GMT
>I have 2 applications: one is using JBoss where the database connection is
>obtained through JNDI lookup; the other is a standalone app and is using
>java.sql.DriverManager for Connection. Every time after obtaining a
>Connection, a stored proc is called.
>My task is to write generic code for database connection to be used in both
>application. Please give some input on what should be the best way.

DriverManager will work from within JBoss; a JNDI lookup will only
work from outside if you have an InitialContext from somewhere; so the
only universal way to obtain a database connection is to use
DriverManager both within your bean (or servlet, or whatever), and in
the standalone application. But I can't beleive that that's an answer
to the question you had in mind, because you obviously know that
already.

What exactly do you mean by 'generic' code? All the database code will
be generic once the Connection is obtained, so what I usually do is to
centralise all the database access in a single class, and provide the
Connection in an open() method. Is that the sort of 'generic' code you
had in mind? (Obviously, there are other equally generic ways of doing
it apart from the way I prefer.)

Pete Barrett
JY - 03 Nov 2005 22:15 GMT
>>I have 2 applications: one is using JBoss where the database connection is
>>obtained through JNDI lookup; the other is a standalone app and is using
[quoted text clipped - 10 lines]
> had in mind? (Obviously, there are other equally generic ways of doing
> it apart from the way I prefer.)

yes thats what i have in my mind. I have identified that
1) to get a javax.sql.DataSource connection, I need:
1.1) a JNDI name

2) to ge a java.sql.DriverManager connection, I need:
2.1) Driver Name
2.2) Database URL
2.3) Database User Id
2.4) Database Password

Now I can create one class which has 2 methods:
public class ConnectionUtil {
 // used by the app using JBoss
 public Connection getDataSourceConnection( String jndiName ) {...}

 // used by the app not using JBoss
 public Connection getDriverManagerConnection( String driverName, String
databaseUrl, String databaseUserId, String databasePassword) {...}
}

Another option i can think of which is cleaner is to have a Connection
interface:
public interface ConnectionI {
 public Connection getConnection(Properties props);
}

Then 2 seperate classes one for DataSource and one for DriverManager which
implements the interface
public class DataSourceConnection implements ConnectionI {
 private String jndiName;

 // props contains JNDI Name
 public Connection getConnection(Properties props) {...}
}

public class DriverManagerConnection implements ConnectionI {
 private String driverName;
 private String databaseUrl;
 private String databaseUserId;
 private String databasePassword;

 // props contains Driver Name, Database Url, Database User Id, Database
Password
 public Connection getConnection(Properties props) {...}
}

Then a factory:
public class ConnectionFactory {
 public static final int DATASOURCE = 1;
 public static final int DRIVERMANAGER = 2;

 public ConnectionI getConnectionI( int connType, Properties props ) {
   switch(connType)
     case DRIVERMANAGER:
       return new DriverManagerConnection( props );
     case DATASOURCE:
       return new DataSourceConnection( props);
     default:
       return null;
 }
}

So the test code to get a DataSource Connection would be:
Properties props = new Properties();
prop.put("JNDINAME", "MSSQLDS");
ConnectionI connectionI = ConnectionFactory.getConnectionI(1, props);
java.sql.Connection conn = connectionI.getConnection();

And the test code to get a DriverManager Connection would be:
Properties props = new Properties();
prop.put("DRIVERNAME", "sun.jdbc.odbc.JdbcOdbcDriver");
prop.put("DATABASEURL", "jdbc:odbc:database_odbc");
prop.put("DATABASEUSERID", "sa");
prop.put("DATABASEPASSWORD", "password");
ConnectionI connectionI = ConnectionFactory.getConnectionI(2, props);
java.sql.Connection conn = connectionI.getConnection();

What do you think which solution is more gneric and more maintainable?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.