Synchronizing db access

In a servlet i have a method which accesses the db to calculate the current max value of a numeric id field (which is the primary key of the table). Then the same method tries to insert a new record with id = max + 1.
If two users have that method executed "at the same time", it happens that the primary key is violated by one of the two, because after the 1st user (session) has his max calculated and before his max+1 id is inserted into the table, the 2nd user calculates his max as well, and of course it comes to be equal to the max of the 1st user.
I'm having an hard time in understanding synchronization; i syncronized the code block to the instance of the servlet (synchronized (this) { but the violations of the PK still occur.
Should i synchronize on the ServletContext ((synchronized (this.getServletContext()) {)?

In the servlet where i must do the insertion of the new record with a new id, the servlet must then retrieve the new id just after the insertion, because the servlet itself needs the id of the new record for further processing.
It's true that i could make the db calculate the new id (which is also the primary key), using an autoincrementing data type for the field, or even just an INSERT SELECT MAX(ID) + 1 kind of statement, but if i do so, how do i then make the servlet aware of what the new id is ? That's the PK, so i wouldn't know how to SELECT it back after its creation (except by building some mechanism aimed solely at that and involving the creation of a new field, which i don't think it's how this quite generic problem is usually approached).

Similar Messages

  • Synchronized ConnectionPool access

    Is there a way to avoid ResourceExceptions when performing
    more requests for database connections from the pool than
    are currently available?
    Is there a kind of synchronized access which forces callers
    to wait until used connections are released?
    Regards
    Osman

    Larry Presswood wrote:
    The issue is that at least in 6.0 sp2 rp2 is that this resource exception is thrown
    even when there are enough DBMS connections.
    We have a situation where we have 75 connections set up and max
    in use at the time of the resource exception is around 7-10??
    There appears to be a bug in 6.0??Well, yes, as you describe it, it sounds like a bug, but there are many current and
    past customers of 6.0, and connection pools are probably the single most
    travelled code path in the J2EE APIs, so we might have heard of this sooner.
    An optimally crafted application can get away with one pool connection per execute
    thread. How many execute threads do you have, and on what sort of machine are
    you running? How many connections in the pool? Please describe how your code
    obtains pool connections, and how it closes them.
    thanks,
    Joe
    >
    >
    Joseph Weinstein wrote:
    osman vurgun wrote:
    Is there a way to avoid ResourceExceptions when performing
    more requests for database connections from the pool than
    are currently available?
    Is there a kind of synchronized access which forces callers
    to wait until used connections are released?
    Regards
    OsmanHi. This is a frequently asked question. Some day we'll put the answer in our
    docs. No, there's no way to allow a caller to wait for a pool connection,
    and from the system point of view there shouldn't be. Any caller waiting
    is tieing up one of the fixed number of execute threads in the server, which
    could otherwise be running another server task that has all it's resources
    to do it's work. Enough 'patient waiters' could freeze the server solid,
    especially if all the pool connections were being held by jobs on the server's
    run queue, but would never run to release these connections because
    all the execute threads were being held by waiters.
    It is the system's responsibility to quickly reject any excess work, above the
    amount it can handle, so it can continue to do work. Any system that permits
    itself to devote it's resources to non-productive work is open to denial-of-
    service attacks (inadvertant or intentional).
    The fundamental problem is too few resources (pool conections) for the
    work load, and the correct response is to make the pool bigger. Optimally
    designed applications only require the server to have one pool connection
    per execute thread.
    Lastly, the proper application response to a resource exception is not
    to do a tight loop retrying, at least not in serverside code. That accomplishes
    the same tieing up of an execute thread. The exception should go back to
    the external client for a retry from there.
    Joe

  • ABAP Proxy synchronous interface - access to payload

    Hi,
    I have a scenario from ABAP Proxy -> XI -> Web Service.
    On occassion, there is an issue with calling the Web Service (unavailable for instance) and an exception is returned to the ABAP Proxy for a SystemError.
    I am able to catch this exception in the proxy and output the error - for example HTTP.Exception.HTTP.Adapter.
    However, I also want to use information in the response payload to output on the error as the payload contain infrmation on why the error was thrown in the target application - for example - Format of data xxx is incorrect.
    I can get access to the request payload using interface/class IF_WSPROTOCOL_PAYLOAD=>GET_SENT_REQUEST_PAYLOAD but there does not seem to be an equivalent for GET_RECEIVED_RESPONSE_PAYLOAD !
    Have anyone done this before - use payload information in an ABAP proxy from the response payload ?
    Kind regards
    Colin.

    Hi,
    I will need the actual payload info not the status.
    I will have to debug SXI_MONITOR in CRM to see how it is obtained.
    Thanks
    Colin

  • [flash.filesystem.FileStream] Data Corruption in Synchronous Random Access Mode

    Performing a READ after WRITE corrupts the WRITE
    I consider this a serious bug wich should be fixed as soon as possible.
    Details can be found in the bugbase and at stackoverflow.
    I would like to ask here to vote the bug in the bugbase, so adobe might consider fixing it.
    tx leo

    Thank you for posting and reporting the bug.  I'd like to echo your request that others effected take a minute and leave a comment/vote for this issue.  I'll also ask the team to investigate.
    Thanks,
    Chris

  • Imlementing synchronized access to shared objects in LabVIEW OOP

    Many objects in object-oriented programming have an
    identity, such as a file, a front-panel object or a hardware device.
    These objects cannot be modelled using present LabVOOP (LabVIEW Object Oriented Programming) objects as
    LabVOOP objects gets copied as wire is branched; multiple different
    wires cannot all represent a single object. This issue has been
    irritating the community of LabVIEW users since the release of LabVOOP
    a few months ago.
    It seems that there is a huge demand for
    objects with unique identity i.e. by-reference objects in LabVIEW. The
    central problem why LabVOOP propably doen't have these objects is the
    difficulty in implementing synchronized access to these objects from
    multiple parallel threads. The problem of synchronized access can be
    divided into two different separate topics. First how the
    sychronization should be implemented in LabVIEW runtime engine. Second
    how this synchronization mechanism should be visible to the developer.
    I'd like to start this thread to discuss these two issues.
    Synhronization under the hood
    Traditionally
    people talk about locking of an object and about get-modify-set pass
    when accessing the object. Locking is traditionally done by acquiring a
    mutex for an object, modifying the object and releasing the mutex so
    that other threads can access the same object instance. This is how
    inter-thread synchronization is traditionally done. However, besides
    the mutex based locking, the computer science community has innovated
    also different kinds of methods on synchronizing the access to objects.
    One way to get object-level synchronization is modify the
    runtime engine so that it only allows a single method of a synchronized
    object to run at any time. This mechanism of syncrhonization is
    implemented in programming languages like O'Haskell, which is a Haskell
    variant with object orirented features.
    Also different
    transactional mechanisms[1,2] have been successful. In transactional
    mechanisms multiple threads are allowed to access a synchronized object
    simultaneously. As each method accessing an object commits their
    changes, they verify that no other object has modified the object
    simultaneously in a manner than would break the transaction. If such a
    modification has occurred, everything is rolled back. Transactional
    mechanism do not suit to every possible situation as not everything can
    be rolled back. For example it's hard to roll back an action that
    somehow modifies the physical world.
    User experience of synchronization
    How
    the synchronization is generally implemented in LabVIEW shouldn't be
    directly visible to the developer end-user. The developer should
    understand the general concepts of synchronization to take full
    advantage of it, but in general the synhronization mechanism should be
    integrated directly to development environment. There should in general
    be no need to acquire a mutex by calling acquire mutex node but instead
    the end-user should be able to specify which data needs synhronized
    access in more sophisticated way.
    In the following I propose a
    mechanism of integrating the synchronized access of by-ref objects to
    the development environemnt of LabVIEW. The proposal is very
    preliminary but I hope it breaks the ice and the community would start
    innovating in how should NI implement the syncrhonization support in
    the user interface of LabVIEW.
    Wire level synchronization
    Only
    methods can access object private data members. In synchronized access
    to the object, it's the methods accessing the private data members that
    need to be synchronized. The private data members are accessed by
    applying unbundle node to the class wire and data is written back to
    the object using bundle node.
    What I propose is the following.
    An unbundle node could either be normal or "synchronized". A
    synchronized unbundle would guarantee the access to the private data
    members in synchronized manner. All data wires originating from
    synchronized unbundle would be of synchronized type, in a little
    similar manner as a dynamic dispatch wire is of special dynamic
    dispatch type. Such a wire must evetually be connected to a bundle
    node. When the wire is bundled back to the originating object, the
    synchronization requirement is released.
    These synchronized
    wires would look somewhat different from normal wires so that the
    developer instantly knows that the wire is synchronized. The developer
    can branch the wire, but only one wire branch can own the synchronized
    type. The developer could easily select which wire would be
    syncrhonized by Ctrl+clicking the wire. Such a wire can be considered
    as a combination of a data and a mutex, even though mutexes don't need
    to be the underlying synchronization method. The wire just guarantees
    that there is a mechanism in the runtime engine that makes sure the
    access to the wire data is synchronized.
    There is a need to wire
    data originating from a non-synchronized wire to a synchronized wire so
    that it can replace the private data member of the class. This is
    accomplished with a new node similar to bundle node, that would allow
    replacing the data in a syncrhonized wire with some data originating
    from a non-synchronized wire.
    The synchronized wire can be
    connected to a front panel controls of special syncrhonized type. This
    way the synchronized wire can originate from a method and allow passing
    the synchronized data to the calling VI and back to another method.
    This is practical for example in a situation when the developer wants
    to run different analyzes to a data class but don't want to rewrite all
    the existing data analysis tools as class members. So the developers
    writes a syncrhonization acquiring getData method that let's the
    calling VI to access the syncrhonized data. Then the developer passes
    this data to an analysis VI and passes the result back to a setData
    method that writes the result back to the class wire.
    There
    will probably be technical problems in allowing the user to connect
    such a synchronized wire to all existing VIs since these VIs. Therefore
    the programming model for all nodes that do not support such
    synchronized wires will be branching the wire and passing the
    non-synchronized wire branch to the node and then bundling the result
    back to the synchronized wire.
    To increase performance and
    decrease unnecessary buffer copies when a syncrhonized wire is
    branched, if the syncrhonized wire continues directly to the new bundle
    synchronized wire node, no buffer copy is made.
    Discussion
    The
    syncrhonized access to a by-ref LabVOOP objects can be implemented in
    multiple ways by National Instruments. The synchronized access should
    be divided to two different and independent parts: 1) the user
    experience of synchronization and 2) the runtime engine synchronization
    mechanisms. As LabVOOP objects have special properties compared to
    other LabVIEW data types, optimal user experience can be gained by
    designing the user experience specifically for LabVOOP objects. From
    user experience point-of-view this syncrhonization mechanism may not
    work for other data types. Separating object syncrhonization from
    synchronization of other data types is advantageous also for other
    reasons. Due to the fact that object data can only be accessed via
    object methods, more advanced synchronization methods may be used with
    objects than can be used with other data types. O'Haskell
    synchronization implementation is an example of this. Integrating the
    synchronization directly to the user interface allows NI to change the
    mehcanisms under the hood, when computer science comes up with more
    advanced methods. Therefore NI could begin with traditional and quite
    easy mutex-based synchronization and later move to more advanced
    perhaps transaction based syncrhonization methods or even combinations
    of multiple different methods.
    I hope this topic generates
    discussion that would help NI to implement an excellent synchronization
    mechanism in LabVOOP. I hope that all talented individuals in the
    community participate this discussion to help NI to reach this goal. I
    also hope that if you just have time, it would be great if you could
    surf the computer science resources to find out what kinds of new
    techniques there exists for synchronizing access to shared resources. A
    Large community may find much more innovative solutions than a few engineers at NI. Let's give NI the power of open source design
    Tomi Maila

    Hello Tomi,
    First, thank you for taking the time to write such a well
    though-out suggestion.  Are you familiar
    with the “LabVIEW Object-Oriented Programming: The Decisions Behind the Design”
    document?  I think the reason we chose to implement a ‘by
    value’ strategy, is that is more in line with the LabVIEW programming paradigm
    of dataflow, and would make sense to most of our LabVIEW users.
    I think your suggestion is interesting, and it does
    highlight the need to think outside of the conventional LabVIEW box and look to
    some of the innovative things other languages do.  However, I think we all agree that
    synchronization takes careful planning and extra work for the programmer.  Even with an ‘ideal’ solution I see no way
    around this.  For LabVIEW users today,
    one great way to get synchronized ‘by reference’ semantics with your objects is
    to use a single-element queue to pass your object.  The queue itself is passed ‘by reference’ and
    is inherently synchronized!  The does
    have the disadvantage of adding one more small layer of complexity to your
    program, but some complexity would have to be introduced in any situation.  The other disadvantage with this is that it
    is not always an intuitive way to implement your program and requires some
    amount of LabVIEW knowledge before one would generally come across this
    technique.
    In any case, I appreciate the time and effort you put in to
    your suggestion.  Please make sure that
    you submit the suggestion formally through the NI Product Suggestion Center so
    that it can be reviewed by some of the decision makers here.
    Thanks again,
    Travis M
    LabVIEW R&D
    National Instruments

  • PI SOAP access to third party Webservice,Return ERROR

    Hi Experts,
    I have one soap synchronous scenario access to third party WEBSERVICE,Return error.While testing the wsdl in soap ui, I am getting response, messages showing successfully processed. I think it is pi to send  xmlns:ns1='http://tempuri.org/'  on both sides should not be a single quotation mark, but I don't know how to adjust  PI set. how do you see.
    1、SOAP UI submitted to Webserver XML
    [2014-05-26 08:47:24.662] --- Recv data from SocketId=272 Socket=10880
    POST /MWGate/wmgw.asmx HTTP/1.1
    Accept-Encoding: gzip,deflate
    Content-Type: text/xml;charset=UTF-8
    SOAPAction: "http://tempuri.org/MongateCsSpSendSmsNew"
    User-Agent: Jakarta Commons-HttpClient/3.1
    Host: 10.0.0.253:8082
    Content-Length: 520
    <soapen:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
        <soapen:Header />
        <soapen:Body>
            <tem:MongateCsSpSendSmsNew>
                <tem:userId>DOA001</tem:userId>
                <tem:password>dennis</tem:password>
                <tem:pszMobis>1313773654</tem:pszMobis>
                <tem:pszMsg>1111</tem:pszMsg>
                <tem:iMobiCount>1</tem:iMobiCount>
                <tem:pszSubPort>*</tem:pszSubPort>
            </tem:MongateCsSpSendSmsNew>
        </soapen:Body>
    </soapen:Envelope>
    2、PI submitted to Webserver XML
    [2014-05-26 08:36:08.725] --- Recv data from SocketId=271 Socket=10704
    POST /MWGate/wmgw.asmx HTTP/1.0
    Accept: */*
    Host: 10.0.0.253:8082
    User-Agent: SAP-Messaging-com.sap.aii.af.sdk.xi/1.0505
    CallingType: SA
    content-id: <[email protected]>
    Content-Type: text/xml; charset=utf-8
    Content-Length: 417
    SOAPACTION: "http://tempuri.org/MongateCsSpSendSmsNew"
        <SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'>
            <SOAP:Header />
            <SOAP:Body>
                <ns1:MongateCsSpSendSmsNew xmlns:ns1='http://tempuri.org/'>
                    <ns1:userId>DOA001</ns1:userId>
                    <ns1:password>dennis</ns1:password>
                    <ns1:pszMobis>13637731567</ns1:pszMobis>
                    <ns1:pszMsg>Constant</ns1:pszMsg>
                    <ns1:iMobiCount>1</ns1:iMobiCount>
                    <ns1:pszSubPort>*</ns1:pszSubPort>
                </ns1:MongateCsSpSendSmsNew>
            </SOAP:Body>
        </SOAP:Envelope>
    3、SXI_MONITOR   Payloads content:
      <ns1:MongateCsSpSendSmsNew xmlns:ns1="http://tempuri.org/">
            <ns1:userId>DOA001</ns1:userId>
            <ns1:password>dennis</ns1:password>
            <ns1:pszMobis>13637731567</ns1:pszMobis>
            <ns1:pszMsg>Constant</ns1:pszMsg>
            <ns1:iMobiCount>1</ns1:iMobiCount>
            <ns1:pszSubPort>*</ns1:pszSubPort>
        </ns1:MongateCsSpSendSmsNew>
    4、PI channel

    Hi Nathan,
    Have you tried the SOAP HTTP Axis function.
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/b092777b-ee47-2a10-17b3-c5f59380957f?overridelayout=t…
    https://help.sap.com/saphelp_nw04/helpdata/en/45/a39e244b030063e10000000a11466f/content.htm
    Configuring the Receiver Axis SOAP Adapter (SAP Library - SAP Exchange Infrastructure)
    Regards,
    Jannus Botha

  • How Do I Sync All Email Accounts and Mailboxes (including those I create)?

    How do I Sync all email accounts in the OSX Mail Application and mailboxes(including those I have created), all using the .mac sync application . . . or is there a better way to sync information from my powerbook to my imac?
    The problem: I am reading and sorting/organizing my emails on my powerbook when traveling, and then I come home to my imac and end up having to manually duplicate the work I just completed. Any easy way of truly syncing the information between computers? Pleae let me know. Thanks
    What is meant by mailbox structures? For example: On my powerbook, I have 2 unread messages in one of my IMAP email accounts. I read both messages. I choose to move message A to a client mailbox that I created in the OSX Mail APPL and the other message B I leave as read in the inbox & respond with a sent email . Now I use the .mac sync app on the powerbook. Then I use the .mac sync app on my imac . . . . will the application not sync the same information/sorting/organizing/sent responces into the appropriate mailboxes? I'm so confused.
    Also, what "mailboxes behavior" & "advanced" preferences should be checked with IMAP email accounts. All sorts of misc. mailboxes keep being created in the OSX Mail APP. when choosing different preferences. When selecting store on server (mailboxes created indicate: stored locally on computer). Again very confused.

    The benefit of using an IMAP is keeping server stored messages synchronized when accessing the account with an email client such as the Mail.app on different Macs.
    Messages in the Inbox mailbox for an IMAP type account are stored on the server by default which cannot be turned off.
    With an IMAP type account, you can also store Drafts, Sent, Trash and Junk messages on the server which is available under the Mailbox Behaviors tab for the account preferences.
    If you want to keep Sent messages synchronized with Mail on both Macs, you need to select "Store sent messages on the server" under the Mailbox Behaviors tab for the account preferences in Mail on both Macs.
    I choose to move message A to a client mailbox that I created
    in the OSX Mail APPL and the other message B I leave as read in
    the inbox & respond with a sent email . Now I use the .mac
    sync app on the powerbook. Then I use the .mac sync app on
    my imac . . . . will the application not sync the same information
    /sorting/organizing/sent responces into the appropriate mailboxes?
    I'm so confused.
    Was this client mailbox created as an "On My Mac" location mailbox? If so, an "On My Mac" location mailbox is stored locally on the hard drive, not on the server so messages moved to such a mailbox will not be kept synchronized with Mail on both Macs. Transferring a message from the account's Inbox mailbox or from any server stored mailbox to an "On My Mac" location mailbox stores the transferred message locally on the hard drive and removes the message from the server at the same time.
    When creating a mailbox, the default location is "On My Mac" which is stored locally on the hard drive. If you are accessing an IMAP type account, you can select the IMAP account via the location selection which will create the mailbox on the server and be available in Mail on the other Mac. Messages placed in a server stored mailbox will be kept synchronized with Mail on both Macs.

  • Java synchronization mechanism allows two threads to hold the same lock?

    Dear all,
    I am a little bit puzzled since I cannot explain why I get the following behavior.
    I have a small Java program to draw components. The drawings are cached in the memory for performance improvements. The cache is access by two different threads. Now the funny thing: even I synchronized every access to the cache, I still get ConcurrentModificationExceptions.
    Here is a part of the code:
         @Override public void paint(Graphics g2) {
              // Check if the image is in the cache
              Image drawing;
              synchronized (m_Cache) { drawing = m_Cache.get(m_CurrentOffset); }
              if (drawing == null) {
                   // The image is not cached, so draw it
                   // Put the image into the cache
                   synchronized (m_Cache) { m_Cache.put(m_CurrentOffset, drawing); }
         public void componentResized(ComponentEvent e) {
              // Upon resizing of the component, adjust several pre-calculated values
              // And flush the cache
              synchronized (m_Cache) { m_Cache.clear(); }
         public void elementUpdated(IHexGridElement Element) {
              // Clear cache where the element may be contained at
              synchronized (m_Cache) { for (Point p : m_Cache.keySet()) { if (isElementInScope(Element.getIndex(), p, new Point(p.x + m_MaxColumn, p.y + m_MaxRow))) { m_Cache.remove(p); } } }
         }The paint and componentResized methods are invoked by the AWTEventQueue-0 thread. The elementUpdated method is invoked by "MyThread" thread.
    Now my question is why do I get java.util.ConcurrentModificationException, especially in situations where a lot of repaintings have to be done and the window is resized? When I remove the caching stuff, everything works perfect, but only a little bit slow.
    Thanks for any help.

    In your elementUpdated method, you are using an Iterator to walk over the set of keys in your map. You can't see the Iterator, because you are using the new for-each loop syntax which hides it from you - but it's there under the covers. You are then removing elements from your map directly. This is what causes your ConcurrentModificationException.
    You should re-write your loop to explicitly use an iterator, and then do your remove operation through the iterator.
    Incidentally, you have a slight race condition in your paint method. Two (or more) threads could simultaneously discover that a given image is not cached, then both draw it, then both cache it. If the drawing operation produces the same drawing every time, then the only problem this causes is the overhead of drawing the image multiple times. To fix this, wrap the get, draw and put operations in a single synchronized block.

  • Problems with java Thread

    I'm reading book "JAVA THREAD" published by OREILLY.
    And on fifth chapter, it gives an example.
    one Thread's two method:
    private boolean done = false;
    public void run()
    while(!done)
    foo();
    public void setDone()
    done = true;
    it says, the run method will be compiled as machine code:
    Begin method run
    load register r1 with memory location 0xff12345
    Label L1:
    Test if register r1 == 1
    If true branch to L2
    Call method foo
    Branch to L1
    Label L2:
    End method run
    setDone method will be compiled like:
    Begin method setDone
    Store 1 into memory location 0xff12345
    End method setDone
    And it says " because Run method will never reload 0xff12345 to register r1(in while loop), so setDone method will never lead to run stop.
    I'm so puzzled with this. I have test this code on windows platform, run method can stop after another Thread call setDone method !.
    but I think "JAVA THREAD" should have error on this, so why ?

    If the book says it will happen like that, then the book is wrong.
    I think what they meant--and what would be correct to say--is that that is an example of what could happen if you don't synchronize all access to the run variable.
    The point is this: Threads can have local copies of variables, that are separate from other threads' local copies and separate from the "master" copy. The spec doesn't define where those local copies live--the implementation can put them anywhere it wants--but the most natural and sensible thing would be to store the local copies in CPU registers, rather than in main mem.
    The example the book gave shows what might happen if that VM stores threads' local copies in registers. There's no guarantee that the problem they described will happen, but it could, so you have to guard against it.
    You guard against it by declaring that shared variable volatile, which requires that the threads use the master copy rather than their local copies, or by synchronizing every access to that thread. Syncing requires reading from the master copy on entering the sync block (or on first access) and writing out to the master copy upon leaving the sync block.

  • Thread safe object cache

    The case:
    I have a case where I hold configuration about a web page in an xml-file. The configuration is cached in memory on first request then I retrieve the configuration from memory on every following request. If the configuration in the xml-file changes between requests I will release the current configuration from memory and renew it by reading from the file.
    Since we are talking about http requests all this will happen in a heavy loaded multithreaded environment.
    The current solution:
    +public class ConfigFile {+
    private static final ConfigCache REGISTRY;
    +static {+
    REGISTRY = ModuleRegistry.create();
    +}+
    +public static Element getConfigElement(final String configKey) {+
    Element configElm = null;
    +if(configFileIsChanged()) {+
    REGISTRY.clear();
    +}+
    +if((configElm = REGISTRY.get(configKey)) == null) {+
    +... //Reload configuration from xml-file+
    REGISTRY.put(configKey, configElm);//and save to registry
    +}+
    return configElm;//Return config data as an xml-element
    +}+
    +... //Class impl..+
    +//This inner class holds the configuration as dom element parts+
    +private static class ConfigCache {+
    +private HashMap<String, Element> _moduleRegistry;+
    +public synchronized void put(final String key, final Element elm) {+
    _moduleRegistry.put(key, elm);+
    +}+
    +public synchronized Element get(final String module) {+
    +return _moduleRegistry.get(key);+
    +}+
    +public synchronized void clear() {+
    _moduleRegistry.clear();+
    +}+
    +public static ConfigCache create(){+
    return new ConfigCache();
    +}+
    +}+
    +}+
    Since the registry is declared as a class variable in the ConfigFile class I handle the put and get operations to it by synchronizing the access.
    The problem/question:
    Is if this is enough to handle possible threading issues when it comes to invalidating the registry. What happens if some thread tries to "get" from the registry when another just cleared it? I guess it needs to wait until it's reloaded, right?
    Regards
    Johannes

    I wasn't clear enough in my previous post. Sorry for that.
    The code will be executed in a servlet/JSP environment but it's not really relevant in this case except for the fact that there will be multiple threads accessing this code.
    Think of it as an in memory data storage with information that will be read from the memory. The memory in this case is implemented as a static HashMap (the ConfigCache). I've wrapped it in the ConfigCache class since I want to synchronize the access to the put and get. All threads have access to the configuration file through the static method call ConfigFile.getModuleElement(somElementName);. This method is not synchronized.
    Suppose we have 20 threads accessing this code concurrently. I'm not sure how the code will behave when this executes. My guess is that the critical parts are when one thread clears the HashMap, another tries to put something to the Map and another tries to get something.
    I made an assertation test where I expected different results from 5 randomly executed web requests with 20 active threads reading and writing to the Map (the ConfigCache). It succeeds but I'm still not sure if I'm on the right track.
    I've read a lot about this but I have not found any material describing this as clearly as I want it. In most cases they only explain how to use synchronized blocks and methods.
    Are you with me or do you still think this belong in the servlet forum? Maybe it does?
    BTW. How do I use code tags?

  • Synchronization on a static object

    If I have a code like:
    synchronized(_otherParsers)
    _otherParsers = new Vector();
    } knowing the _otherParsers is a static object in the class, the synchronization will block all access to the object or it will block the access to the sync. block?
    OBS: So when one thread enters the synchronized block, accesses to that object by other threads are blocked until the original thread exits the synchronized block. [PENDING: verify previous statement, and figure out if synch'ed blocks block out all method calls on the object or just synch'ed ones] (quote from Java Tutorial, http://java.sun.com/docs/books/tutorial/together/bingo/synchronized.html)

    IIRC, when using synchronize(someObject) then
    someObject is locked, meaning that the original object
    isn't locked. So other methods will execute correctly.let's say the following:import java.util.Vector;
    public class A {
      public static Vector v = null;
      public A() {
        v = new Vector();
      public static void main(String[] args)
         B b = new B();
         new Thread(b).start();
         new A();
    class B implements Runnable {
      public void run() {
        synchronized(A.v) {
            A.v = new Vector();
            A.v.add("test");
    }As you can clearly see, the static vector, member of A class is initialized twice (once by the new A() in the main method of A class and once by A.v = new Vector() in the run() method of B class). Assuming the thread enters the synchronized block before the call to new A() in the main method, when that call ocures, is the A.v object locked? or not?

  • Strange PriorityQueue behaviour

    Hey all,
    Im observring somr rather peculiar behaviour with a PriorityQueue. Essentially I make a check that PriorityQueue pq has some elements in it i.e.
    if(pq.size()>0){
    // Then, I try and access (pop) the element at the head of the queue:
    element = pq.poll();
    ...etc
    However sometime, when it gets to pq.poll() in the above code, an exception is raised. NB, I has even tried if(pq.peek()!=null), yet occasionally, a null pointer exception is raised. Has anyone else noticed this very strange behaviour, and if so, what on earth could be hapenning?
    Cheers
    Ben.

    My understanding is that the threadsafe aspect of PriorityBlockingQueue ensures that the internal data will not be currupted when multiple threads are accessing it. This will not, however, prevent one thread from removing elements while another thread is checking peek() and doing poll().
    I recommend synchronizing all access to pq so in between your peek() and your poll() another thread isn't adding/removnig elements out from under you.synchronized(pq)    // one thread runs this
        if(pq.peek() == null)
            element = pq.poll();
            element.method();
    synchronized(pq)        // another thread attempts to run this, but must wait until it's safe.
        pq.add(...);
        pq.remove(...);
    }

  • A game example in wireless toolkit: why synchronize?

    For exmaple of wormgame come with j2me wireless toolkit,the worm is synchronized when accessed.
    synchronized (worm) {
         worm.removeAllElements();
         worm.addElement(new WormLink(INIT_X, INIT_Y,
                             INIT_LEN, INIT_DIR));
         // Reset class variables
         currentDirection = INIT_DIR;
         needUpdate = false;
         hasEaten = false;
         moveOnNextUpdate = false;
    But when start the game MIDlet,only one thread is create.
    in wormmain.java
    protected void startApp() {
              Display.getDisplay(this).setCurrent(theGame);
              try {
                   // Start the game in its own thread
                   Thread myThread = new Thread(theGame);
                   myThread.start();
              } catch (Error e) {
                   destroyApp(false);
                   notifyDestroyed();
    Does it really need to synchronzied the worm?

    I know this message is a little old already, but I'll reply anyway.
    I haven't examined the code, but when you create a new thread, you will not only have this thread running, but also the main thread. So you have actually two threads running. And if they both modify the worm Vector it is necessary to synchronize it.

  • PRFI - Error

    Hello Gurus,
    When I execute PRFI, I get the following error
    ALE receiver determination failed, no RFC destination maintained  
    Can anyone please provide some input on this issue?  I am new to travel management module, so not sure what to check in SM59. 
    Thanks,
    Ram

    Hi,
    Please look at the documentation (especially "Prerequisites"):                                                                               
    -> Transaction SALE                                                         
       -> Application Link Enabling (ALE)                                       
          -> Modelling and Implementing Business Processes                      
             -> Predefined ALE Business Processes                               
                -> Human Resources                                              
                   -> HR <-> AC                                                 
                      -> Coding Block                                           
    Coding Block                                                                               
    The coding block is an Accounting service. When posting transactions are    
    effected, it can be used to enter account assignments whose validity is     
    then checked.                                                                               
    The manner in which the coding block occurs in posting transactions         
    never changes (subscreen). In the HR application, this is the case for      
    time recording, travel expenses, master data, and construction pay. The     
    following fields are available:                                                                               
    - Company code                                                             
    - Business area                                                            
    - Cost center                                                              
    - Order                                                                    
    - Cost object                                                              
    - Activity type                                                            
    - Funds center                                                             
    - Commitment item                                                          
    - Fund                                                                     
    - Sales order and item                                                     
    - WBS element                                                              
    - Network and operation number                                                                               
    The checks performed for the coding block are integrated with the cost      
    distribution infotypes (0027 in Payroll, and 1018 in Organizational         
    Management). The following account assignments are possible:       
    - Company code and/or controlling area                                 
    - Cost center                                                          
    - Order                                                                
    - WBS element                                                          
    - Funds center                                                         
    - Fund                                                                               
    Additional account assignments can be checked locally, or in an         
    Accounting system via synchronous remote access. This system must       
    include master data for all of the account assignment fields entered.                                                                               
    Distribution Model Maintenance                                                                               
    To ensure communication between systems for validation purposes, you    
    must call the Maintain Distribution Model step and enter the following  
    for Add Method:                                                                               
    -> Client: HR System                                                   
    -> Server: Accounting System                                           
    -> Object: AcctngServices                                              
    -> Method: CheckAccountAssignment                                      
    -> Filter object: Company code                                                                               
    An entry in the distribution model ensures that account assignments can 
    be checked in the Accounting System for all HR transactions that use    
    the coding block. If account assignments in the Accounting System can   
    be replicated in the HR System (that is, cost centers, internal orders, 
    and WBS elements), you are advised to set up the Local Validation for   
    Coding Block function. If a line item includes an account assignment    
    that cannot be checked locally, the entire line item is checked in the  
    Accounting System. Checks are also performed remotely if you do not     
    configure the local check function for coding blocks.                                                                               
    Prerequisites                                                                               
    If you want to use the company code filter object in the distribution   
    model, customizing data for the company code must be replicated in the  
    HR System.                                                                               
    >> All master data for which the input help function is     <<        
      >> required to display valid values must be replicated      <<        
      >> in the HR System. WBS elements must also be replicated   <<        
      >> so that they can be entered in an HR System.             <<        
    Regards,
    Raynard

  • Thread-safe rehash

    Here is a problem that at first sounds simple, but in fact is quite the opposite.
    Let's say I want to keep get(key) of a hash map unsynchronized for maximum performance (other methods may be synchronized). What would be the best way to implement the rehashing?
    - Marcus Sundman

    A simple (and somewhat dumb) algorithm would be to
    create a second hash map (or substructure) as
    identical copy, rehash it and then switching over.
    This is the time when at least one reference changes.
    This change may be atomic but your read operation
    surely isn't.OK, so this translates to the following code:Look at this version:
       Bucket[] table;
       Object get(Object key) {
        // get a reference of the table
    (1) Bucket b = table[key.hashCode()%table.length];
        // uses reference
    (3) return b.get(key);
       synchronized rehash() {
        // get a copy of the table
        Bucket[] new_table = getCopy(table);
        // resize the copy
        rehash(new_table, new_table.length*2);
        // replace the table
        table=new_table;
    (2) // old table reference gets probably invalid at end of function
       }The hypothetical but possible execution order is
    (1) Get a Bucket reference in get()
    (2) rehash function ends, making table reference invalid
    (3) get() references an overwritten or garbage collected memory area
    The unsynchronized sequence get ref / use ref is not atomic and can't be. For a single operation this is not likely but this depends of course on your load and the frequency of rehash operations. It's like most thread-unsafe programs which are running fine most of the time but eventually they crash: one of these nasty errors no one is ever tracking down. The only solution would be to make a complete deep copy of the table at (1).
    That depends on the situation. If a rehash is needed
    once a year and read operations are performed several
    hundred times a second then effectiveness of the
    rehash procedure doesn't really matter.But then the exact time of the rehash doesn't matter either. So you can cooperatively stop your reads once a year to make a rehash.
    I don't think so. Execution order has to be source
    code order if the operations depend on each other or
    the compiler can't decide if it's the case. E.g.
    boolean evaluation has to be in order else it would
    break many programs.Can you point to the section of the specs that says
    that? Java Language Spec (v2.0, ch. 14.2) states:
    A block is executed by executing each of the local variable declaration statements and other statements in order from first to last (left to right).
    Oh, I didn't realize that you were talking about a read/write lock.
    However, this still requires all reads to synchronize,
    and if the actual lookup is fast then this algorithm
    is quite slow since each access (both reads and
    writes) will have to synchronize twice (once to
    aquire the lock and once to release it).Change the code. If you don't want synchronized read access you don't have to acquire the mutex, just change the read count. But if you don't have a read count there is no point in synchronizing write. If you happen to invent an algorithm which is data consistent with unsynchronized read/write pairs, it will also be consistent with concurrent unsynchronized write/write operations (I guess :-).

Maybe you are looking for

  • How to merge PDF docs into one?

    How can I merge several individual PDF documents into one? Can I use preview for this? Thanks a

  • IBook G3 (USB): Unable to connect to the internet

    Good Morning; I am having a problem with my iBook G3 Model M7701ll, 600 mhz which I just bought. The iBook is running OS 10.4.7 I live remote and my only connection to the web is dial up with a 24.4K connection on my G4 using a external USB Modem. I

  • Iphone 4 Device UDID

    I'm all a bit new to the Devs corner so please be gentle In my excitement, (or stupidity) I've installed iOS 5 beta on my iphone 4, forgetting like a chimp to add my UDID to my devices list. Only is I can't access it now via iTunes for obvious reason

  • Macbook Dying by a thousand cuts

    Hi Hope someone can help. Macbook all ok until I downloaded some MAC generated updates recently, sorry I don't know what they were. Anyway, -Battery now won't charge -Bluetooth won't work -Entourage just gives Error 16008 all the time (yes have tried

  • OpenSPARCT1 on Virtex-7

    Hi, I am trying to implement OpenSPARCT1 on Xilinx Virtex-7(VC707 XC7VX485T-2FFG1761CES Evaluation Board), single core and multi core. I first tried the EDK project on Virtex-5 and now I want to check feasibility of implementation of the same on Virt