[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Database usage for database objects
> Per Nyfelt wrote:
>
> Is there a way to get access to the Database from "within" it without
> harcoding location and port number?
> I.e. if you have an object stored in the db that needs to
> manage/interact with other objects in the same db can you get to the
> open database without doing something like:
> db = ExternalDatabase.openDatabase("ozonedb:remote://localhost:3333");
>
> I was looking for something maybe like
> db = ExternalDatabase.openDatabase(); ( or perhaps
> LocalDatabase.openDatabase() )
> Is there some way LocalDatabase can be used without specifying where
> it is?
Hi Per,
In J2EE, they have a configuration file where all the data sources are
listed. On startup all the sources get added to the registry which is
accessed via JNDI in the code. The reference (which is a connection
pool) is requested from JNDI and then the code can getConnection() from
the Datasource.
This is what the code looks like.
public class BankAccountBean implements EntityBean {
// this is internal hard code that never has to be changed
private String logicalDBName = "java:comp/env/jdbc/BankAccountDB";
private void makeConnection() throws NamingException, SQLException {
System.out.println("BankAccountBean: makeConnection() executing");
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(logicalDBName);
con = ds.getConnection();
}
}
Here's what the file looks like.
jdbc.datasources=jdbc/Cloudscape|jdbc:cloudscape:rmi:CloudscapeDB;create=true
The name is jdbc/Cloudscape and the url is after the | symbol.
jdbc/cloudscape is mapped to java:comp/env/jdbc/BankAccountDB when the
component is deployed. The component has a private location in JNDI so
to components built by two different vendors could need a
jdbc/BankAcoountDB but at deployment time they could resolve to
different datasources.
Shoot, I thought this was a development question. Sorry.
If you want a quick hack. Create a openDatabase() method that looks for
a property called ozone.url and then uses that name to call
openDatabase(String url). Start the VM like this.
java -Dozone.url=ozonedb:remote://localhost:3333 MyApplication
Then you have no internal hard code.
Eric