TimerTask and database

hi all,

i want to develop an java application which use timer to get records from 2 different data bases and do actions on them.

i can do this easily but i am little performance concious as i always create connection and as timertask ends i close it.

Now i want that if someone suggest me some good design pattern to handle this kind of things.

1- i am using 2 different db's

so any good design pattern for java desktop application with good database manipulation will be helpful for me.

regards

[540 byte] By [emmi@javaa] at [2007-10-3 3:02:06]
# 1
How often are you running the task?
dcmintera at 2007-7-14 20:51:50 > top of Java-index,Java Essentials,Java Programming...
# 2
Well if you're running this task every 3 seconds I'd go a different approach.spin off some new threads loop in the threads and sleep for a desired amount of time. This way you can keep the connection open in your thread.
Norweeda at 2007-7-14 20:51:50 > top of Java-index,Java Essentials,Java Programming...
# 3
hi norweed,i run my task after every 5 seconds,please elaborate your design so that i can optimal use of 2 databases in my application.thanks & regardsimran
emmi@javaa at 2007-7-14 20:51:50 > top of Java-index,Java Essentials,Java Programming...
# 4
hi all,i read sme article but didnt get good idea how i handle 2 databases in my application for optimal transaction of DB.regards
emmi@javaa at 2007-7-14 20:51:50 > top of Java-index,Java Essentials,Java Programming...
# 5

> i run my task after every 5 seconds,

You could retain a pair of connections to your respective databases and keep them open.

You could either do this "manually" by assigning them to a static class member when you kick off the task, or you could use a connection pool and limit the pool size (this will allow you to retain your original code pretty much as-is - you'll keep on "close"-ing the connection, but the pool will actually keep it open).

I agree that every 5 seconds sounds like it might result in a performance impact, and my inclination would be to fix it - BUT don't waste time optimising this unless you're actually experiencing a performance impact, or unless you genuinely don't have anything better to do.

dcmintera at 2007-7-14 20:51:50 > top of Java-index,Java Essentials,Java Programming...
# 6

> for optimal transaction of DB.

Unless you're doing two phase commit (XA), you're not going to be doing transactions across the databases anyway. Besides, it's often overkill.

Without knowing what you're trying to achieve with this architecture it's impossible to advise you on "optimal" approaches.

dcmintera at 2007-7-14 20:51:50 > top of Java-index,Java Essentials,Java Programming...
# 7
hi,i can share my code with u, ifu can look on it , ita nt a big code just a couple of classes.regards
emmi@javaa at 2007-7-14 20:51:50 > top of Java-index,Java Essentials,Java Programming...
# 8
No thanks.You either have a performance problem or not. If you do, what are the specifics of that problem please?If you don't have a performance problem, why are you trying to solve a non-existant problem?
dcmintera at 2007-7-14 20:51:50 > top of Java-index,Java Essentials,Java Programming...
# 9

hi dc,

infact i read design pattern but cant evaluate which will be better for me,

my problem is that i am not satisfied from my solution which is nt true OO i just append classes/functions and work with them.

i want to discuss my code wid some one for good design, plz help me in this regards.

where frm i can get help.

realy i m searching lot and cant cme to solutions.

emmi@javaa at 2007-7-14 20:51:50 > top of Java-index,Java Essentials,Java Programming...
# 10

Design patterns are not the be-all and end-all of good programming. Nor is object orientation.

If you have a pair of simple classes that are doing what you need them to do and aren't exhibiting any performance problems then you'd be an idiot to re-design them unless you absolutely needed to.

If as a purely learning exercise you want to post your code and get feedback about how others might tackle the same problem in an alternative fashion, then fine go for it (please use the [code] tags) - but don't assume that because you're not following any particular pattern or because it's not OO that it's "wrong" in some objective sense.

If you are going to post your code, I'd also suggest that you explain what you're trying to achieve - for example the best programmer in the world can't critique a program that copies one table to another table based purely upon the source code if what it's supposed to be doing is the same thing in the reverse direction!

dcmintera at 2007-7-14 20:51:50 > top of Java-index,Java Essentials,Java Programming...