|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Java FAQ Ask Doctor Java
Ask Doctor Java
By: James McGovern
Oct. 1, 2001 12:00 AM
Unlike the doctor that works for your HMO, Doctor Java is here to help you with all of your Java problems. The good Doc will not require a copayment for each visit nor ask you to fill out long arduous forms. The Doctor is here to help the readers of Java Developer's Journal find a cure for their Java system ills.
How can I synchronize method access across two different classes?
This, however, does not work across objects. The best way to solve this problem is not to declare your methods as synchronized but to use a synchronized block. First, we'll look at typical code that does not lock across methods:
public synchronized void Let's consider doing something like this:
public void increasePaycheck(int percent) { This would be considered a noble effort but unfortunately it does not do the trick. The main problem with this is that you are still trying to acquire a lock on this. What we really need is an alternative object to lock. Let's restructure our code and see how this can be realized.
public HRObject { The preceding code example is the prescription for your woes. We basically used an "impartial" object as the basis for acquiring a lock. This approach requires that you always use the same lock object and methods similar in their approach to synchronization. We could have alternatively created a static method in a class that is synchronized and have this method make all the other method calls on your behalf.
I am building a JSP/JDBC site with an ISP that never allows a dsn
type connection. The database I want to use is access. How do I do a
dsn-less connection?
[ODBC] To talk to an ODBC data source you will need to utilize a JDBC to ODBC bridge. Sun recommends using the bridge in situations where no native JDBC driver exists. Merant is a company that provides JDBC drivers that can connect to many disparate databases. To utilize the bridge, your code would look similar to:
try { The ODBC connection strings may vary in syntax depending upon the actual database and language you are connecting to.
I would like to see all of the elements in the request header in my
JSP for debugging purposes. Could you point me in the right direction?
<html> The doctor assumes the reason you have the need for such code is because you are having a problem related to types not being set properly on your Web server.
How do I delete a
cookie within a
JSP page?
Cookie killCookie = new Cookie("uid", null); The code removes a cookie from the browser once the browser is closed. This approach does not work in all instances. Many browsers keep it in memory and will not actually destroy the cookie until the browser instance is shut down. Let's take it one step further. In some scenarios, it may be useful to have the cookie destroyed without shutting down the browser. To realize this goal, you need to change your code slightly as follows:
Cookie killCookie = new Cookie("uid", null); This should do the trick nicely.
When should I use the singleton pattern in my java application?
Singletons can be problematic, like the girlfriend in high school They're typically implemented with synchronized blocks. Many threads will fight to obtain a lock on your Singleton causing a big performance bottleneck. The Singleton pattern can be problematic in a cluster as you will want more than one instance. The other problem with this pattern is that you need to make sure that its access is completely hidden from the outside world, or else it is essentially a big fat global variable. The other thought I have is that it's always easier to make something a Singleton than to unmake something a Singleton. In my mind, this is a red flag that needs serious architectural thinking.
Letters to Doctor Java: LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK SPONSORED BY INFRAGISTICS
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||