Stack Exchange
Connect to Single Database with 2 users by spring boot and java
I have a DATABASE name "dev", and i want to connect this with 2 users, 
user1: "root" , password1: "password", GRANTS: save, update, delete, read
user2: "testuser", password: "password", GRANTS: Read only

I specified grants in mysql db not in Spring boot or in code.
If am block the UPDATE grant still its updating.
Please Give me solution for ONE Database with MULTIPLE USERS with DIFFERENT GRANTS.
Here is my code
my Application.properties is:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/dev?useSSL=false
spring.datasource.username =root
spring.datasource.password =password

spring.datasource2.driver-class-name=com.mysql.jdbc.Driver
spring.datasource2.url = jdbc:mysql://localhost:3306/dev?useSSL=false
spring.datasource2.username =testuser
spring.datasource2.password =password

Application.java(main class):
@SpringBootApplication(scanBasePackages = {"com.ssss"})
@EnableTransactionManagement
@EnableAsync
@ComponentScan(basePackages = {"com.ssss"})
public class Application {
@Bean(name = "datasource")
    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    public DataSource dataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "datasource2")
    @ConfigurationProperties(prefix = "spring.datasource2")
    public DataSource dataSource2(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name="tm1")
    @Autowired
    @Primary
    DataSourceTransactionManager tm1(@Qualifier("datasource") DataSource datasource) {
        DataSourceTransactionManager txm  = new DataSourceTransactionManager(dataSource());
        return txm;
    }

    @Bean(name="tm2")
    @Autowired
    DataSourceTransactionManager tm2(@Qualifier ("datasource2") DataSource datasource) {
        DataSourceTransactionManager txm  = new DataSourceTransactionManager(dataSource2());
        return txm;
    }
}

Repository class:
@Repository
public interface testRepository extends JpaRepository<testModel, Integer> {


    @Qualifier("datasource2")
    Optional<testModel> findBySbAndIsActiveTrueAndIsDeletedFalse( String sb );


    @Qualifier("datasource2")
    @Modifying
    @Query( value = "UPDATE testModel c SET c.buttonColor =:buttonColor" )
    Integer updateTestModelByFirmLogoPath( @Param( "buttonColor" ) String buttonColor );

Service Implementation class:
@Service("OpenApiService")
public class OpenApiServiceImpl implements OpenApiService {

    ......
    @Autowired
    private TestRepository testRepository;

    @Qualifier("datasource2")
    @Autowired
    private DataSource dataSource;



 //Fetching Authority
        @Override
        public TestBean getTestBean(String sb) throws Exception {

        try {
            Optional<TestModel> testModelOptional = cpaFirmRepository.findBySuAndIsActiveTrueAndIsDeletedFalse(sb);
            if(!TestModelOptional.isPresent()){
                throw Exception .......
            }

            TestModel testModel = testModelOptional.get();
            TestBean testBean= new TestBean();

            testBean.setPc(testModel.getPrimaryColor());
            testBean.setSc(testModel.getSecondaryColor());
            testBean.setBc(testModel.getButtonColor());
            testBean.setFl(testModel.getFirmLogoPath());

            return testBean;
        }catch (Exception e){
            L....error(ERROR, e);
            throw e;
        }
    }

//Updating Grant i blocked in the Mysql database
    @Transactional("tm2")
    @Override
    public String updateDB(String sb) throws Exception {

        try {


            Integer ss = testRepository.updateCpaFirmModelByFirmLogoPath("#005062");

            return "Updated success";
        }catch (Exception e){
            LOGGER.error(ERROR, e);
            throw e;
        }
    }
}


Would you please fix the massive formatting issues?

Your @Qualifier is pretty. much useless on those methods and you are basically using the main (thus the root) DataSource only. You want to use a routing datasource to route the read-only one to the correct datasource, else use the default. Alsoyour configuration is flawed as you should configure 2 JpaTransactionManager instances and not DataSourceTransactionmanagers.

But previously am implemented many services, and am using JWT auth, if i create JpaTransactionManager its throwing exception like "entityManagerFactory bean not found"
