Created
September 24, 2025 22:03
-
-
Save hakimkal/cb7e7a3b6f7d9c792b7d673f3c0c0b40 to your computer and use it in GitHub Desktop.
ScyllaDbConfig for Springboot Project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.datastax.oss.driver.api.core.CqlSession; | |
| import com.datastax.oss.driver.api.core.CqlSessionBuilder; | |
| import com.datastax.oss.driver.api.core.config.DefaultDriverOption; | |
| import com.datastax.oss.driver.api.core.config.DriverConfigLoader; | |
| import org.springframework.beans.factory.annotation.Value; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.context.annotation.Lazy; | |
| import java.net.InetSocketAddress; | |
| import java.time.Duration; | |
| @Configuration | |
| public class ScyllaDbConfig { | |
| // | |
| @Value("${scylladb.contact-points}") | |
| private String contactPoint; | |
| @Value("${scylladb.port}") | |
| private int port; | |
| @Value("${scylladb.username}") | |
| private String username; | |
| @Value("${scylladb.password}") | |
| private String password; | |
| @Value("${scylladb.local-datacenter}") | |
| private String localDatacenter; | |
| @Bean | |
| @Lazy | |
| public CqlSession session() { | |
| DriverConfigLoader loader = DriverConfigLoader.programmaticBuilder() | |
| .withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(15)) | |
| .build(); | |
| CqlSessionBuilder builder = CqlSession.builder() | |
| .addContactPoint(new InetSocketAddress(contactPoint, port)) | |
| .withConfigLoader(loader); | |
| // Fallback to "datacenter1" if nothing is provided | |
| if (localDatacenter != null && !localDatacenter.isBlank()) { | |
| builder.withLocalDatacenter(localDatacenter); | |
| } else { | |
| builder.withLocalDatacenter("datacenter1"); | |
| } | |
| // Only set auth if both username and password are provided | |
| if (username != null && !username.isBlank() && | |
| password != null && !password.isBlank()) { | |
| builder.withAuthCredentials(username, password); | |
| } | |
| return builder.build(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment