BAEL-1419: Guide to cockroachDB in Java (#3325)
* adding CockroachDB code * Fixed database name * Added handling transaction examples
This commit is contained in:
parent
8108875723
commit
fa92d1db3d
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [Guide to CockroachDB in Java](http://www.baeldung.com/)
|
|
@ -0,0 +1,74 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>java-cockroachdb</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<postgresql.version>42.1.4</postgresql.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>Central</id>
|
||||
<name>Central</name>
|
||||
<url>http://repo1.maven.org/maven2/</url>
|
||||
<layout>default</layout>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.cockroachdb.domain;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class Article {
|
||||
|
||||
private UUID id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String author;
|
||||
|
||||
public Article(UUID id, String title, String author) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,172 @@
|
|||
package com.baeldung.cockroachdb.repository;
|
||||
|
||||
import com.baeldung.cockroachdb.domain.Article;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Repository for the articles table related operations
|
||||
*/
|
||||
public class ArticleRepository {
|
||||
|
||||
private static final String TABLE_NAME = "articles";
|
||||
private Connection connection;
|
||||
|
||||
public ArticleRepository(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the articles table.
|
||||
*/
|
||||
public void createTable() throws SQLException {
|
||||
StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(TABLE_NAME)
|
||||
.append("(id uuid PRIMARY KEY, ")
|
||||
.append("title string,")
|
||||
.append("author string)");
|
||||
|
||||
final String query = sb.toString();
|
||||
Statement stmt = connection.createStatement();
|
||||
stmt.execute(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alter the articles table adding a column
|
||||
*
|
||||
* @param columnName Column name of the additional column
|
||||
* @param columnType Column type of the additional column
|
||||
* @throws SQLException
|
||||
*/
|
||||
public void alterTable(String columnName, String columnType) throws SQLException {
|
||||
StringBuilder sb = new StringBuilder("ALTER TABLE ").append(TABLE_NAME)
|
||||
.append(" ADD ")
|
||||
.append(columnName)
|
||||
.append(" ")
|
||||
.append(columnType);
|
||||
|
||||
final String query = sb.toString();
|
||||
Statement stmt = connection.createStatement();
|
||||
stmt.execute(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new article in the articles table
|
||||
*
|
||||
* @param article New article to insert
|
||||
* @throws SQLException
|
||||
*/
|
||||
public void insertArticle(Article article) throws SQLException {
|
||||
StringBuilder sb = new StringBuilder("INSERT INTO ").append(TABLE_NAME)
|
||||
.append("(id, title, author) ")
|
||||
.append("VALUES (?,?,?)");
|
||||
|
||||
final String query = sb.toString();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
preparedStatement.setString(1, article.getId().toString());
|
||||
preparedStatement.setString(2, article.getTitle());
|
||||
preparedStatement.setString(3, article.getAuthor());
|
||||
preparedStatement.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Select article by Title
|
||||
*
|
||||
* @param title title of the article to retrieve
|
||||
* @return article with the given title
|
||||
* @throws SQLException
|
||||
*/
|
||||
public Article selectByTitle(String title) throws SQLException {
|
||||
StringBuilder sb = new StringBuilder("SELECT * FROM ").append(TABLE_NAME)
|
||||
.append(" WHERE title = ?");
|
||||
|
||||
final String query = sb.toString();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
preparedStatement.setString(1, title);
|
||||
|
||||
try (ResultSet rs = preparedStatement.executeQuery()) {
|
||||
|
||||
List<Article> articles = new ArrayList<>();
|
||||
|
||||
while (rs.next()) {
|
||||
Article article = new Article(
|
||||
UUID.fromString(rs.getString("id")),
|
||||
rs.getString("title"),
|
||||
rs.getString("author")
|
||||
);
|
||||
articles.add(article);
|
||||
}
|
||||
return articles.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Select all the articles
|
||||
*
|
||||
* @return list of all articles
|
||||
* @throws SQLException
|
||||
*/
|
||||
public List<Article> selectAll() throws SQLException {
|
||||
StringBuilder sb = new StringBuilder("SELECT * FROM ").append(TABLE_NAME);
|
||||
|
||||
final String query = sb.toString();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
try (ResultSet rs = preparedStatement.executeQuery()) {
|
||||
|
||||
List<Article> articles = new ArrayList<>();
|
||||
|
||||
while (rs.next()) {
|
||||
Article article = new Article(
|
||||
UUID.fromString(rs.getString("id")),
|
||||
rs.getString("title"),
|
||||
rs.getString("author")
|
||||
);
|
||||
articles.add(article);
|
||||
}
|
||||
return articles;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete article by title
|
||||
*
|
||||
* @param title title of the article to delete
|
||||
* @throws SQLException
|
||||
*/
|
||||
public void deleteArticleByTitle(String title) throws SQLException {
|
||||
StringBuilder sb = new StringBuilder("DELETE FROM ").append(TABLE_NAME)
|
||||
.append(" WHERE title = ?");
|
||||
|
||||
final String query = sb.toString();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
preparedStatement.setString(1, title);
|
||||
preparedStatement.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all rows in the table
|
||||
*
|
||||
* @throws SQLException
|
||||
*/
|
||||
public void truncateTable() throws SQLException {
|
||||
StringBuilder sb = new StringBuilder("TRUNCATE TABLE ").append(TABLE_NAME);
|
||||
|
||||
final String query = sb.toString();
|
||||
Statement stmt = connection.createStatement();
|
||||
stmt.execute(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete table
|
||||
*/
|
||||
public void deleteTable() throws SQLException {
|
||||
StringBuilder sb = new StringBuilder("DROP TABLE IF EXISTS ").append(TABLE_NAME);
|
||||
|
||||
final String query = sb.toString();
|
||||
Statement stmt = connection.createStatement();
|
||||
stmt.execute(query);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,208 @@
|
|||
package com.baeldung.cockroachdb;
|
||||
|
||||
import com.baeldung.cockroachdb.domain.Article;
|
||||
import com.baeldung.cockroachdb.repository.ArticleRepository;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.postgresql.util.PSQLException;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ArticleRepositoryIntegrationTest {
|
||||
|
||||
private static final String TABLE_NAME = "articles";
|
||||
|
||||
private Connection con;
|
||||
private ArticleRepository articleRepository;
|
||||
|
||||
@Before
|
||||
public void connect() throws Exception {
|
||||
Class.forName("org.postgresql.Driver");
|
||||
con = DriverManager.getConnection("jdbc:postgresql://localhost:26257/testdb", "user17", "qwerty");
|
||||
|
||||
articleRepository = new ArticleRepository(con);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingTable_thenCreatedCorrectly() throws Exception {
|
||||
articleRepository.deleteTable();
|
||||
articleRepository.createTable();
|
||||
|
||||
PreparedStatement preparedStatement = con.prepareStatement("SHOW TABLES");
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
List<String> tables = new ArrayList<>();
|
||||
while (resultSet.next()) {
|
||||
tables.add(resultSet.getString("Table"));
|
||||
}
|
||||
|
||||
assertTrue(tables.stream().anyMatch(t -> t.equals(TABLE_NAME)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAlteringTheTable_thenColumnAddedCorrectly() throws SQLException {
|
||||
articleRepository.deleteTable();
|
||||
articleRepository.createTable();
|
||||
|
||||
String columnName = "creationdate";
|
||||
articleRepository.alterTable(columnName, "DATE");
|
||||
|
||||
String query = "SHOW COLUMNS FROM " + TABLE_NAME;
|
||||
PreparedStatement preparedStatement = con.prepareStatement(query);
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
List<String> columns = new ArrayList<>();
|
||||
while (resultSet.next()) {
|
||||
columns.add(resultSet.getString("Field"));
|
||||
}
|
||||
|
||||
assertTrue(columns.stream().anyMatch(c -> c.equals(columnName)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsertingNewArticle_thenArticleExists() throws SQLException {
|
||||
articleRepository.deleteTable();
|
||||
articleRepository.createTable();
|
||||
|
||||
String title = "Guide to CockroachDB in Java";
|
||||
String author = "baeldung";
|
||||
Article article = new Article(UUID.randomUUID(), title, author);
|
||||
articleRepository.insertArticle(article);
|
||||
|
||||
Article savedArticle = articleRepository.selectByTitle(title);
|
||||
assertEquals(article.getTitle(), savedArticle.getTitle());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSelectingAllArticles_thenAllArticlesAreReturned() throws SQLException {
|
||||
articleRepository.deleteTable();
|
||||
articleRepository.createTable();
|
||||
|
||||
Article article = new Article(UUID.randomUUID(), "Guide to CockroachDB in Java", "baeldung");
|
||||
articleRepository.insertArticle(article);
|
||||
|
||||
article = new Article(UUID.randomUUID(), "A Guide to MongoDB with Java", "baeldung");
|
||||
articleRepository.insertArticle(article);
|
||||
|
||||
List<Article> savedArticles = articleRepository.selectAll();
|
||||
|
||||
assertEquals(2, savedArticles.size());
|
||||
assertTrue(savedArticles.stream().anyMatch(a -> a.getTitle().equals("Guide to CockroachDB in Java")));
|
||||
assertTrue(savedArticles.stream().anyMatch(a -> a.getTitle().equals("A Guide to MongoDB with Java")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeletingArticleByTtile_thenArticleIsDeleted() throws SQLException {
|
||||
articleRepository.deleteTable();
|
||||
articleRepository.createTable();
|
||||
|
||||
Article article = new Article(UUID.randomUUID(), "Guide to CockroachDB in Java", "baeldung");
|
||||
articleRepository.insertArticle(article);
|
||||
|
||||
article = new Article(UUID.randomUUID(), "A Guide to MongoDB with Java", "baeldung");
|
||||
articleRepository.insertArticle(article);
|
||||
|
||||
articleRepository.deleteArticleByTitle("A Guide to MongoDB with Java");
|
||||
|
||||
List<Article> savedArticles = articleRepository.selectAll();
|
||||
assertEquals(1, savedArticles.size());
|
||||
assertTrue(savedArticles.stream().anyMatch(a -> a.getTitle().equals("Guide to CockroachDB in Java")));
|
||||
assertFalse(savedArticles.stream().anyMatch(a -> a.getTitle().equals("A Guide to MongoDB with Java")));
|
||||
}
|
||||
|
||||
@Test(expected = PSQLException.class)
|
||||
public void whenDeletingATable_thenExceptionIfAccessed() throws SQLException {
|
||||
articleRepository.createTable();
|
||||
articleRepository.deleteTable();
|
||||
|
||||
StringBuilder sb = new StringBuilder("SELECT * FROM ").append(TABLE_NAME);
|
||||
|
||||
final String query = sb.toString();
|
||||
PreparedStatement preparedStatement = con.prepareStatement(query);
|
||||
preparedStatement.executeQuery();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTruncatingATable_thenEmptyTable() throws SQLException {
|
||||
articleRepository.deleteTable();
|
||||
articleRepository.createTable();
|
||||
|
||||
Article article = new Article(UUID.randomUUID(), "Guide to CockroachDB in Java", "baeldung");
|
||||
articleRepository.insertArticle(article);
|
||||
|
||||
article = new Article(UUID.randomUUID(), "A Guide to MongoDB with Java", "baeldung");
|
||||
articleRepository.insertArticle(article);
|
||||
|
||||
articleRepository.truncateTable();
|
||||
|
||||
List<Article> savedArticles = articleRepository.selectAll();
|
||||
assertEquals(0, savedArticles.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsertingTwoArticlesWithSamePrimaryKeyInASingleTransaction_thenRollback() throws SQLException {
|
||||
articleRepository.deleteTable();
|
||||
articleRepository.createTable();
|
||||
|
||||
try {
|
||||
con.setAutoCommit(false);
|
||||
|
||||
UUID articleId = UUID.randomUUID();
|
||||
|
||||
Article article = new Article(articleId, "Guide to CockroachDB in Java", "baeldung");
|
||||
articleRepository.insertArticle(article);
|
||||
|
||||
article = new Article(articleId, "A Guide to MongoDB with Java", "baeldung");
|
||||
articleRepository.insertArticle(article);
|
||||
|
||||
con.commit();
|
||||
} catch (Exception e) {
|
||||
con.rollback();
|
||||
} finally {
|
||||
con.setAutoCommit(true);
|
||||
}
|
||||
|
||||
List<Article> savedArticles = articleRepository.selectAll();
|
||||
assertEquals(0, savedArticles.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsertingTwoArticlesInASingleTransaction_thenInserted() throws SQLException {
|
||||
articleRepository.deleteTable();
|
||||
articleRepository.createTable();
|
||||
|
||||
try {
|
||||
con.setAutoCommit(false);
|
||||
|
||||
Article article = new Article(UUID.randomUUID(), "Guide to CockroachDB in Java", "baeldung");
|
||||
articleRepository.insertArticle(article);
|
||||
|
||||
article = new Article(UUID.randomUUID(), "A Guide to MongoDB with Java", "baeldung");
|
||||
articleRepository.insertArticle(article);
|
||||
|
||||
con.commit();
|
||||
} catch (Exception e) {
|
||||
con.rollback();
|
||||
} finally {
|
||||
con.setAutoCommit(true);
|
||||
}
|
||||
|
||||
List<Article> savedArticles = articleRepository.selectAll();
|
||||
assertEquals(2, savedArticles.size());
|
||||
assertTrue(savedArticles.stream().anyMatch(a -> a.getTitle().equals("Guide to CockroachDB in Java")));
|
||||
assertTrue(savedArticles.stream().anyMatch(a -> a.getTitle().equals("A Guide to MongoDB with Java")));
|
||||
}
|
||||
|
||||
@After
|
||||
public void disconnect() throws SQLException {
|
||||
articleRepository = null;
|
||||
con.close();
|
||||
con = null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue