Table of Contents
Question content
Solution
Home Java When trying to start two or more Spring Batch jobs at the same time, an error is thrown: ORA-08177: Unable to serialize access for this transaction

When trying to start two or more Spring Batch jobs at the same time, an error is thrown: ORA-08177: Unable to serialize access for this transaction

Feb 09, 2024 pm 05:00 PM
lsp

php editor Xigua may encounter a problem when using Spring Batch: When trying to start two or more Spring Batch jobs at the same time, an error will be thrown: ORA-08177: Unable to serialize access to this transaction . This error may be confusing, but it is actually caused by a database locking issue. Before solving this problem, we need to understand some background knowledge about Spring Batch and database transactions.

Question content

I encountered an error when trying to use completablefuture to run two spring batch jobs in parallel. The error message is as follows:

originalsql = insert into batch_job_instance(job_instance_id, job_name, job_key, version)
    values (?, ?, ?, ?)
, error msg = ora-08177: can't serialize access for this transaction
Copy after login

I am using spring batch 5.x, spring boot 3.1.6, jdk 17

Application Properties

spring.batch.repository.isolationlevelforcreate=isolation_read_committed
spring.batch.isolationlevel=read_committed
spring.batch.jdbc.table-prefix=batch_
spring.batch.job.enabled=false
Copy after login

batchconfig.java

@configuration
public class batchconfig {

@bean("simpletaskexecutor")
    public taskexecutor simpletaskexecutor() {
        simpleasynctaskexecutor asynctaskexecutor = new simpleasynctaskexecutor("simpletaskexecutor");
        asynctaskexecutor.setconcurrencylimit(concurrencycount);
        return asynctaskexecutor;
    }


@bean("joblauncherasync")
    @scope("prototype")
    public joblauncher joblauncherasync(datasource datasource, jobrepository jobrepository) throws exception {

        taskexecutorjoblauncher joblauncher = new taskexecutorjoblauncher();
        joblauncher.setjobrepository(jobrepository);
        joblauncher.settaskexecutor(simpletaskexecutor());
        joblauncher.afterpropertiesset();
        return joblauncher;
    }
    
    @bean
    public jpatransactionmanager transactionmanager(entitymanagerfactory entitymanagerfactory) {
        return new jpatransactionmanager(entitymanagerfactory);
    }
    
    @bean
    public batchjobexecutionlistener batchjobexecutionlistener() {
        return new batchjobexecutionlistener();
    } 
    
    @bean
    public batchjobstepexecutionlistner batchjobstepexecutionlistner() {
        return new batchjobstepexecutionlistner();
    }
Copy after login

}

employeejobconfig.java

@configuration
@import(batchconfig.class)
public class employeejobconfig{
    
        
    @autowired
    employeestatuswritter employeestatuswritter;
    
    @autowired
    employeependingprocessor employeependingprocessor;
    
    
    
    @bean("employeependingreader")
    public jpapagingitemreader<employee> employeependingreader(datasource ds,entitymanagerfactory entitymanagerfactory) {
        
        jpapagingitemreader<employee> jpareader = new jpapagingitemreader<>();
        jpareader.setentitymanagerfactory(entitymanagerfactory);
        jpareader.setquerystring("select e from employee e");
        jpareader.setpagesize(5000);
        return jpareader;

    }
    
    @bean("employeespinvokestep")
    public step employeespinvokestep(@qualifier("employeependingreader") itemreader<employee> reader,
            @qualifier("employeestatuswritter") itemwriter<employee> writer,
            @qualifier("employeependingprocessor") itemprocessor<employee, employee> processor,jobrepository jobrepository,
            platformtransactionmanager transactionmanager,batchjobstepexecutionlistner batchjobstepexecutionlistner,taskexecutor simpletaskexecutor) {

        return new stepbuilder("employeespinvokestep",jobrepository)
                .<employee, employee>chunk(50,transactionmanager).reader(reader)
                .processor(processor).writer(writer)
                .listener(batchjobstepexecutionlistner)
                .taskexecutor(simpletaskexecutor)
                .build();
    }
    
    @bean("employeespjob")
    public job employeespjob(@qualifier("employeespinvokestep") step employeespinvokestep,jobrepository jobrepository,batchjobexecutionlistener batchjobexecutionlistener) {
        return new jobbuilder("employeespjob",jobrepository)
                .incrementer(new runidincrementer())
                .listener(batchjobexecutionlistener)
                .start(employeespinvokestep)
                .build();
    }

}
Copy after login

managerconfig.java

@configuration
@import(batchconfig.class)
public class managerconfig {
    
        

    @autowired
    managerstatuswritter managerstatuswritter;
    
    
    @autowired
    managerpendingprocessor managerpendingprocessor;
    
    @bean("managerpendingreader")
    public jpapagingitemreader<manager> managerpendingreader(datasource ds,entitymanagerfactory entitymanagerfactory) {
        
        jpapagingitemreader<manager> jpareader = new jpapagingitemreader<>();
        jpareader.setentitymanagerfactory(entitymanagerfactory);
        jpareader.setquerystring("select m from manager m");
        jpareader.setpagesize(5000);
        return jpareader;

    }
    
    @bean("managerspinvokestep")
    public step indvinvoiceconsctlspinvokestep(@qualifier("managerpendingreader") itemreader<manager> reader,
            @qualifier("managerstatuswritter") itemwriter<manager> writer,
            @qualifier("managerpendingprocessor") itemprocessor<manager, manager> processor,jobrepository jobrepository,
            platformtransactionmanager transactionmanager,batchjobstepexecutionlistner batchjobstepexecutionlistner,taskexecutor simpletaskexecutor) {

        return new stepbuilder("managerspinvokestep",jobrepository)
                .<manager, manager>chunk(5000,transactionmanager).reader(reader)
                .processor(processor).writer(writer)
                .listener(batchjobstepexecutionlistner)
                .taskexecutor(simpletaskexecutor)
                .build();
    }
    
    @bean("managerspjob")
    public job managerspjob(@qualifier("managerspinvokestep") step indvinvoiceconsctlspinvokestep,jobrepository jobrepository,batchjobexecutionlistener batchjobexecutionlistener) {
        return new jobbuilder("managerspjob",jobrepository)
                .incrementer(new runidincrementer())
                .listener(batchjobexecutionlistener)
                .start(indvinvoiceconsctlspinvokestep)
                .build();
    }
    
}
Copy after login

batchjobmanager.java

@service
public class batchjobmanager {

    
    
      @autowired applicationcontext context;
     
      @autowired batchexecutorservice batchexecutorservice;
      
      @autowired
      batchjobrunner batchjobrunner;
      
       
    
    public void startjob() {
        
         try {
               system.out.println("batchjobmanager called .. "+new date());
                
               string[] invoicenames={"employeespjob","managerspjob"};
               
               list<string> invoicenameslist = arrays.aslist(invoicenames);
               launchasyn(getbatchjoblist(invoicenameslist));
                
                 
         } catch (exception e) {
             system.out.println("while loading job..");
             e.printstacktrace();
         }
        
    }
      

    public list<batchjob>  getbatchjoblist(list<string> jobnames) throws exception{
        list<batchjob> batchjoblist=new arraylist<batchjob>();
        for(string job:jobnames) {
             batchjob batchjob= batchjob.builder().jobname(invoicejob).build();
             batchjoblist.add(batchjob);
        }
        return batchjoblist;
    }
      
    
       
    public void launchasyn( list<batchjob> batchjoblist) throws exception{
         list<completablefuture<batchjob>> batchjobfuturelist = new arraylist<completablefuture<batchjob>>();
          
          for(batchjob batchjob:batchjoblist) {
          completablefuture<batchjob> jobfuture = batchexecutorservice.execute(batchjob, asynctaskexecutor);
          batchjobfuturelist.add(jobfuture);
          }
          
          completablefuture<void> jobfutureresult = completablefuture
                    .allof(batchjobfuturelist.toarray(new completablefuture[batchjobfuturelist.size()]));
          
          
          completablefuture<list<canbatchjob>> allcompletablefuture = jobfutureresult.thenapply(future -> {
                return batchjobfuturelist.stream().map(completablefuture -> completablefuture.join())
                        .collect(collectors.tolist());
            });
        
          list<batchjob> resultfuturelist=allcompletablefuture.get();
          
          for(batchjob batch:resultfuturelist) {
              system.out.println("status "+batch.getiscompleted());
          }
    }
   
    
}
Copy after login

batchexecutorservice.java

@service
public class batchexecutorservice {
    
        
    @autowired 
    batchjobrunner batchjobrunner;
        
    public completablefuture<canbatchjob> execute(canbatchjob canbatchjob,taskexecutor threadpooltaskexecutor){
        return completablefuture.supplyasync(() -> batchjobrunner.execute(canbatchjob),threadpooltaskexecutor);
    }
    
    
}
Copy after login

batchjobrunner.java

@service
public class batchjobrunner {

    @autowired applicationcontext context;
      
    
     @autowired 
     @qualifier("joblauncherasync")
     joblauncher joblauncherasync;
    
    
    /*
     * @autowired joblauncher joblauncherasync;
     */
    
    public batchjob execute(batchjob batchjob) {
        try {
            system.out.println(" batchjob"+batchjob.getjobname()+" called ...");
            joblauncherasync.run(getjob(batchjob.getjobname()), getjobparameters(batchjob.getjobname()));
            thread.sleep(15000);
            batchjob.setiscompleted(true);
            system.out.println(" batchjob"+batchjob.getjobname()+" completed ...");
        }
        catch(exception e) {
            system.out.println("exception "+e.getmessage());
            batchjob.seterrordesc(e.getmessage().tostring());
            e.printstacktrace();
        }
        
        return canbatchjob;
    }
    
    public job getjob(string jobname) {
        
         return  (job) context.getbean(jobname); 
    }
    
    public jobparameters getjobparameters(string jobname) {
    
    jobparameters  jobparameters = new jobparametersbuilder() .addstring("unique_id",
              uuid.randomuuid().tostring(), true) .addstring("job_name", jobname,
              true) .adddate("execution_start_date", date.from(instant.now()),
              true).tojobparameters();
    return jobparameters;
    }
    
}
Copy after login

batchjob.java

public class BatchJob {

    private String jobName;
    private Boolean isCompleted;
    private String errorDesc;
    
    
}
Copy after login

Jobs run successfully when executed one by one or in sequence. However, when using completablefuture, I ran into a problem. Is starting spring batch jobs at the same time the right way?

Solution

1. Add the following properties to application.properties

spring.main.allow-bean-definition-overriding=true

2. Updated joblauncher(), as shown in batchconfig.java below

@bean("joblauncher")
    public joblauncher joblauncher(datasource datasource, jobrepository jobrepository) throws exception {
        taskexecutorjoblauncher joblauncher = new taskexecutorjoblauncher();
        joblauncher.setjobrepository(jobrepository);
        joblauncher.settaskexecutor(simpletaskexecutor());
        joblauncher.afterpropertiesset();
        return joblauncher;
    }
Copy after login
  • Removed completablefuture.

  • Deleted batchexecutorservice.java

  • Added the following methods in batchjobmanager.java to call jobs.

    public void launchSync(List<BatchJob> batchJobList) throws Exception {
             for(BatchJob batchJob:batchJobList) {
                  batchJobRunner.execute(batchJob);
                  }  
         }
    Copy after login

    The above is the detailed content of When trying to start two or more Spring Batch jobs at the same time, an error is thrown: ORA-08177: Unable to serialize access for this transaction. For more information, please follow other related articles on the PHP Chinese website!

  • Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How to hide photo album on Redmi Note13RPro? How to hide photo album on Redmi Note13RPro? May 01, 2024 pm 12:50 PM

    RedmiNote13RPro is a new model with very good performance and configuration. This phone has a very useful function of hiding photo albums. It can help users hide their photo albums so that others cannot view your photo albums. Next, the editor will tell you how to hide the photo album on Redmi Note13R Pro to help you protect your privacy. How to hide photo album on Redmi Note13RPro? 1. Enter the settings of your Xiaomi phone. 2. Then click Privacy and Protection. 3. Click Protect Privacy here again. 4. Click on the safe. 5. Finally, you can set up a private photo album here. Frequently Asked Questions Connecting to Bluetooth Changing Input Method Theme Changing Factory Settings Hide Applications Mirroring TV NFC Enable Dual SIM Installation Activation Time

    How to adjust the font size on Redmi Note13RPro? How to adjust the font size on Redmi Note13RPro? May 07, 2024 pm 06:34 PM

    Do not change the meaning of the original content, fine-tune the content, rewrite the content, do not continue. Redmi Note13RPro not only has excellent performance, but also has an even better user experience. In order to give everyone a more comfortable experience, Redmi Note13RPro is equipped with a font adjustment function to allow users to Ability to adjust fonts for your phone. If you want to know how to adjust the font size of Redmi Note13R Pro, then take a look. How to adjust the font size on Redmi Note13RPro? 1. Open the Settings app. 2. Scroll to the bottom and select "Display." 3. Click "Text Size". 4. On this screen, you can adjust the font size by dragging the slider or select a preset font size. 5. After the adjustment is completed, press "OK" to save

    How to connect Redmi Note13RPro to the computer? How to connect Redmi Note13RPro to the computer? May 09, 2024 pm 06:52 PM

    The phone Redmi Note13RPro has been very popular recently. Many consumers have purchased this phone. However, many users are using this phone for the first time, so they don’t know how to connect the Redmi Note13RPro to the computer. In this regard, the editor is here to explain to you Detailed tutorial introduction is provided. How to connect Redmi Note13RPro to the computer? 1. Use a USB data cable to connect the Redmi phone to the USB interface of the computer. 2. Open the phone settings, click Options, and turn on USB debugging. 3. Open the device manager on your computer and find the mobile device option. 4. Right-click the mobile device, select Update Driver, and then select Automatically search for updated drivers. 5. If the computer does not automatically search for the driver,

    How to turn off the camera sound on Redmi Note13RPro? How to turn off the camera sound on Redmi Note13RPro? May 02, 2024 pm 06:31 PM

    The camera function of Redmi Note13RPro is very easy to use, especially when taking pictures, it will simulate the sound of the camera shutter. However, this function may affect others in quiet situations such as libraries, so many users want to know how to turn off the camera sound of Redmi Note13RPro. , let the editor tell you below. How to turn off the camera sound on Redmi Note13RPro? 1. For the first method, you first need to open the settings of your phone. 2. Then find system applications under the settings menu. 3. Then we find the camera option under the system application interface. 4. Finally, we can set whether to turn off the camera sound in the camera interface. We only need to turn off the switch on the right side of the camera sound. 5. The second method, the first step, open

    How to extract text from pictures on Redmi Note13RPro? How to extract text from pictures on Redmi Note13RPro? May 08, 2024 pm 10:00 PM

    The Redmi Note13RPro mobile phone integrates a number of smart tools in terms of software functions. Among them, quickly and accurately extracting text content from pictures is one of them. The following editor will introduce to you how Redmi Note13RPro extracts text from pictures. How to extract text from pictures on Redmi Note13RPro? Use the Xiaomi QR code scanning function, open the QR code scanning application on your phone, click the picture icon, select a picture, and then click the "Recognize text" option on the right to successfully extract the text in the picture. Operate through mobile phone album. Find the picture for which text needs to be extracted in the mobile phone album, click "More" below the picture, and select "Extract Text". After successful recognition, you can copy or save the text as needed. Use WeChat mini programs. Open micro

    A complete guide to all levels of 'Whole Life' A complete guide to all levels of 'Whole Life' May 07, 2024 pm 06:31 PM

    Entire Huoba is a very fun casual puzzle game that everyone can experience on multiple platforms! The game provides countless exciting levels waiting to be unlocked. Each level is full of novelty and challenges, allowing you to experience an interesting adventure of answering questions and unlock new game content. So how to clear the entire level of Huoba? Today I bring you a complete guide to all levels of "Whole Life". If you encounter any problems in passing the level, you can refer to it! "The Whole Life" complete guide to all levels of "The Whole Life" The complete guide to all levels of "The Whole Life" Help her to watch the New Year's Eve, help the little sister to identify the scumbag wolf, and the grandmother helps the little girl escape from danger for outdoor adventure, pretending not to see Ah Piao and transforming back The humanoid helped the orangutan to become a humanoid. Someone in the back seat stayed calm and dealt with the female ghost. The night-shift taxi kept calm and dealt with the female ghost.

    How to set traffic display on Redmi Note13RPro? How to set traffic display on Redmi Note13RPro? May 02, 2024 pm 03:34 PM

    RedmiNote13RPro is a mobile phone that is loved by many users. This phone provides a special method to display traffic on the phone. If you still don’t know how to set up RedmiNote13RPro to display traffic, then follow the editor to find out. How to set traffic display on Redmi Note13RPro? 1. Enter "Settings" and click "Dual SIM Mobile Network". 2. Scroll down to select the "Data Package Settings" option. 3. Turn on the function switches on the right side of "Display traffic information in the notification bar" and "Display the current network speed in the status bar". 4. After the setting is successful, pull down the status bar to see the display of real-time network speed and traffic information. FAQ Connect Bluetooth Change input method theme Change factory settings Hide application casting

    'Whole Life' Journey to the West beauty helps the female characters of Journey to the West return to their places and clear the level 'Whole Life' Journey to the West beauty helps the female characters of Journey to the West return to their places and clear the level May 07, 2024 pm 03:46 PM

    Entire Huoba is a very popular fun level-breaking game on Douyin. There are many levels in the game waiting for everyone to challenge! In the game level "Journey to the West Beauty", everyone needs to help the female character of "Journey to the West" return to her position. How to pass the level? The following is a guide for you to clear the level of "Journey to the West" brought to you by the beauty of Journey to the West. Friends who don't know how to pass the level, let's take a look. I hope it can help you. "Whole Life" Journey to the West beauty helps the female character of Journey to the West to return to her place. Strategy for clearing the stage. The beauty of Journey to the West helps the female character of Journey to the West to return to her place. 1. Click on the scroll to appear the female character. 2. The white-bone spirit and the peacock spirit need to click to switch actions. 3. The answer is as shown in the figure below: "The whole life" Let's Live" has a complete guide to all levels, helping her to be a watcher of the year, helping the little sister to identify the scumbag and the wolf, and the grandmother to help the little girl escape from danger and have outdoor adventures.