-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support bit-reversed sequences on the emulator (#984)
* fix: JSON column DDL was generated as STRING * feat: support bit-reversed sequences on the emulator Bit-reversed sequences require the use of a read/write transaction. Hibernate starts a second read/write transaction when it needs to fetch new identifiers, and this transaction is aborted when it is running on the emulator. The PooledBitReversedSequenceGenerator now solves this by rolling back that transaction after getting the sequence values. This is semantically equivalent to committing the transaction, as the state of the sequence is durably changed regardless whether the transaction is committed or rolled back. Fixes #983 * test: use JSON null values as well
- Loading branch information
Showing
11 changed files
with
295 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
113 changes: 113 additions & 0 deletions
113
...ta-jpa-full-sample/src/main/java/com/google/cloud/spanner/sample/entities/TicketSale.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2019-2024 Google LLC | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||
*/ | ||
|
||
package com.google.cloud.spanner.sample.entities; | ||
|
||
import com.google.cloud.spanner.hibernate.PooledBitReversedSequenceStyleGenerator; | ||
import com.google.cloud.spanner.hibernate.types.SpannerStringArray; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.ManyToOne; | ||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import org.hibernate.annotations.GenericGenerator; | ||
import org.hibernate.annotations.Parameter; | ||
import org.hibernate.annotations.Type; | ||
import org.hibernate.id.enhanced.SequenceStyleGenerator; | ||
|
||
/** {@link TicketSale} shows how to use bit-reversed sequences to generate primary key values. */ | ||
@Entity | ||
public class TicketSale extends AbstractEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ticketSaleId") | ||
@GenericGenerator( | ||
name = "ticketSaleId", | ||
// Use this custom strategy to ensure the use of a bit-reversed sequence that is compatible | ||
// with batching multiple inserts. See also | ||
// https://docs.jboss.org/hibernate/orm/6.3/userguide/html_single/Hibernate_User_Guide.html#batch. | ||
type = PooledBitReversedSequenceStyleGenerator.class, | ||
parameters = { | ||
// Use a separate sequence name for each entity. | ||
@Parameter(name = SequenceStyleGenerator.SEQUENCE_PARAM, value = "ticket_sale_seq"), | ||
// The increment_size is not actually set on the sequence that is created, but is used to | ||
// generate a SELECT query that fetches this number of identifiers at once. | ||
@Parameter(name = SequenceStyleGenerator.INCREMENT_PARAM, value = "200"), | ||
@Parameter(name = SequenceStyleGenerator.INITIAL_PARAM, value = "50000"), | ||
// Add any range that should be excluded by the generator if your table already | ||
// contains existing values that have been generated by other generators. | ||
@Parameter(name = PooledBitReversedSequenceStyleGenerator.EXCLUDE_RANGE_PARAM, | ||
value = "[1,1000]"), | ||
}) | ||
private long id; | ||
|
||
@ManyToOne(optional = false, fetch = FetchType.LAZY) | ||
private Concert concert; | ||
|
||
private String customerName; | ||
|
||
private BigDecimal price; | ||
|
||
@Type(SpannerStringArray.class) | ||
private List<String> seats; | ||
|
||
public TicketSale() { | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public Concert getConcert() { | ||
return concert; | ||
} | ||
|
||
public void setConcert(Concert concert) { | ||
this.concert = concert; | ||
} | ||
|
||
public String getCustomerName() { | ||
return customerName; | ||
} | ||
|
||
public void setCustomerName(String customerName) { | ||
this.customerName = customerName; | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
|
||
public List<String> getSeats() { | ||
return seats; | ||
} | ||
|
||
public void setSeats(List<String> seats) { | ||
this.seats = seats; | ||
} | ||
} |
This file contains 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
26 changes: 26 additions & 0 deletions
26
...sample/src/main/java/com/google/cloud/spanner/sample/repository/TicketSaleRepository.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2019-2024 Google LLC | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||
*/ | ||
|
||
package com.google.cloud.spanner.sample.repository; | ||
|
||
import com.google.cloud.spanner.sample.entities.TicketSale; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface TicketSaleRepository extends JpaRepository<TicketSale, Long> { | ||
|
||
} |
This file contains 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
Oops, something went wrong.