Commit e6867087 authored by Matija Obreza's avatar Matija Obreza

Hibernate 2nd level cache with Hazelcast

parent 000cbdba
......@@ -16,6 +16,7 @@
package org.genesys2.spring.config;
import org.genesys2.spring.hazelcast.HazelcastCacheRegionFactoryWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
......@@ -26,6 +27,7 @@ import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ILock;
import com.hazelcast.core.ManagedContext;
import com.hazelcast.hibernate.HazelcastCacheRegionFactory;
import com.hazelcast.spring.context.SpringManagedContext;
@Configuration
......@@ -61,4 +63,29 @@ public abstract class HazelcastConfig {
return lock;
}
/**
* Loads an instance and sets the static field
* @param hazelcastCacheRegionFactory
*
* @return
*/
@Bean
public HazelcastCacheRegionFactoryWrapper hazelcastCacheRegionFactoryWrapper(HazelcastCacheRegionFactory hazelcastCacheRegionFactory) {
System.err.println("Hazelcast Cache");
HazelcastCacheRegionFactoryWrapper x = new HazelcastCacheRegionFactoryWrapper();
x.setHazelcastCacheRegionFactory(hazelcastCacheRegionFactory);
return x;
}
/**
* Injected into wrapper above
*
* @param instance
* @return
*/
@Bean
public HazelcastCacheRegionFactory hazelcastCacheRegionFactory(HazelcastInstance instance) {
System.err.println("HazelcastCacheRegionFactory");
return new HazelcastCacheRegionFactory(instance);
}
}
......@@ -21,13 +21,12 @@ import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.genesys2.spring.hazelcast.HazelcastCacheRegionFactoryWrapper;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
......@@ -80,11 +79,11 @@ public class SpringDataBaseConfig {
}
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception {
public LocalContainerEntityManagerFactoryBean entityManagerFactory(HazelcastCacheRegionFactoryWrapper wrp) throws Exception {
final LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource());
entityManager.setPersistenceUnitName("spring-jpa");
entityManager.setJpaVendorAdapter(hibernateJpaVendorAdapter());
entityManager.setJpaVendorAdapter(jpaVendorAdapter());
entityManager.setPersistenceProvider(new HibernatePersistenceProvider());
Properties jpaProperties = jpaProperties();
for (Object key : jpaProperties.keySet()) {
......@@ -111,7 +110,7 @@ public class SpringDataBaseConfig {
return jpaProp;
}
private HibernateJpaVendorAdapter hibernateJpaVendorAdapter() {
private HibernateJpaVendorAdapter jpaVendorAdapter() {
final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setShowSql(dbShowSql);
jpaVendorAdapter.setGenerateDdl(dbGenerateDdl);
......
/*
* Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.spring.hazelcast;
import java.util.Properties;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.spi.CollectionRegion;
import org.hibernate.cache.spi.EntityRegion;
import org.hibernate.cache.spi.NaturalIdRegion;
import org.hibernate.cache.spi.QueryResultsRegion;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.TimestampsRegion;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cfg.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.hazelcast.hibernate.HazelcastCacheRegionFactory;
@Component
public class HazelcastCacheRegionFactoryWrapper implements RegionFactory {
private static HazelcastCacheRegionFactory hazelcastCacheRegionFactory;
public HazelcastCacheRegionFactoryWrapper() {
}
public HazelcastCacheRegionFactoryWrapper(final Properties properties) {
this();
}
@Autowired(required = true)
public void setHazelcastCacheRegionFactory(HazelcastCacheRegionFactory hazelcastCacheRegionFactory) {
HazelcastCacheRegionFactoryWrapper.hazelcastCacheRegionFactory = hazelcastCacheRegionFactory;
}
@Override
public CollectionRegion buildCollectionRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException {
return hazelcastCacheRegionFactory.buildCollectionRegion(regionName, properties, metadata);
}
@Override
public void start(Settings settings, Properties properties) throws CacheException {
hazelcastCacheRegionFactory.start(settings, properties);
}
@Override
public void stop() {
hazelcastCacheRegionFactory.stop();
}
@Override
public boolean isMinimalPutsEnabledByDefault() {
return hazelcastCacheRegionFactory.isMinimalPutsEnabledByDefault();
}
@Override
public AccessType getDefaultAccessType() {
return hazelcastCacheRegionFactory.getDefaultAccessType();
}
@Override
public long nextTimestamp() {
return hazelcastCacheRegionFactory.nextTimestamp();
}
@Override
public EntityRegion buildEntityRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException {
return hazelcastCacheRegionFactory.buildEntityRegion(regionName, properties, metadata);
}
@Override
public NaturalIdRegion buildNaturalIdRegion(String regionName, Properties properties, CacheDataDescription metadata) throws CacheException {
return hazelcastCacheRegionFactory.buildNaturalIdRegion(regionName, properties, metadata);
}
@Override
public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties properties) throws CacheException {
return hazelcastCacheRegionFactory.buildQueryResultsRegion(regionName, properties);
}
@Override
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties) throws CacheException {
return hazelcastCacheRegionFactory.buildTimestampsRegion(regionName, properties);
}
}
#Hibernate specific properties
javax.persistence.sharedCache.mode=ENABLE_SELECTIVE
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.connection.charSet=utf8
hibernate.connection.autocommit=false
hibernate.jdbc.batch_size=50
hibernate.jdbc.batch_size=20
hibernate.order_updates=true
hibernate.order_inserts=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
hibernate.cache.use_minimal_puts=true
#MEM:hibernate.cache.region.factory_class=org.hibernate.testing.cache.CachingRegionFactory
#hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory
hibernate.cache.region.factory_class=org.genesys2.spring.hazelcast.HazelcastCacheRegionFactoryWrapper
hibernate.cache.use_structured_entries=false
hibernate.generate_statistics=false
......@@ -25,7 +25,7 @@ db.url=jdbc:mysql://localhost/genesyslive?useUnicode=true&characterEncoding=UTF-
db.driverClassName=com.mysql.jdbc.Driver
db.username=root
db.password=
db.showSql=false
db.showSql=true
db.hbm2ddl=false
c3p0.acquireIncrement=1
......
......@@ -21,6 +21,7 @@ import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.genesys2.spring.hazelcast.HazelcastCacheRegionFactoryWrapper;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.junit.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -68,7 +69,7 @@ public class JpaDataConfig {
}
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception {
public LocalContainerEntityManagerFactoryBean entityManagerFactory(HazelcastCacheRegionFactoryWrapper wrp) throws Exception {
final LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setDataSource(dataSource());
......@@ -77,10 +78,13 @@ public class JpaDataConfig {
bean.setPersistenceProvider(new HibernatePersistenceProvider());
final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setShowSql(env.getProperty("db.show.sql", Boolean.TYPE, true));
jpaVendorAdapter.setGenerateDdl(env.getProperty("db.generate.ddl", Boolean.TYPE, true));
bean.setJpaProperties(jpaProperties());
jpaVendorAdapter.setShowSql(env.getProperty("db.showSql", Boolean.TYPE, true));
jpaVendorAdapter.setGenerateDdl(env.getProperty("db.hbm2ddl", Boolean.TYPE, true));
Properties jpaProperties = jpaProperties();
for (Object key : jpaProperties.keySet()) {
System.err.println("JPA: " + key + " = " + jpaProperties.get(key));
}
bean.setJpaProperties(jpaProperties);
bean.setJpaVendorAdapter(jpaVendorAdapter);
......
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.test;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.genesys2.spring.hazelcast.HazelcastCacheRegionFactoryWrapper;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.junit.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Ignore
@Configuration
@PropertySource("classpath:/spring/spring-database.properties")
@EnableJpaRepositories(basePackages = { "org.genesys2.server.persistence.acl", "org.genesys2.server.persistence.domain" }, repositoryImplementationPostfix = "CustomImpl", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
@EnableTransactionManagement
@TransactionConfiguration(defaultRollback = true, transactionManager = "transactionManager")
public class JpaRealDataConfig {
@Autowired
private Environment env;
@Bean(name = "dataSource")
public DataSource dataSource() throws Exception {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("db.driverClassName"));
dataSource.setUrl(env.getProperty("db.url"));
dataSource.setUsername(env.getProperty("db.username"));
dataSource.setPassword(env.getProperty("db.password"));
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(HazelcastCacheRegionFactoryWrapper wrp) throws Exception {
final LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setDataSource(dataSource());
bean.setPersistenceUnitName("spring-jpa");
bean.setPackagesToScan("org.genesys2.server.model");
bean.setPersistenceProvider(new HibernatePersistenceProvider());
final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setShowSql(env.getProperty("db.showSql", Boolean.TYPE, true));
jpaVendorAdapter.setGenerateDdl(env.getProperty("db.hbm2ddl", Boolean.TYPE, true));
Properties jpaProperties = jpaProperties();
// for (Object key : jpaProperties.keySet()) {
// System.err.println("JPA: " + key + " = " + jpaProperties.get(key));
// }
bean.setJpaProperties(jpaProperties);
bean.setJpaVendorAdapter(jpaVendorAdapter);
return bean;
}
@Bean
public PersistenceExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
@Bean(name = "transactionManager")
public JpaTransactionManager jpaTransactionManager(EntityManagerFactory entityManagerFactory) throws Exception {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setDataSource(dataSource());
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
private Properties jpaProperties() throws Exception {
Properties jpaProp = new Properties();
jpaProp.load(getClass().getResourceAsStream("/spring/hibernate.properties"));
return jpaProp;
}
}
......@@ -30,3 +30,5 @@ log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=info, stdout
log4j.category.org.genesys2=debug
#log4j.category.org.hibernate.cache=debug
#log4j.category.org.hibernate.jdbc=debug
#Hibernate specific properties
javax.persistence.sharedCache.mode=ENABLE_SELECTIVE
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.charSet=utf8
hibernate.connection.autocommit=false
hibernate.jdbc.batch_size=50
hibernate.jdbc.batch_size=15
hibernate.order_updates=true
hibernate.order_inserts=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
hibernate.cache.use_minimal_puts=true
#MEM:hibernate.cache.region.factory_class=org.hibernate.testing.cache.CachingRegionFactory
#hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory
hibernate.cache.region.factory_class=org.genesys2.spring.hazelcast.HazelcastCacheRegionFactoryWrapper
hibernate.cache.use_structured_entries=true
hibernate.generate_statistics=false
#-------------------------------------------------------------------------------
# Copyright 2014 Global Crop Diversity Trust
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
base.url=http://localhost:8080
#DB connection properties
#In-memory HSQLDB
db.url=jdbc:mysql://localhost/genesyslive?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
db.driverClassName=com.mysql.jdbc.Driver
db.username=root
db.password=
db.showSql=true
db.hbm2ddl=false
# reCAPTCHA API
captcha.privateKey=A
captcha.publicKey=B
lucene.indexDir=./lucene/
# paths
download.files.dir=./data/
# ITPGRFA Easy-SMTA account (if you have one)
itpgrfa.easysmta.username=
itpgrfa.easysmta.password=
# GA Account
google.analytics.account=
# CDN
cdn.server=https://s1.cdn.genesys-pgr.org
cdn.base=${cdn.server}/html
cdn.flags.url=${cdn.base}/flags
# TileServer CDN
tileserver.cdn=''
# Content creation on startup
auto.createContent=false
scheduler.tokens.cleanup.hours=1
# Mail configuration
mail.user.from=test@localhost
mail.requests.to=test@localhost
mail.async=true
mail.debug.message= Email has been sent succesfully\n\
Details:\n\
From:%s\n\
To:%s\n\
Subject: %s\n\
Text: \n==================================\n\n\
%s\
\n\n==================================
# Hazelcast
hazelcast.instanceName=genesys
hazelcast.name=genesys
hazelcast.password=hazelcasts
hazelcast.port=5701
# AWS Autodetection
hazelcast.aws.access-key=
hazelcast.aws.secret-key=
hazelcast.aws.region=eu-west-1
hazelcast.aws.security-group=sg-hazelcast
# TileServer Cache
cache.tileserver.max-size=1000
cache.tileserver.time-to-live-seconds=300
cache.tileserver.max-idle-seconds=0
cache.tileserver.eviction-policy=LRU
transifex.project=genesys-dev
transifex.username=Alexandr19011990
transifex.password=Alexandr19011990
transifex.min.translated=80
transifex.base.api.url=https://www.transifex.com/api/2/
transifex.hook.key=this-is-a-test-hook-key
#TODO perhaps, Velocity template?
transifex.content.template=<html xmlns="http://www.w3.org/1999/xhtml">\
<head>\
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>\
<title>%s</title>\
</head>\
<body>%s</body>\
</html>
......@@ -23,7 +23,7 @@ db.driverClassName = org.hsqldb.jdbc.JDBCDriver
db.username = sa
db.password =
db.showSql=true
db.hbm2ddl=create
db.hbm2ddl=true
# reCAPTCHA API
captcha.privateKey=A
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment