Hi everyone, I am using Hikari as my connection pool thing (a manager?) with MySQL database. I follo...
m

minucha

over 5 years ago
Hi everyone, I am using Hikari as my connection pool thing (a manager?) with MySQL database. I followed stack overflow and some other tutorials and final code is as follows : I created a
DataSource
class which configures Hikari
public class DataSource {
    private static HikariConfig config = new HikariConfig();
    private static HikariDataSource ds;

    static {
        config.setJdbcUrl("jdbc:<mysql://username>:<mailto:password@q2gen47hi68k1yrb.chr7pe7iynqr.eu-west-1.rds.amazonaws.com|password@q2gen47hi68k1yrb.chr7pe7iynqr.eu-west-1.rds.amazonaws.com>:3306/mmylxkukgpzvjvfx");
        config.setUsername("username");
        config.setPassword("password");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        config.addDataSourceProperty("cachePrepStmts", "true");
        ds = new HikariDataSource(config);
    }

    private DataSource() {
    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}
In all tutorials I followed, they show this code or similer one but they don’t tell where and how to use the
getConnection()
. And the question is when, where and how do I call the
getConnecion()
method?
Am I going to call it in every request I receive (in every api method defined in every single controller) ?
@GetMapping(path = "/all")
    public @ResponseBody Iterable<ExampleEntity> getAllUsers() {
        try {
            Connection connection = DataSource.getConnection();
// what am I gonna do with the connection ?
            return mExampleService.getAll();
        } catch (SQLException ex) {
            System.err.println("Exception Occurred");
        }
    }
I"m not able to use coroutine Context, in the flowOn(....), app is crashing with `java.lang.Illegal...
a

althaf

almost 3 years ago
I"m not able to use coroutine Context, in the flowOn(....), app is crashing with
java.lang.IllegalArgumentException: Flow context cannot contain job in it. Had [SupervisorJobImpl{Active}@7b4ca1f, <http://Dispatchers.IO|Dispatchers.IO>]
interface UseCaseScope : CoroutineScope {
    override val coroutineContext: CoroutineContext
        get() = SupervisorJob() + <http://Dispatchers.IO|Dispatchers.IO>
}
The intention here is to cancel the coroutine context when we business logic need to cancel it. So that i can call coroutineContext.cancel()
class LoginUseCase(private val authRepository: AuthRepository) : FlowUseCase<LoginRequest, Boolean>() {

    override fun execute(parameter: LoginRequest): Flow<Result<Boolean>> =
        flow {
            authRepository.login(parameter.username, parameter.password).collect { response ->
                this.defaultResultHandler(onSuccess = {
                    emit(Result.success(true))
                }, response)
            }
        }.flowOn(coroutineContext)

    override fun cancel() {
        coroutineContext.cancel()
    }
}
^^^ will cause app the crash Or is this is the way to execute my intention, and ignore flowOn(.....) , i'm really confused here
class LoginUseCase(private val authRepository: AuthRepository) :
    FlowUseCase<LoginRequest, Boolean>() {

    override fun execute(parameter: LoginRequest): Flow<Result<Boolean>> =
        flow {
>>>>         launch(coroutineContext) {
                authRepository.login(parameter.username, parameter.password).collect { response ->
                    this@flow.defaultResultHandler(onSuccess = {
                        emit(Result.success(true))
                    }, response)
                }
            }
        }

    override fun cancel() {
>>>    coroutineContext.cancel()
    }
}