1

I am new to java world (spring hibernate etc). I am doing PoC for cache implementation for cluster environment. I simply want to reduce hits in database and hence want database objects to be stored in cache. I got to know from internet that I need to use transnational cache using JPA with combination of ehcache and terracotta server (BigMemory Max). Ehcache can easily recognise some of transnational management libraries so I used atomikos. Please correct me if I am at wrong path. I have below code to achieve this but I see 0 cache objects in management tool.

1) ehcache.xml

<cache name="com.example.pocehcache.model.Note"
 maxEntriesLocalHeap="500"
 eternal="false"
 copyOnRead="true"
 copyOnWrite="true"
 transactionalMode="xa">
 <persistence strategy="distributed"/>
 <terracotta consistency="strong"/>
</cache>
<terracottaConfig url="localhost:9510" />

2) Application.properties

# application.properties
spring.cache.ehcache.config=classpath:ehcache.xml
com.atomikos.icatch.threaded_2pc=false
spring.jpa.open-in-view=true


## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=password
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect


# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=create-drop


server.port = 8090

3) Controller class (took sample from internet)

@RestController
@RequestMapping("/api")
public class NoteController {


 @Autowired
 NoteRepository noteRepository;


 // Get All Notes
 @GetMapping("/notes")
 @Transactional
 public List<Note> getAllNotes() {
 List<Note> notes = noteRepository.findAll();
 return notes;
 }


 // Create a new Note
 @PostMapping("/notes")
 @Transactional
 public Note createNote(@Valid @RequestBody Note note) {
 return noteRepository.save(note);
 }


...

4) Application class

@SpringBootApplication
@EnableJpaAuditing
@EnableAutoConfiguration
@EnableCaching
public class PocEhcacheApplication {
 public static void main(String[] args) {
 SpringApplication.run(PocEhcacheApplication.class, args);
}

5) Model (with setters, getters, equals and hashCode methods)

    @Entity
    @Table(name = "notes")
    @EntityListeners(AuditingEntityListener.class)
    @JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, 
     allowGetters = true)
    public class Note implements Serializable {
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     private Long id;


     @NotBlank
     private String title;


     @NotBlank
     private String content;


     @Column(nullable = false, updatable = false)
     @Temporal(TemporalType.TIMESTAMP)
     @CreatedDate
     private Date createdAt;


     @Column(nullable = false)
     @Temporal(TemporalType.TIMESTAMP)
     @LastModifiedDate
     private Date updatedAt;
    ...

6) Terracotta management console o objects in cache in Terracotta management console

Any help or pointer would be a great support. I am using BigMemory Max-4.3.4.3.15 and relevant jars for application in the class path. Please let me know in case any other information is required.

Note: If I put objects directly into BigMemory it works (bigMemory.put(new Element(note.getId(), note))). I could able to see number of objects in terracotta console.

Thank you very much!

snesh
  • 71
  • 7
  • Welcome to `Stack Overflow`, Please consider using a [**MCVE** (**M**inimal **C**omplete **V**erifiable **E**xample)](https://stackoverflow.com/help/mcve) because it's way to much to read and understand! – Hille Jan 09 '18 at 09:53

0 Answers0