How to time out an application

Hi,I have written a Java Application which uses a MySql server on the net as Database.How do I time out the application if the user is inactive for over 3 mins. I want the application to go back to a state where the user has to log in again.Any help will be welcome.
[294 byte] By [indrajitguhaa] at [2007-11-26 14:30:21]
# 1
Is this a stand-alone application or a web-application?
cotton.ma at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 2
Stand alone application.
indrajitguhaa at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 3
> Stand alone application.Is this database you are connecting to on the internet?
cotton.ma at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 4

Yes. A MySql database.

Class.forName("com.mysql.jdbc.driver").newInstance();

con=DriverManager.getConnection("jdbc:mysql://"+hostname, user, password");

I am keeping this connection alive till the user logs out.

i.e. I am not reconnecting for each an every transaction the user is doing. I connect only once and use it for all purposes. SELECT, INSERT, DELETE, UPDATE.

indrajitguhaa at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 5
You are aware that it would be trivial for someone to get the connection information from your program and connect to the database themselves?
cotton.ma at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 6

So what are you suggesting.

I create a connection each and every time I have to do some work on the MySql server? Or you dont want me to use Application environment at all?

Cant do the second thing cause client wants it this way.

As for the first option-

I have created a private Connection con;

There are no getConnection method in my JFrame class.

In the JFrame class I instantiate a no of custom panels.

new ABCPanel(Connection)

All ABCPanels have private Connection con;

What else can I do?

indrajitguhaa at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 7

> So what are you suggesting.

>

> I create a connection each and every time I have to

> do some work on the MySql server?

No I am suggesting you have a serious security flaw.

in 10 minutes with a decompiler I can get access through your code to your database.

This is probably highly undesirable. It doesn't have much to do with your original question but I think you should be aware of just how badly open your database is to attacks.

It would be preferrable to have the application connect to a servlet which deals with the database for you.

cotton.ma at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 8

The first security flaw is that the database is hosted in a public shared server.

Second I dont have much leeway in terms of the architecture.

I have been contracted to build a Sales-CRM application for a company. They want desktop applications not web applications which was my first choice.

So leaving aside architecture choices I am still faced with the problem of how to time out applications? I am searching - hopefully I will find something or some kindly soul might show me the way.....

indrajitguhaa at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 9

> The first security flaw is that the database is

> hosted in a public shared server.

> Second I dont have much leeway in terms of the

> architecture.

> I have been contracted to build a Sales-CRM

> application for a company. They want desktop

> applications not web applications which was my first

> choice.

>

> So leaving aside architecture choices I am still

> faced with the problem of how to time out

> applications? I am searching - hopefully I will find

> something or some kindly soul might show me the

> way.....

Is it possible for you to stop whining?

Timing out the user is a frivolous activity and I haven't told you that you can't have an application. I am telling you you MUST add a tier to interface from the client to the database server.

This tier can also deal with your timeout issue relatively easily.

I didn't say don't do a desktop application. I just told you to do it correctly.

cotton.ma at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 10
I would have a seperate thread running in the background that sleeps for 3 minutes then disconnencts. When the user if does anything, kill the thread and the re-initialize it so it starts counting from the top again.Message was edited by: AFlood
AFlooda at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 11

> I would have a seperate thread running in the

> background that sleeps for 3 minutes then

> disconnencts. When the user if does anything, kill

> the thread and the re-initialize it so it starts

> counting from the top again.

>

if this approach is taken I would do it slightly differently.

I would set a thread in a timer that goes off once every three minutes and checks a boolean flag to see if anything happened. Then it resets the flag. If anything happens then it sets the flag to true.

I say this because flipping a flag is going to be more workable from a performance perspective then constantly stopping and starting a thread which I think would pretty much freeze up the system quickly.

cotton.ma at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 12
Thanks , will try this approach.
indrajitguhaa at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 13
Just use a Swing Timer that fires in 3 minutes. Every time activitiy occurs restart the Timer so you have a full 3 minutes again before the Timer fires.
camickra at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 14
> Just use a Swing Timer that fires in 3 minutes. Every> time activitiy occurs restart the Timer so you have a> full 3 minutes again before the Timer fires.Couldn't that be awfully expensive though depending on what "activity" means I suppose.
cotton.ma at 2007-7-8 2:25:02 > top of Java-index,Java Essentials,Java Programming...
# 15

> Couldn't that be awfully expensive though depending on what "activity" means I suppose.

I don't know what the overhead is to restart a Timer. I suspect it isn't much though. It will at least guarantee a 3 minute timeout.

Using the boolean approach the timeout period could be up to 6 minutes long. Assume you start the Timer and a couple of seconds later an activity occurs. Then the boolean is set. After 3 minutes the Timer fires, activity has occured so the boolean is reset and the Timer is set to fire in another 3 minutes. So the earliest the timeout can occur is when the Timer next fires.

camickra at 2007-7-21 16:12:04 > top of Java-index,Java Essentials,Java Programming...