You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@empire-db.apache.org by David Marko <dm...@tiscali.cz> on 2008/09/25 12:13:15 UTC

How to use it with web application?

Hello,
can anyone share some common scenario on how to initialise necessary things in web application using 
e.g. SpringFramework?

##### Using the code from the EmpireDB Site:
config.init((args.length > 0 ? args[0] : "config.xml"));
// STEP 1: Get a JDBC Connection
Connection conn = getJDBCConnection();
In step two the sample creates and initializes a database driver object for the target DBMS. This is 
HSQLDB by default.

// STEP 2: Choose a driver
DBDatabaseDriver driver = getDatabaseDriver(config.getDatabaseProvider());
Then in step three the database object is opened using the driver. Only when opened, other methods 
of the database object may be used. Finally we check whether or not our database objects exist.

// STEP 3: Open Database and check if tables exist
db.open(driver, conn);
databaseExists(conn);
In order to check existence of the database the sample simply performs a query on the Departments 
table ("select count(*) from DEPARTMENTS") using the following code:

DBCommand cmd = db.createCommand();
cmd.select(db.DEPARTMENTS.count());
db.querySingleInt(cmd.getSelect(), -1, conn);
###

... should/can be the conn and db initialised using some singleton pattern and used within the 
application in this way?

Thanks for info,
David

AW: How to use it with web application?

Posted by Rainer Döbele <do...@esteam.de>.
Hi David,

not sure what you mean by schema migration.

In the FAQ it says for example that you can dynamically change the datamodel / schema at runtime (it that what you mean?). There is an example for that in the advanced Empire-db example application called DBSampleAdv. This has nothing to do with migration though.

Rainer

David Marko wrote:
> 
> Hi Rainer,
> thanks for pointing me to 'Empire-Struts2 Example Application'. Will look
> at this, but provided
> descrition seems to be obvious.
> 
> Anothoner question :-) In FAQ is said, that there is schema migration
> available. How does it
> work/can be used? Does it work automticly in some way or is there any docs
> about it?
> 
> 
> Thanks once again,
> David
> 
> Rainer Döbele wrote:
> > Hi David,
> >
> > in a web-application you should ideally work with connection pooling,
> which is usually done by the application server (i.e. Apache-Tomcat).
> >
> > We recommend the following:
> >
> > 1. create an Application object which is instantiated and initialized
> once when the web-application is loaded in the web application server.
> This can be done via a custom filter class registered in the web.xml
> definition file of your web-application. The application object is a
> singleton.
> >
> > 2. use a JNDI context to access a datasource provided by the application
> server. When initializing your application obtain a Connection from the
> data source and initialize your DB-Objects (create the driver, open the
> database, check the schema, etc.)
> >
> > 3. for each request, obtain a connection from the pool which you can
> then pass to the appropriate Empire-db function. When the request finished
> close the connection which will effectively return it to the pool.
> >
> > I recommend downloading and looking at our Empire-Struts2 Example
> Application called DBWebSample provided with the distribution of the
> empire-struts2-extentions. Even if you don't intend to use Struts2 as your
> web application framework it gives a good idea on how to use Empire-db in
> a web application. You should particularly look the init() method of the
> SampleApplication class. However the sample application does not implement
> connection pooling - but it can be easily added.
> >
> > Hope this is the kind of answer you expected.
> >
> > Regards
> > Rainer
> >
> > David Marko wrote:
> >> Hello,
> >> can anyone share some common scenario on how to initialise necessary
> >> things in web application using
> >> e.g. SpringFramework?
> >>
> >> ##### Using the code from the EmpireDB Site:
> >> config.init((args.length > 0 ? args[0] : "config.xml"));
> >> // STEP 1: Get a JDBC Connection
> >> Connection conn = getJDBCConnection();
> >> In step two the sample creates and initializes a database driver object
> >> for the target DBMS. This is
> >> HSQLDB by default.
> >>
> >> // STEP 2: Choose a driver
> >> DBDatabaseDriver driver =
> getDatabaseDriver(config.getDatabaseProvider());
> >> Then in step three the database object is opened using the driver. Only
> >> when opened, other methods
> >> of the database object may be used. Finally we check whether or not our
> >> database objects exist.
> >>
> >> // STEP 3: Open Database and check if tables exist
> >> db.open(driver, conn);
> >> databaseExists(conn);
> >> In order to check existence of the database the sample simply performs
> a
> >> query on the Departments
> >> table ("select count(*) from DEPARTMENTS") using the following code:
> >>
> >> DBCommand cmd = db.createCommand();
> >> cmd.select(db.DEPARTMENTS.count());
> >> db.querySingleInt(cmd.getSelect(), -1, conn);
> >> ###
> >>
> >> ... should/can be the conn and db initialised using some singleton
> pattern
> >> and used within the
> >> application in this way?
> >>
> >> Thanks for info,
> >> David
> >


Re: How to use it with web application?

Posted by David Marko <dm...@tiscali.cz>.
Hi Rainer,
thanks for pointing me to 'Empire-Struts2 Example Application'. Will look at this, but provided 
descrition seems to be obvious.

Anothoner question :-) In FAQ is said, that there is schema migration available. How does it 
work/can be used? Does it work automticly in some way or is there any docs about it?


Thanks once again,
David

Rainer Döbele wrote:
> Hi David,
> 
> in a web-application you should ideally work with connection pooling, which is usually done by the application server (i.e. Apache-Tomcat).
> 
> We recommend the following:
> 
> 1. create an Application object which is instantiated and initialized once when the web-application is loaded in the web application server. This can be done via a custom filter class registered in the web.xml definition file of your web-application. The application object is a singleton.
> 
> 2. use a JNDI context to access a datasource provided by the application server. When initializing your application obtain a Connection from the data source and initialize your DB-Objects (create the driver, open the database, check the schema, etc.)
> 
> 3. for each request, obtain a connection from the pool which you can then pass to the appropriate Empire-db function. When the request finished close the connection which will effectively return it to the pool.
> 
> I recommend downloading and looking at our Empire-Struts2 Example Application called DBWebSample provided with the distribution of the empire-struts2-extentions. Even if you don't intend to use Struts2 as your web application framework it gives a good idea on how to use Empire-db in a web application. You should particularly look the init() method of the SampleApplication class. However the sample application does not implement connection pooling - but it can be easily added.
> 
> Hope this is the kind of answer you expected.
> 
> Regards
> Rainer
> 
> David Marko wrote:
>> Hello,
>> can anyone share some common scenario on how to initialise necessary
>> things in web application using
>> e.g. SpringFramework?
>>
>> ##### Using the code from the EmpireDB Site:
>> config.init((args.length > 0 ? args[0] : "config.xml"));
>> // STEP 1: Get a JDBC Connection
>> Connection conn = getJDBCConnection();
>> In step two the sample creates and initializes a database driver object
>> for the target DBMS. This is
>> HSQLDB by default.
>>
>> // STEP 2: Choose a driver
>> DBDatabaseDriver driver = getDatabaseDriver(config.getDatabaseProvider());
>> Then in step three the database object is opened using the driver. Only
>> when opened, other methods
>> of the database object may be used. Finally we check whether or not our
>> database objects exist.
>>
>> // STEP 3: Open Database and check if tables exist
>> db.open(driver, conn);
>> databaseExists(conn);
>> In order to check existence of the database the sample simply performs a
>> query on the Departments
>> table ("select count(*) from DEPARTMENTS") using the following code:
>>
>> DBCommand cmd = db.createCommand();
>> cmd.select(db.DEPARTMENTS.count());
>> db.querySingleInt(cmd.getSelect(), -1, conn);
>> ###
>>
>> ... should/can be the conn and db initialised using some singleton pattern
>> and used within the
>> application in this way?
>>
>> Thanks for info,
>> David
> 


Re: How to use it with web application?

Posted by Rainer Döbele <do...@esteam.de>.
Hi David,

in a web-application you should ideally work with connection pooling, which is usually done by the application server (i.e. Apache-Tomcat).

We recommend the following:

1. create an Application object which is instantiated and initialized once when the web-application is loaded in the web application server. This can be done via a custom filter class registered in the web.xml definition file of your web-application. The application object is a singleton.

2. use a JNDI context to access a datasource provided by the application server. When initializing your application obtain a Connection from the data source and initialize your DB-Objects (create the driver, open the database, check the schema, etc.)

3. for each request, obtain a connection from the pool which you can then pass to the appropriate Empire-db function. When the request finished close the connection which will effectively return it to the pool.

I recommend downloading and looking at our Empire-Struts2 Example Application called DBWebSample provided with the distribution of the empire-struts2-extentions. Even if you don't intend to use Struts2 as your web application framework it gives a good idea on how to use Empire-db in a web application. You should particularly look the init() method of the SampleApplication class. However the sample application does not implement connection pooling - but it can be easily added.

Hope this is the kind of answer you expected.

Regards
Rainer

David Marko wrote:
> 
> Hello,
> can anyone share some common scenario on how to initialise necessary
> things in web application using
> e.g. SpringFramework?
> 
> ##### Using the code from the EmpireDB Site:
> config.init((args.length > 0 ? args[0] : "config.xml"));
> // STEP 1: Get a JDBC Connection
> Connection conn = getJDBCConnection();
> In step two the sample creates and initializes a database driver object
> for the target DBMS. This is
> HSQLDB by default.
> 
> // STEP 2: Choose a driver
> DBDatabaseDriver driver = getDatabaseDriver(config.getDatabaseProvider());
> Then in step three the database object is opened using the driver. Only
> when opened, other methods
> of the database object may be used. Finally we check whether or not our
> database objects exist.
> 
> // STEP 3: Open Database and check if tables exist
> db.open(driver, conn);
> databaseExists(conn);
> In order to check existence of the database the sample simply performs a
> query on the Departments
> table ("select count(*) from DEPARTMENTS") using the following code:
> 
> DBCommand cmd = db.createCommand();
> cmd.select(db.DEPARTMENTS.count());
> db.querySingleInt(cmd.getSelect(), -1, conn);
> ###
> 
> ... should/can be the conn and db initialised using some singleton pattern
> and used within the
> application in this way?
> 
> Thanks for info,
> David