Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Synchronized is implementation detail

JavaFAQ Home » General Java Go to all tips in General Java


Bookmark and Share Although the synchronized keyword can appear in a method header, it
does not form a part of a method's API or contract. Synchronization is
part of the implementation, not part of the interface.

Note: some early Sun do*****entation (javadoc) included the synchronized
keyword in the description of methods. This error was later corrected.

Example

package myapp.data;

import myapp.business.Device;

/**
* The business layer talks to the data layer about storage of Device objects
* through a DeviceDAO reference.
*
* DataAccessException is a wrapper class, which exists only to wrap
* low-level exceptions specific to each storage mechanism (for example,
* SQLException and IOException). When an implementation class throws
* an exception, it is caught, wrapped in a DataAccessException, and then
* rethrown. This protects the business layer from ripple effects caused by
* changes to the datastore implementation.
*/

public interface DeviceDAO {
Device fetch( String aId ) throws DataAccessException;
void add( Device aDevice ) throws DataAccessException;
void change( Device aDevice ) throws DataAccessException;
void delete( Device aDevice ) throws DataAccessException;
}




The following valid implementation of DeviceDAO has some methods
declared as synchronized, even though the interface has nothing to say
regarding synchronization.

package myapp.data;

import myapp.business.Device;
import java.net.InetAddress;
import javax.servlet.ServletConfig;

/**
* An implementation of DeviceDAO which is specific to a MySql database.
*
* This class must be package-private, to ensure that the business layer
* remains unaware of its existence.
*
* Any or all of these methods can be declared as synchronized. It all depends
* on the details of your implementation.
*
* Note that it is often possible to use properties files (or ResourceBundles) to
* keep SQL out of compiled code, which is often advantageous.
*/

final class DeviceDAOMySql implements DeviceDAO {

DeviceDAOMySql( ServletConfig aConfig ) {
//..elided
}

public Device fetch( String aId ) throws DataAccessException {
//create a SELECT using aId, fetch a ResultSet, and parse it into a Device
return null; //toy implementation
}

synchronized public void add( Device aDevice ) throws DataAccessException{
//parse aDevice into its elements, create an INSERT statement
}

synchronized public void change( Device aDevice ) throws DataAccessException{
//parse aDevice into its elements, create an UPDATE statement
}

synchronized public void delete( Device aDevice ) throws DataAccessException {
//extract the Id from aDevice, create a DELETE statement
}
}
republished from "Collected Java Practices" site (license)




 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!

Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy