Mikael Månsson

Greenhorn
+ Follow
since Sep 20, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mikael Månsson

Thanks, good stuff, so according to the best pracitices I do not really need store procedures, prepared statements are good enough for my application. However, on all examples of preparedstatements they are always prepared and closed just before and after the magic happens. Wouldn't it be better to prepare the statement once when the class is loaded, and then reuse this statement all over without closing it?

Also, it didn't help me answering whether I should keep my database connection open all the time or close it every time I have done something with the database. I ahve only one database client and it needs to enter information continuesly.
Hi!

I am going to use java and mysql for my application, but I'm not really sure how to connect for the best performance.

A short background to what the application will do: capture ip packages and insert extracted data into a database with up to 100 packets per second peak time. there might be the need of some ip -> int, mac -> long conversions when inserting..if that is more effective than storing just strings. Basically one package would consist of timestamp,ip,mac1,mac2.

So..I have been thinking, since I would use a consumer / producer pattern, with one queue, and only one thread doing database inserts I was thinking that no connection pooling would help..I would just open a connection once and keep it open, since i am capturing and inserting live. Also..since I would be using some conversions maybe stored procedures would be the best...and then just JDBC, and no extra layer above it.

Does it sound reasonable using JDBC (and mysql myisam) using stored procedures for this kind of application? Would it be better to use batch updates instead of stored procedures and take 1000 at a time or just do on the fly with stored procedures?

Thankful for all advices!
Thank you for your reply Roel.

When I think about it, I could actually remove the controller class and purely use my client interface as controller...just that the naming becomes different. I still want to see it as a client though. Client can be of two types, so I'd like to keep it that way.

For the server, I have a mainwindow and a reference to the server, so from the view I start the server directly. This is indeed not pure mvc, correct?
Thank you Andy, but I'm not sure I'm following completely.

If I see my application as follows then:

The GUI is the view. The controller and client is the controller, and the business interface is the model.
The business model knows nothing about the GUI and i can easily create a web ui using the same model.

This is not following the mvc pattern in what ways?

About my jtable i was thinking that my customized jtablemodel was kind of a model for the table (view) and the event handler for the table model were somehow making its own little mvc pattern within the gui.
Hi!

I have seen my solution as using MVC, but I'm getting confused of what parts correspond to what.

I have a Window, ClientWindow and ServerWindow that corresponds to "view". They have event listeners, and the ClientWindow has a JTable.

The ClientWindow has a Controller that has a reference to Client, which can be either a remote or a direct client. (The client actually has the same methods, but are invoking on a RemoteDatabase or Database object.)

The model in my case here i would want to see as the Client, since its the object getting all the data.

But I also have JTable containing data...which could be seen as a model for the ClientWindow..so do I have two separate mvcs?

Also for the ServerWindow there is only the Server, and I have skipped a controller for it...and let the ServerWindow invoke directly on it. This is no longer MVC, but a modified one, and it should be ok as long as i justify it in choices.txt, right?
Hi

I am using the standard java logger, and i'm wondering about stack traces.

Normally, not using a logger, i would propagate all lower level exceptions as higher level exceptions up to the last place, the gui, and there I would present the user with an understandable error message. At the same time, for debugging, i would want to call the printstacktrace method on the exception so that I can see in what file and what line the exception occured.

Now, when I am using the logger, I would want to log both the message from the error and the stack trace for the same kind of tracability, but i don't find any simple way of doing this, is this not a common thing to do?

Hi

I agree that the simplest (but not most effective) solution is to make sure that the data access methods are synchronized on the same object, and doing that is easiest by ensuring that the data class is a singleton.

I have synchronized all methods in my data singleton class except for two, lock and unlock, because concurrent work simply did not work when update and lock (for different clients) were synchronized on the same object.

The data class methods lock and unlock use an underlying class LockManager (holding the lock map), and I chose then to synchronize the methods lock and unlock in LockManager instead of in Data. So locking will synchronize on another object.

Now I'm wondering...will this really be safe? ..since the LockManager class is not a singleton. I believe it should, since the only reference to LockManager is inside Data, which is a singleton. The LockManager object is initialized only once, when Data is initialized.

Does this approach seem ok?
Thank you for all your help. I think I've cleaned it up just a little.
Hi Roel, thanks for you blazing fast answer

I see that you are into using only one client for both. I also want to keep it that way.

You had to make both businesservices throw remoteexceptions..even the local one, and your client has to catch them, even though you will never get one from the localbusinessservice. I guess there is no way out of this, if you want to keep your client unaware of what type it is.

May I ask..did you wrap these RemoteExceptions in your client to some other expections and propagated them up to the GUI, or did you handle them in your Client?
Hi!

I am working on the B&S assignment and I thought that I had made a clean design...until i started to change things..now I think I need to consult on what is a recommended java approach to the design.

I have a Database interface that is the interface to the database. There are 4 methods.

book
findByName
findByCity
listAll

I have a RemoteDatabase inteface that is the interface to the database using a remote client. It has exactly the same signature as Database, except for that all methods throw RemoteException.

book throws RemoteException
findByName throws RemoteException
findByCity throws RemoteException
listAll throws RemoteException

Then I have a Client interface that is the common client interface for the Controller to use (my model in mvc). Implementing classes of Client are RemoteClient and DirectClient, and client has also the exact signature as Database has. This is good, because the controller only needs to know that its working with a Client, but can be unaware of that it is a Direct or Remote client.

Now... what I earlier did is that the RemoteClient caught the RemoteExceptions and displayed an error to the customer, and did not propagegate them further, since that would break the Client interface (with expections). Then I figured... I think it is more correct to propage these errors all the way to the GUI, wrap them as ControllerExceptions in the Controller and catch and show in the GUI itself.

So... then I need the RemoteClient to actually follow the RemoteDatabase interface exactly...but the DirectClient to follow the Database (without the remoteexceptions). Now Client can no longer be an interface to these two...and I cannot use it nicely in my Controller class anymore.

Also I seem to have so many interfaces and classes that have the same setup of methods...

Client, RemoteClient, DirectClient, Database, RemoteDatabase, DatabaseImpl, RemoteDatabaseImpl.

Maybe I'm not completely off, but I feel a bit like i'm painted in a corner here.

What would be a proper java way of using the interfaces and classes?
Thank you for your fast reply Stas. I think that does the trick. Cheers.
14 years ago
Hi

I have a subclass of a JDialog, MyJDialog and I have made inner classes that takes care of Actions.
The question is, how can I call on the MyJDialog's inherited methods from JDialog from within the Action handlers?

Example:

14 years ago
Thank you for your reply, Roel.

So it works as I suspected, as one thread enters a synchronized method no other thread can enter any synchronized methods locked on the same object.

Then that is why I wonder how it can be possible with this impl without getting a deadlock?

Thread a aquires the lock for record 1 (the lockrecord method is synchronized on the Data object)
thread b tries to aquire the lock for record 1, but it bubbles down to the lockmanager's wait() call. Now thread b is waiting for record 1 to be unlocked, and is actually waiting within the synchronized Data method lockrecord.
Now thread a cannot enter updaterecord(1) because this method is synchronized on data.. Deadlock.
Hi all!

Not it's time for me to walk the path of the SCJD.

I have been reading up a lot, and I have seen that very many have chosen implemented the Data access class (implementing Sun's interface) as a singleton object (to ensure that only one instance of the file handling code exists). At the same time, I have seen that is a common solution to synchronize all the data access methods (methods in the singleton Data).

A synchronized method is the same as a method with an internal synchronized(this) block, and that means that all executing threads will gain mutual exclusion based on the Data access object....which is a singleton object.

Doesn't this mean that if any thread is in the synchronized method A (in Data) then no other thread can enter any other synchronized method in Data, since they are locking on the same object? So regardless of they being in different synchronized blocks, they will still block each other?

What I'm getting at is this:

How could it be possible with a Data class being singleton and having all methods synchronized to be accessed in the following way?:

Thread 1 runs dataref.lock(1);
Thread 2 runs dataref.lock(2); // will cause T 2 to wait(), and will stay in a synchronized data method until notified.
Thread 1 runs dataref.update(1, obj); // Thread 1 will now be blocked because T 2 has aquired a thread lock of the same data object?
Ok, but for the submission to the SCJP, where you deliver a jar file, what is then the preferred way?

Lets say I have the following structure:



Say that the remote object and server is in remote and the rmi client is in local. I will use the same jar on both machines but with different startup options. How do i specify the codebase when I start the application? Can it be omitted, since the class files are available locally anyway?
14 years ago