-
Can MySQL DataSource be created in a Context?
I am trying to learn how to use datasources to get connections,
and I am successful in doing this for the MysqlDataSource in this way:
connection = mysqlDataSource.getConnection();
However, I want to try to retrieve the connection via a Context,
and am unsuccessful in creating the initial association, like this:
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUrl ( "jdbc:mysql://127.0.0.1:3306/test" );
dataSource.setUser( stringUser );
dataSource.setPassword( stringPass );
//
String stringDataSourceMYS = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource";
Properties properties = new Properties();
properties.put( Context.INITIAL_CONTEXT_FACTORY, stringDataSourceMYS );
properties.put( Context.PROVIDER_URL , "jdbc:mysql://127.0.0.1:3306/test" );
//
Context context = new InitialContext( properties );
context.bind( "jdbc/mySql" , dataSource );
It renders the following error:
javax.naming.NoInitialContextException:
Cannot instantiate class: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
[Root exception is java.lang.ClassCastException]
I do not think this is a path issue (since it works w/o using the context bind).
I do not know what I need to make it "bind" properly.
Can anyone help with this? Thank You.
Last edited by mamgeorge; 05-31-2007 at 10:24 AM.
Reason: clarity
-
It can be done.
The properties of the Context do not show an appropriate provider url for the hosting of the datasource. If you are avoiding an App server (for a blackbox demo, for example) create an RMI registry server, create the datasource (with its own URL), and bind the datasource to the context with the provider url of the RMI server:
LocateRegistry.createRegistry( intPortRMI );
//
MysqlDataSource mysDataSource = new MysqlDataSource();
mysDataSource.setUrl ( stringUrlMYS ); // setPort setServerName
mysDataSource.setUser ( stringUser );
mysDataSource.setPassword ( stringPass );
DataSource dataSource = ( DataSource ) mysDataSource ;
//
Properties properties = new Properties();
properties.put( Context.INITIAL_CONTEXT_FACTORY, stringFactRmi );
properties.put( Context.PROVIDER_URL, stringUrlRmi );
//
InitialContext context = new InitialContext( properties );
context.rebind( "anyLookupName" , mysDataSource );
Similar Threads
-
Replies: 0
Last Post: 03-27-2007, 09:56 AM
-
By Moonster in forum Java
Replies: 0
Last Post: 11-22-2006, 10:06 AM
-
By manoj nahar in forum Database
Replies: 0
Last Post: 03-07-2002, 01:11 AM
-
By frest in forum oracle.general
Replies: 0
Last Post: 05-01-2001, 07:33 AM
-
By Nick Koszykowski in forum VB Classic
Replies: 0
Last Post: 03-28-2000, 02:21 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks