{"id":28763843,"url":"https://github.com/cockroachdb/cockroach-transaction-demo","last_synced_at":"2025-07-10T08:38:32.292Z","repository":{"id":239986088,"uuid":"801161569","full_name":"cockroachdb/cockroach-transaction-demo","owner":"cockroachdb","description":"Demonstrate best practices in using transactions in a Spring Boot application","archived":false,"fork":false,"pushed_at":"2024-06-03T14:31:59.000Z","size":245,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":48,"default_branch":"main","last_synced_at":"2025-06-13T14:02:30.690Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cockroachdb.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-15T17:58:16.000Z","updated_at":"2024-08-19T18:09:07.000Z","dependencies_parsed_at":"2024-06-02T23:27:48.452Z","dependency_job_id":"bbb36f03-fdb5-42ba-a3b7-92cd54bdb324","html_url":"https://github.com/cockroachdb/cockroach-transaction-demo","commit_stats":null,"previous_names":["cockroachdb/cockroach-transaction-demo"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cockroachdb/cockroach-transaction-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cockroachdb%2Fcockroach-transaction-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cockroachdb%2Fcockroach-transaction-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cockroachdb%2Fcockroach-transaction-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cockroachdb%2Fcockroach-transaction-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cockroachdb","download_url":"https://codeload.github.com/cockroachdb/cockroach-transaction-demo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cockroachdb%2Fcockroach-transaction-demo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260326793,"owners_count":22992388,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-06-17T09:10:35.220Z","updated_at":"2025-06-17T09:10:36.605Z","avatar_url":"https://github.com/cockroachdb.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"== CockroachDB Transaction Best Practices\n\nThis repository contains a sample application that illustrates some best practices regarding transaction handling with CockroachDB.\n\nThe project included uses Spring Boot + Spring Data JPA.\n\nBTW, if you're new to CockroachDB and how it works, be sure to check out https://thesecretlivesofdata.com/raft/[The Raft protocol], the mechanism Cockroach uses to seamlessly replicate and manage your data in a truly distributed fashion.\n\n=== Database Schema + Sample Data\n\n[source,sql]\n----\n-- drop tables\nDROP TABLE IF EXISTS items;\n\n-- re-create tables\nCREATE TABLE items (item_id UUID PRIMARY KEY DEFAULT gen_random_uuid (),\n                    name STRING,\n                    description STRING,\n                    price DECIMAL NOT NULL,\n                    quantity INT DEFAULT 0);\n\nINSERT INTO items (name, description, quantity, price) VALUES ('foo', 'fang', 200, 0.0);\n----\n\n=== JPA Structure\n\nThe following Java class shows the JPA structure used in the app:\n\n[source,java]\n----\n@Entity\n@Table(name = \"items\")\nclass Item {\n\n\t@Id\n\t@GeneratedValue(strategy = GenerationType.UUID) //\n\tprivate UUID itemId;\n\n\tprivate String name;\n\n\tprivate String description;\n\n\tprivate int quantity;\n\n\tprivate double price;\n\n    ...\n}\n----\n\nTo see the rest of the project, feel free to clone this repository and open it up inside your favorite IDE.\n\n=== Read Committed Transaction\n\nThe following diagram illustrates a simple scenario that involves *read committed* transactions, and what can happen when you have competing transactions.\n\nimage::01-read-committed-competing-transaction.png[Read Committed Flow]\n\nSee link:READ_COMMITTED.md[]\n\n=== Serializable Transaction\n\nThe following diagram illustrates a simple scenario that involves *serializable* transactions, and how competing transactions can translate into an exception, which can be handled.\n\nimage::02-serializable-competing-transaction.png[Serializable Flow]\n\nSee link:SERIALIZABLE.md[]\n\n=== SELECT FOR UPDATE scenario\n\n`SELECT FOR UPDATE` provides the means to reduce retry errors from occurring.\n\nimage::04-sql-for-update.png[SELECT FOR UPDATE scenario]\n\nSee link:SELECT_FOR_UPDATE.adoc[]\n\n=== To run this application\n\nFor this app to work, you must:\n\n. Type `cockroach start-single-node --insecure` to launch CockroachDB.\n. In another shell, type `cockroach sql --insecure` to creation a SQL session.\n. Type `drop database if exists kwikshoppr cascade; create database kwikshoppr;` to create the database for this app.\n. In the SQL session, type `create database kwikshoppr;` to create the database for this app.\n. In the SQL session, type `SET CLUSTER SETTING sql.txn.read_committed_isolation.enabled = 'true';` to enable READ_COMMITTED transactions.\n\nIf you need to restart, type `drop database if exists kwikshoppr cascade;` in the SQL session and then create the database again.\n\nYou can NOT run the application without the database existing, because the name of the database (`kwikshoppr`) is embedded inside `application.properties` as part of the JDBC connection string.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcockroachdb%2Fcockroach-transaction-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcockroachdb%2Fcockroach-transaction-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcockroachdb%2Fcockroach-transaction-demo/lists"}