Database connection failures that are OK

26 views
Skip to first unread message

Thomas Hallgren

unread,
Mar 26, 2012, 11:46:31 AM3/26/12
to web4j-users
Hi,

I'm building an app that uses several DataSources. It works well but
there's one thing that could be improved. Some of the data sources are
only mandatory when the user visits certain pages. Some will never be
accessed unless the user has a specific role.

How do I control this behavior? At present, the
DbConfig.initDataLayer() requires that all databases returned by the
ConnectionSource.getDatabaseNames() are accessible on start-up.

John O'Hanley

unread,
Mar 26, 2012, 5:58:23 PM3/26/12
to web4j...@googlegroups.com
Hi Thomas,

I'm a bit confused about this question. The database initialization code runs at startup only. This set up is 'global', in the sense that it is application-wide.

In what sense is the data base connection config dependent on the roles and operations applicable to a user? Please explain further.

Database connections themselves are very ephemeral. When the web4j data layer needs a connection, it uses ConnectionSource to get one. When it's finished with the db operation, it closes the connection (returning it to the connection pool).

If the initialization code related to the db is running for every request/operation, then something is definitely amiss.

 - John

Thomas Hallgren

unread,
Mar 27, 2012, 2:13:25 AM3/27/12
to web4j...@googlegroups.com
Hi John,

I'm building an "information collector" for an organization that has a lot of databases that are used by other systems.
My task is to provide easy retrieval of cross-system data without adding code to those systems. Hence the need for
several DataSource declarations.

One problem is that it's not guaranteed that all databases have 100% up-time. I want my app to start without problems
even if one or several databases are unavailable at that time. It's not unlikely that they will become available later
on. I would like the fact that a database is unavailable to become apparent when the user selects a page where it will
be needed, not before.

Not all users have access to all systems, so the menu of available queries will vary depending on who you are. This is
significant since the plan is to make the app available on several servers where not all servers have access to all
databases. I can work around that by using different configurations of course, but it would be great if I didn't have to.

- thomas

John O'Hanley

unread,
Mar 27, 2012, 10:00:24 AM3/27/12
to web4j...@googlegroups.com
I see.

This use case is a bit unusual, and was never part of web4j's design. It seems most likely that you will need to provide a custom Controller class, which allows the behaviour you're seeking. Although I have never implemented the behaviour you're looking for, it should be possible, but you'll have to provide a custom Controller.

The current Controller makes the assumption that if any of the app's databases is down, then the app should not process requests normally. See, for example:
  • Controller.init() - towards the end
  • Controller.tryDatabaseInitAndStartupTasks()
  • Controller.ensureDatabasesOK()
  • Controller.fDbStartupSuccess

The Controller source is here.

If I understand you correctly, the desired behavior is something like
  • upon startup, don't freak out if any of the databases are down
  • for each request, Actions would first test to see if the target db's needed by the Action are up; if not, the Action shows an error message.

If your connection pool misbehaves when the database is down, one option is to not use  connection pools. These days, it may be the case that a 'straight connection' may be perfectly fine with regards to performance.

Does this help? Am I making sense?

- John

Thomas Hallgren

unread,
Mar 27, 2012, 10:29:19 AM3/27/12
to web4j...@googlegroups.com
Very helpful indeed. Thanks!

- thomas

Reply all
Reply to author
Forward
0 new messages