How to handle race conditions in Web Service?

I implemented a Web Service with Java Servlets. I got the following setup: There is a database which handles ‘job’-entries. Each job has a status like ‘executing’ or ‘in queue’ or ‘finished’. If a user starts a new job, there is made an entry in the database with a job and the status ‘in queue’. The job should only be…
Read more

Why does the following Java 8 pipeline report a type mismatch in Eclipse?

The following code uses Java 8 streams and lambdas to find an unlimited number of “sum primes” : import java.util.stream.Stream; public class FunctionalPrime { public static void main(String[] args) { printSumPrimes(6); } /** * Given n>0, print all pairs i & j s.t. 1<=i<=j<=n and i+j is prime * * NOTE: This BREAKS in Eclipse because of some Eclipse bug!…
Read more

Refactoring a Java project causes hundreds of "X not public" errors

I’m busy upgrading an ancient java app, and after getting it compiling I set about reorganizing the code structure. There are currently 500 files in ONE massive folder, so I need to split it into a neater folder structure. The moment I moved some files, the compiler generates heaps of “X() is not public in <BaseClass>” errors. Yes, the refactoring…
Read more

why string length -1

I have found the following program to check if a string is palindrome. import java.util.Scanner; public class PalindromeString{ public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println(“Enter the string which you want to check whether that is palindrome or not: “); String s = in.next(); String r = “”; for(int i=s.length()-1; i>=0; i–){ r = r+s.charAt(i); } System.out.println(“Reverse…
Read more

Android SQLiteOpenHelper is not working

The onCreate void of my SQLiteOpenHelper is not called even when get a databse with “database = helper.getWritableDatabase();”. I read lots of posts on this page here but I didn’t find any soloution that worked for me. SQLiteOpenHelper: package eu.michael1011.currencies.sql; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class Helper extends SQLiteOpenHelper { public static final String database_name =…
Read more

Creating a boolean array by prime number

Write a code fragment that creates an N-by-N Boolean array a[][] such that a[i][j] is true if i and j are relatively prime and false otherwise. My code is public static void main(String[] args){ int N = 12; boolean[][] a = new boolean[N][N]; for(int i = 0; i < a.length; i++){ for(int j = 0; j < a[i].length; j++){ if(i%j…
Read more

Aspect around call on annotated field

I want AspectJ to inject the measuring code around all invocations of any method, on fields annotated with @Measured and capture the method’s name. This is what I have: @Pointcut(“get(@my.annotation.Measured * *) && @annotation(measured)”) public void fieldAnnotatedWithMeasured(Measured measured) {} @Around(value = “fieldAnnotatedWithMeasured(measured)”, argNames = “joinPoint,measured”) public Object measureField(ProceedingJoinPoint joinPoint, Measured measured) throws Throwable {…} Use case: public class A {…
Read more

Maven failing to find jar from Project Repository

I have a jar file which I need to include in my project. I am using eclipse with Maven I created the following folder in my project on my computer: /home/robert/eclipse/BobProjects/BobDiary/in_project_maven_repository/metcarob.com.lib/nachocalendar/0.23 and I placed a file there called: nachocalendar-0.23.jar I then added the dependancy to my POM file: <dependency> <groupId>metcarob.com.lib</groupId> <artifactId>nachocalendar</artifactId> <version>0.23</version> <scope>compile</scope> </dependency> I also added the Repo into…
Read more

Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final – Inert duplicate rows each time

Hi, There is a lot of version of this problem but all the wats to fix it are failed. Every time I insert object, it insert me duplicate data I tried to change the add function with “persist(role)” or “saveOrUpdate(role)” but it is`nt working. What could be the problem? StartupDbDataInit package com.searcher.startup; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener;…
Read more

Java generic type casting [duplicate]

This question already has an answer here: Why does this generic code compile in java 8? 3 answers why java compiler (1.8) doesn’t find an error when i use newList1()? public class Test { public static void main(String[] args) { String str1 = newList1(); //1. it’s ok String str2 = newList2(); //2. compiler will warn } private static <T extends…
Read more