wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Data.close() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Data.close()" Watch "Data.close()" New topic
Author

Data.close()

Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
I want to shutdown the server from a server UI. The first step in the process would be to call: data.close(). My question is that the method, close() is not in the DBAccess interface. So how can I add the public close method? I have looked at previous threads which have said they either put it in an interface or made it public static. Is this acceptable for the B&S project or the other newer project versions?

Thanks very much.


-Paula
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
Would it be adviseable to include the following code in the server shutdown:

data = null;

and let the finalize method of data handle the closing of the file?

Thanks.

-Paula
dennis du
Ranch Hand

Joined: Dec 31, 2004
Posts: 59
-------------------------------------
My question is that the method, close() is not in the DBAccess interface. So how can I add the public close method?
-------------------------------------

In my assigment,the DBInterface is named (DB).
I think I can't work just under this interface, So I added some other methods to the Data.java for using.

So if you want close(),just add it to the Data.java class.


-------------------------------<br />OCP 9i <br />SCJP/SCJD/SCWCD(92%)/SCDJWS<br />XML(IBM Test 141)<br />Who is the next?SCBCD<br />SCBCD/CCNA/CCNP/PMP<br />not sure
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
Hi Dennis,

Thanks for the email. I have just changed my code to add a public static method in my Data class called, close(). This should be legal. I made this change because it impacts my code the least amount. Thanks again.

-Paula
Charles Fung
Greenhorn

Joined: Jan 05, 2005
Posts: 13
Paula Decker:
I have just changed my code to add a public static method in my Data class called, close().

How does this work?
I am afraid it is not a good design to use a static method in class Data to control instances of Data.
Also, the assignment defines an interface DB, so the server should rather reference DB interface to hide the implementation, than reference directly the class Data.
What do you think?


SCJD 1.4<br />SCWCD
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
Hi Charles,

Thanks for the post. My Data class implements DBAccess and uses a singleton for the database access, so its like a singleton itself. That is why I think a static is ok. I call Data.close() from my gui controller to close the database if I'm in standalone mode. I call Data.close() from the server.close() method. Both the standalone client and the server use an instance of Data to make all calls to the database. I don't really like using the static but it should work. I was wondering if I should add it because close() is not in the DBAccess interface. What do you think? Normally, I would just add another method, close() to the interface and be done with it.

Thanks.

-Paula
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Paula Decker:
Hi Charles,

Thanks for the post. My Data class implements DBAccess and uses a singleton for the database access, so its like a singleton itself. That is why I think a static is ok. I call Data.close() from my gui controller to close the database if I'm in standalone mode. I call Data.close() from the server.close() method. Both the standalone client and the server use an instance of Data to make all calls to the database. I don't really like using the static but it should work. I was wondering if I should add it because close() is not in the DBAccess interface. What do you think? Normally, I would just add another method, close() to the interface and be done with it.

Thanks.

-Paula


This question is actually about "session" control. Without sessions you don't really need to close Data objects except at program exit where you could use finalize.

I added a second interface that I call Session to the Data class. The Session interface includes connect, and disconnect methods. My Data and DataProxy classes both implement DBAccess and Session, these act as a Facade to the data access layer which consists of a singleton LockManager and DataBaseTable class.

The LockManager requires an instance of Session so that it can handle orphan locks when clients crash. I use a factory to provide instances of DataAdapter to clients, so there is always a single Session instance per client. The factory is the only object registered in the rmi registry.
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
Hi Peter,

Thanks for the input. I am only calling Data.close() when the server or standalone client applications are closed. I tried closing the file from Data during its finalize method. I don't believe the finalize method got called. I wasn't sure if this was a reliable approach.

What are the thoughts about Data.finalize() ?? Is it reliable?

I am not implementing the notion of a client session. The reason for this is that a user login was not required. I handle a record lock which was left by a crashed client by using: wait(<10 minutes> ; After the 10 minutes, the user receives a message that the record is still locked, and to please inform an administrator.

Thanks.

-Paula
Charles Fung
Greenhorn

Joined: Jan 05, 2005
Posts: 13
Originally posted by Paula Decker:
I call Data.close() from my gui controller to close the database if I'm in standalone mode. I call Data.close() from the server.close() method. Both the standalone client and the server use an instance of Data to make all calls to the database. I don't really like using the static but it should work. I was wondering if I should add it because close() is not in the DBAccess interface. What do you think?

I would use the following design:
(-> for reference; => for method call)
server->FileManager->file handle
server->DB (instantiate Data from FileManager)
FileManager has a constructor using the data file path, and provides access to the underlying data file.
server.close()
=>shuts down server so no more client request will be handled
=>FileManager.close()=>close file and release file handle
The GUI controller follows similar pattern.
This allows use of DB interface instead of the Data class.
This would not need to depend on finalize() to close file. Finalize() is called on garbage collection. There is no guarantee VM calls finalize() on each and every object on VM shut down.
Note that server.close() should make sure no thread is writing to the data file while calling FileManager.close(). Otherwise, you may risk corrupting the data file.
Hope this helps.
Paula Decker
Ranch Hand

Joined: Dec 17, 2004
Posts: 36
Charles,

Thanks for the reply. My RandomAccessFile is wrapped pretty tightly by my Data class. I don't think I want to pull any of it out at this point. I guess I'll take my chances with Data.close(). Thanks for your valuable input about the finalize() method. I also read that it may not always be called. Thanks again.

-Paula
 
 
subject: Data.close()
 
Similar Threads
Urgent, please help, reading remote Data
why can't close the remote data?
Design Patterns in my design after refactoring the server side classes, advice needed.
method throws exception is NOT inside a try/catch block. jbuilder does not show error
Data.close() is it needed?