Synchronizing methods and a static main method

I have a class with a static main method, calling 3 different jdbc updates.
I want to make sure this is thread safe when called for various clients.
It's goes:
args[0] = clientI
update 1:update field1 where clientid = client1
update 2:update field2 where clientid = client1
update 3:update field3 where clientid = client1I assume I am in jeopardy of mixing up my data?
I don't think I can synchronize a main method, nor would it do any good to synchronize the class?
Should I break the updates into methods, then synchronize them, or synchronize parts of the code?
you opinion would help greatly.

You haven't provided enough information.
Are those updates occurring sequentially? Then it's threadsafe.
Are they running in different threads? Then to make them threadsafe you'll need proper synchronization, or a database transaction.
Of course, looking at those particular updates, I don't see any reason why you'd need to worry about thread safety. They look to be independent of each other. Or perhaps you meant you need them to be atomic? That is, any query will see either the old values of all three fields, or the new values of all three fields. Nobody will be able to see the new values for fields 1 and 2, but the old value for field 3. If that's the case, then those three updates need to be in a database transaction.

Similar Messages

  • Does not contain a static 'Main' method suitable for an entry point_

    Hello
    I want to to do a project with Entiity Framework . I add a project to my solution for my domain class and it's name is 'LinkModel.DomainClasses'.  when I prees F5 there is an error like bellow.
    Could You help me to solve this? thanks alot
    ..\Documents\Training\LinkCodeFirstLast\LinkCodeFirst\LinkCodeFirst\LinkModel.DomainClasses\obj\Debug\LinkModel.DomainClasses.exe'
    does not contain a static 'Main' method suitable for an entry point

    Hi bkshn,
    This error is caused by the missing "Main" method in your project. it is the entry point of your project.
    If you want to create a EF project, you could follow the way in the aricle below.
    https://msdn.microsoft.com/en-us/data/ee712907#codefirst
    The Main method is like below.
    class Program
    static void Main(string[] args)
    using (var db = new BloggingContext())
    // Create and save a new Blog
    Console.Write("Enter a name for a new Blog: ");
    var name = Console.ReadLine();
    var blog = new Blog { Name = name };
    db.Blogs.Add(blog);
    db.SaveChanges();
    // Display all Blogs from the database
    var query = from b in db.Blogs
    orderby b.Name
    select b;
    Console.WriteLine("All blogs in the database:");
    foreach (var item in query)
    Console.WriteLine(item.Name);
    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
    And you could start to learn the EF from the following MSDN blogs.
    https://msdn.microsoft.com/en-us/data/ee712907
    If you have any other concern regarding this issue, please feel free to let me know.
    Best regards,
    Youjun Tang
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Projectname.exe' does not contain a static 'Main' method suitable for an entry point .

    Hi,
    I'm developing a blog reader for windows 8 store app. It was perfectly worked before. But because of some miss behave my coding i get below error. No other errors there.
    Error    1    Program 'c:\Users\.........\Desktop\Blog_Reader\Blog_Reader\obj\Release\intermediatexaml\Blog_Reader.exe' does not contain a static 'Main' method suitable for an entry point    C:\Users\..........\Desktop\Blog_Reader\Blog_Reader\CSC  
     Blog_Reader
    Please help me to figure this.
    Thank You!

    Go to your App.Xaml and R-Click Go to Properties
    Check whether the Build Action is ApplicationDefinition
    If not change it to ApplicationDefinition. Clean the code (solution ) and Deploy..
    Chears....

  • Solution for Windows Store app "projectname.exe" does not contain a static 'Main' method suitable for an entry point . Error.

    Hi,
    I'm developed a blog reader for windows 8 store app. It was perfectly worked before. But suddenly it started to miss behave and I got an
    error. No other errors were there other than that.
    Error 
    Program c:\Users\.........\Desktop\Blog_Reader\Blog_Reader\obj\Release\intermediatexaml\Blog_Reader.exe' does not contain a static 'Main'
    method suitable for an entry point. 
    C:\Users\..........\Desktop\Blog_Reader\Blog_Reader\CSC    Blog_Reader
    But I found the solution while I fixing it.
    Solution for that is like below.
    Go to your App.Xaml and Right-Click thenGo to Properties
    Check whether the Build Action is
    ApplicationDefinition
    If not change it to ApplicationDefinition.
    Clean the code (solution) and Deploy
    Now the error is fiexed.

    Hi Robana, 
    Good sharing on the Technet. 
    This will definitely benefit other who may encounter the same issue as yours.
    Thanks for your sharing again. 
    Kate Li
    TechNet Community Support

  • Static main methods and run()

    I am having trouble with main methods, and how to structure code, im not 100% sure on what static methods are, and im not sure how to use it when calling other methods etc cos its static and stuff - i read that i should put stuff in the constructor - but im not sure how this will work with a run()
    I have written a program to draw some objects (just an oval at a certain position)
    anyway here is the main method:
    public static void main(String[] args) {
            // TODO code application logic here
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    Main game = new Main();
                    game.createAndShowGUI();
                    //Create some graphics objects at the desired position!
                    GOA[0] = new GraphicsObject(300,200);         *
                    GOA[1] = new GraphicsObject(350,200);         *
                    game.frame.updateGraphics(GOA);            *
        }I am getting "non-static variable GOA cannot be referenced from a static context" at * i cant just make everything static, how should this be structured? where should the run() be?
    Thanks for any help

    Ok, i might as well include the whole thing - its not too big , and i would like to see if any of it is right really.
    ackage joefootball2;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferStrategy;
    public class Main {
        GameWindow frame;
        //Create the graphics object array to pass to the painting stuff
        GraphicsObject[] GOA;
        private void createAndShowGUI() {
            //Create and set up the window.
            frame = new GameWindow();
            frame.DrawWindow();
        public static void main(String[] args) {
            // TODO code application logic here
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    Main game = new Main();
                    game.createAndShowGUI();
                    //Create some graphics objects!
                    GOA[0] = new GraphicsObject(300,200);
                    GOA[1] = new GraphicsObject(350,200);
                    game.frame.updateGraphics(GOA);
    class GameWindow extends JFrame
        DrawPanel canvas;
        public void updateGraphics(GraphicsObject[] GOA)
            canvas.updateObjects(GOA);
        public void DrawWindow()
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            canvas = new DrawPanel();
            canvas.setDoubleBuffered(true);
            this.getContentPane().add(canvas);
            this.setVisible(true);
            this.setSize(800, 600);
            this.setResizable(false);
    class DrawPanel extends JPanel
        //Create the graphics object array to draw
        GraphicsObject[] GOA;
        public void updateObjects(GraphicsObject[] givenGOA)
            GOA=givenGOA;
        public void paint(Graphics g)
            for (int i=0 ; i<GOA.length ; i++)
                g.drawOval(GOA.x-5, GOA[i].x-5, 10, 10);
    class GraphicsObject
    //for now, just make the graphics object a point to draw a circle
    int x; int y;
    }thanks                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Difference between calling static method and not static method?

    Hi,
    Suppose i want to write a util method, and many class might call this method. what is better? writing the method as static method or not static method. what is the difference. what is the advantace in what case?

    writing the method as static method or not static
    method. what is the difference.The difference is the one between static and non-static. Any tutorial or the JLs will clarify the difference, no need to repeat it here.
    what is the advantace in what case?It's usually not like you have much of a choice. If you need access to an instance's attributes, you can't make it static. Otherwise, make it static.

  • When I want to select multiple files in a row, the click shift method and the click drag method is not working. Is there a setting I need to change and if so how?

    My click then shift+click will not select the multiple files, Is there a setting to change somewhere?

    Click and shift works for me, so this is not a Mavericks bug.
    Try deleting any finder.plist files and restarting.
    To do that, click the desktop so Finder is active, then click on Go in the menu bar. In the dropdown select Library. In that folder look for Preferences. In the Preferences folder there will be at least two files whose names start com.apple.finder.....
    Trash them all and restart.

  • Static synchronized methods VS non-static synchronized methods ??

    what is the difference between static synchronized methods and non-static synchronized methods as far as the behavior of the threads is concerned? if a thread is in static synchronized method can another thread access simple (ie. non static) synchronized methods?

    javanewbie80 wrote:
    Great. Thanks. This whole explanation made a lot of sense to me.Cool, glad I was able to help!
    Probably I was just trying to complicate things unnecessarily.It's a classic case of complexity inversion. It seems simpler to say something like "synchronization locks the class" or "...locks the method" than to give my explanation and then extrapolate the implications. Just like the seemingly simpler, but incorrect, "Java passes objects by reference," vs. the correct "Java passes references by value," or Java's seemingly complex I/O vs. other languages' int x = readInt(); or whatever.
    In the seemingly complex case, the primitive construct is simpler, but the higher level construct requires more assembly or derivation of the primitive constructs, making that case seem more complicated.
    Okay, I just re-read that, and it seems like I'm making no sense, but I'll leave it, just in case somebody can get some meaning out of it. :-)

  • Instantiation outside main method

    Why can't you do the following? I get an error message: Java programs\Exp.java:14: non-static variable scan cannot be referenced from a static context
    boxes = scan.nextInt(); I don't understand this message since you can instantiate objects and initialize variables within a class and outside a method with no problem. What's so special about the main method that you cannot do this? By the way, I know how to fix this. I just want to know why you can't do this.
    import java.util.Scanner;
    public class Exp
         Scanner scan = new Scanner(System.in);
         public static void main(String[] args)
              int boxes;
              System.out.println("Enter a number: ");         
              boxes = scan.nextInt();
              if(boxes > 5)
                   System.out.println("So what!");
              else
                   System.out.println("Less than!");
                   System.out.println("I don't know.");
    }

    afried01 wrote:
    Well, if the objects are instantiated outside the main method then none of them could be used inside that method. You would have to use them outside the main method or any other static method, that is, if I'm getting your point correctly.No. You could still use them within the main method, but first you'd have to instantiate an object of the class.
    Keep in mind that your main method should be pretty lightweight. You should only use it to bootstrap your program (i.e., to start it up at the beginning). So generally you'd instantiate the primary classes of your application, maybe call an initialization or start method on one of them, etc. If your main method is more than 15 or so lines of code it's probably too heavy.
    Or you could just make the field (outside the main class) static, so the (static) main method could use them, but as mentioned already that's generally not the right approach.

  • Main method errors

    I am trying to create the Euclid Algorithm in a simple command line application. I get the following error when I try to compile...
    Cannot make a static reference to the non-static method getInputs()
    Cannot make a static reference to the non-static method doGCD()
    Cannot make a static reference to the non-static method doLCM()
    public class Euclid
         public static void main(String[] args)
    //This is where I try to invoke the methods getInputs(), doGCD(), and doLCM() but it errors.
         public void getInputs()
         {      //User defined inputs
         public void doGCD()     
         {      //Algorithm
         public void doLCM()     
         {      //Algorithm               
    Is there a proper way to invoke these because I've been researching and I think I need to create objects, but I'm not sure what that means.

    adam.lee wrote:
    Try putting your methods outside of the main method. What does that even mean? They are outside the main() method. Unless you mean call them somewhere else, but that doesn't make sense because if you can't call any methods inside the main() method then you can't make your program do anything.
    You can call them inside the main method but you might have to put 'static' in front.Terrible advice. You only ever make a method static if it needs to be static. Never, ever, ever, ever, EVER make a method static just for the sake of getting your program to compile. This breeds ignorance of what the term 'static' even means. Only make it static if it should belong to the class, and not an object of the class. A method like getInputs() shouldn't be static because each Euclid object could potentially have it's own inputs.

  • Main method question in java, please help

    Guys I have looked all over the internet, and cannot understand any of these questions: I would really appreciate any help on them. The first, and most important thing I would like to know is, what is static: what is a static int, or a static main method? Why is this necessary, and what does it do? Could you please provide an example? Also, why is String[] args necessary within the parameters of the main method? Is this so that it can execute it? Last thing is: objects. Where can I learn about them? I know they are made with the new operator, but don't understand their use? Could you explain why they are useful? Can you give them values? and how do they relate to the dot operator? After these are answered, I will be so happy because I will feel like I actually know a little bit. Right now, I'm just confused... Thank you so, so much in advance!

    The first, and most important thing I would like to know is, what is static'static' means that the item concerned is associated with the class itself rather than an instance: so, it is accessible to all instances.
    Could you please provide an example?
    public static void main(String[] args)is an example, but you knew that. I don't really understand the question.
    Also, why is String[] args necessary within the parameters of the main method?'args' receives the arguments you type on the command line after the class name or jar name, depending on how you invoke the application.
    This is all covered in the documentation, which you should read before asking further questions.
    Last thing is: objects. Where can I learn about them?If you don't know about objects you're not going to learn about them by asking questions on a forum. It's far too large a topic. Get yourself a book.

  • Main method in mulitple classes?

    I'm new to java as some of you might know, and I've been working on a single class. Now I'm trying to add another class. I've tried many ways to do this, and I think I almost have it, but I'm still a little stuck. Here are a few questions:
    1. I only can have one main method, which goes in the main class, right? Then the subclasses just have methods? So, how does the subclass know which methods to call first, and when? If there is a main method in the subclasses, t should be private and the main method in the main class should be public? So if I have multiple methods in a subclass, how do I tell my main method which to call? I've tried:
    new Timer.test();
    new Timer(test);Neither seem to work.
    So, organized in a tree it should be like so:
    Public class mainClass
         public main method
              new subClass
      Private class subClass
          (no main method?)
              private subClassMethod1
              private subClassMethod2I've tried searching, but maybe I'm not searching for the right terms. I can find things on creating classes and methods, but not really on the way multiple classes are set up. Can someone explain so I understand exactly what I'm doing with multiple classes, or give me a link to a good explanation? Thanks
    Also, the subclass ALWAYS extends mainClass right?
    Edited by: LIVE3A17 on Apr 28, 2009 3:13 PM

    LIVE3A17 wrote:
    Ok, so I can use main methods in the other classes, and java knows which is the main class by which one is opened when the program is run. Got it. Don't even put a main method in the other classes, unless you intend on one of them to be an entry point. Otherwise it's just code bloat.
    >
    Now, I have a main method in both classes. When I have the main method in the main class set to public, I get an error in the second class saying "Cannot reduce the visiblity of the inherited method". I gives me the option to change visibilty of my main class to private. However, when I do this, and try to run the main class, I get an error saying main method not public.I don't know what you're talking about. Sounds like you are subclassing something and for whatever reason trying to 'override' the main method. The normal static main method isn't even inherited, being static. So this error you're speaking of doesn't make much sense. Of course without seeing your code, we're left to mindreading exercises which usually don't go so well.

  • Public main method in a non-public class

    Hi,
    It is said that main method in a java class must be public so that it can be invoked by JVM. This works even if the class itself has default modifier.
    But while programming, any method (be it public) of a class with default access modifier from one package can't be accessed by classes from other packages. For example,
    1. Consider class A in package a.
    package a;
    class A {
    public void methA() {}
    2. Now consider class B in pakage b.
    package b;
    class B {
    void methB() {
    // Here it can't access class A and its method methA()
    So how does JVM invoke the main method of a class with default access modifier. Assume that the class is in some package a.b.c
    Regards,
    Amit

    Hi,
    Maybe this can also be beneficial for you.
    Standalone applications don't necessarily have init and start methods. They must have a public static void main routine to start things off. The main routine typically creates some object of the master class. It can't use any non-static variables or methods in the class with the implied this, since there is no this object. It typically quickly returns. Then the event loop processing starts. Even the static main method must live inside some public class, or at least in the top class, the first class and the class for whom the source file is named. There are no such thing as standalone static methods in Java. The signature of the main class must look exactly like this. Cut and paste it to be sure:

  • Help with Declaring a Class with a Method and Instantiating an Object

    hello all i am having trouble understanding and completing a lab tutorial again!
    im supposed to compile an run this code then save work to understand how to declare aclass with a method an instantiate an object of the class with the following code
    // Program 1: GradeBook.java
    // Class declaration with one method.
    public class GradeBook
    // display a welcome message to the GradeBook user
    public void displayMessage()
    System.out.println( "Welcome to the Grade Book!" );
    } // end method displayMessage
    } // end class GradeBook
    // Program 2: GradeBookTest4.java
    // Create a GradeBook object and call its displayMessage method.
    public class GradeBookTest
    // main method begins program execution
    public static void main( String args[] )
    // create a GradeBook object and assign it to myGradeBook
    GradeBook myGradeBook = new GradeBook();
    // call myGradeBook's displayMessage method
    myGradeBook.displayMessage();
    } // end main
    } // end class GradeBookTest4
    i saved above code as shown to working directory filename GradeBookTest4.java
    C:\Program Files\Java\jdk1.6.0_11\bin but recieved error message
    C:\Program Files\Java\jdk1.6.0_11\bin>javac GradeBook4a.java GradeBookTest4.java
    GradeBookTest4.java:2: class, interface, or enum expected
    ^
    GradeBookTest4.java:27: reached end of file while parsing
    ^
    2 errors
    can someone tell me where the errors are because for class or interface expected message i found a solution which says 'class? or 'interface' expected is because there is a missing { somewhere much earlier in the code. i dont know what "java:51: reached end of file while parsing " means or what to do to fix ,any ideas a re appreciated                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Doesn't solve your problem, but this works for me...
    public class GradeBook
      public void displayMessage() {
        System.out.println( "Welcome to the Grade Book!" );
      public static void main( String args[] ) {
        try {
          GradeBook gradeBook = new GradeBook();
          gradeBook.displayMessage();
        } catch (Exception e) {
          e.printStackTrace();
    }

  • Static abstract methods - how can I avoid them?

    Hi everybody,
    just found out that java doesn't allow static abstract methods nor static methods in interfaces. Although I understand the reasons, I can't think of how to change my design to gain the required behavior without the need of static abstract methods.
    Here's what I want to do and how my thoughts lead to static abstract methods:
    ClassA provides access to native code (c-dll, using jna). The path to the dll can be set programmatically. Here's a draft of the class:
    public ClassA {
       private static String path;
       public static void setRealtivePath(String path) {
          //check if path exists and is not null -> get absolute path
          setPath(path);
       public static void setPath(String absolutePath) {
          this.path = path;
      //code to provide access to native lib
    }There is some more classes which provide access to different dlls (therefore the code for accessing the dlls differs from ClassA). Although this works, the setRelativePath method has to be implemented in each class. I thought it would be nice to put this method into a abstract super class that looks like this:
    public abstract class superClass {
       public static void setRelativePath(String path) {
          //check if path exists and is not null -> get absolute path
          setPath(path);
       //force inherting class to implement a static method
       public static abstract void setPath(String absolutePath);
    }thus simplifying ClassA and it's look-a-likes:
    public ClassA {
       private static String path;
       @Override
       public static void setPath(String absolutePath) {
          this.path = path;
      //code to provide access to native lib
    }Since static abstract methods (and overriding static methods) is not allowed, this doesn't work.
    I hope someone can catch my idea ;). Any suggestions how to do this in a nice clean way?
    Thanks in advance,
    Martin
    Edited by: morty2 on Jul 22, 2009 2:57 AM

    First of all, thanks a lot for your answer.
    YoungWinston wrote:
    Actually, you can "override" static methods (in that you can write the same method for both a subclass and a superclass, providing the superclass method isn't final); it's just that it doesn't work polymorphically. The "overriding" method masks the overridden one and the determination of what gets called is entirely down to the declared type.Yes, I know that. There's one problem: Your suggestion means that I simply have to drop the abstract modifier in the super classes setPath method. However, since the super class calls the setPath method (and not the inherting classes!) it will always be the super classes' method being called.
    YoungWinston wrote:
    Why are you so concerned with making everything static? Seems to me that the simplest solution would be to make all the contents instance-based.I want ClassA and it's look-a-likes to be set up properly at application start up, be accessible quite anytime and easily, they don't do anything themselves except for setting the path and calling into the native library which does the math. (Compareable to how you call e.g. Math.cos()). Therefore I don't think that an instance-based solution would be a better approach.
    YoungWinston wrote:
    Furthermore, you could make the class immutable; which might be a good thing - I'm not sure I'd want someone being able to change the pathname after I've >set up a class like this.Thanks for that!
    PS: As mentioned in my first post, I do have a working solution. However, I'm really corious about finding a nicer and cleaner way to do it!
    Martin

Maybe you are looking for

  • Help!: jTDS is not working on j2sdkee1.4!

    Hello there! I'm working with Win2k sp4, MS-Sql Server 7 and j2ee sdk 1.4 (the newest release). I'm trying to use the jTDS JDBC driver to access a MSSql database. This is the connection pool configuration: <jdbc-connection-pool connection-validation-

  • Overloaded Functions

    Hi, i got a question in my exam paper which went: Write a program in Java(using BlueJ) to generate the following pattern for the first 'n' number of rows using overloaded functions Following is the output for 5 rows: a)    123454321          b) .... 

  • How to get the default selection color from JTable

    Hi, there, I have a question for how to get the default selection color from JTable. I am currently implementing the customized table cell renderer, but I do want to set the selection color in the table exactly the same of default table cell renderer

  • Installed second drive but os cant see it

    installed second drive- WD 750GB 7500BPKT but OS doesnt see it contacted WD and these have no built in drives, they sent me to OS for support, but as it was OEM, they sent me for support here. help please. Thanks

  • Regarding BAPI HRXSS_CAT_WD_RECORD

    Hi Experts, This is regarding BAPI HRXSS_CAT_WD_RECORD.for  this BAPI ,  I am providing inputs like this IV_PERNR = 90030. IV_ACTION  = COPY_FROM_PREVIOUS_PERIOD. IV_BEGDA = 09/20/2008 . IV_ENDDA = 09/26/2008 . So this BAPI has to copy the previous p