From 92ad3eed8080c63eb206665953cd050ba32373d5 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Thu, 9 Jun 2011 03:53:04 +0800 Subject: [PATCH] HHH-6297 remove legacy cache api --- .../manual/en-US/content/performance.xml | 36 +- etc/hibernate.properties | 12 +- etc/hibernate.properties.template | 975 +++++++++--------- .../org/hibernate/MultiTenancyStrategy.java | 26 +- .../cache/internal/HashtableCache.java | 114 -- .../cache/internal/NoCacheProvider.java | 86 -- .../hibernate/cache/internal/Timestamper.java | 29 +- .../CollectionAccessStrategyAdapter.java | 114 -- .../bridge/CollectionRegionAdapter.java | 81 -- .../bridge/EntityAccessStrategyAdapter.java | 132 --- .../internal/bridge/EntityRegionAdapter.java | 80 -- .../RegionFactoryCacheProviderBridge.java | 125 --- .../bridge/TimestampsRegionAdapter.java | 39 - .../spi/AbstractJndiBoundCacheProvider.java | 111 -- .../java/org/hibernate/cache/spi/Cache.java | 132 --- .../cache/spi/CacheConcurrencyStrategy.java | 197 ---- .../hibernate/cache/spi/CacheProvider.java | 68 -- .../cache/spi/NonstrictReadWriteCache.java | 178 ---- .../hibernate/cache/spi/OptimisticCache.java | 87 -- .../hibernate/cache/spi/ReadOnlyCache.java | 165 --- .../hibernate/cache/spi/ReadWriteCache.java | 500 --------- .../hibernate/cache/spi/RegionFactory.java | 7 +- .../cache/spi/TransactionalCache.java | 183 ---- .../access/EntityRegionAccessStrategy.java | 33 - .../org/hibernate/cfg/AvailableSettings.java | 7 - .../java/org/hibernate/cfg/Environment.java | 5 - .../cfg/ExternalSessionFactoryConfig.java | 12 +- .../org/hibernate/cfg/SettingsFactory.java | 86 +- .../hibernate/jmx/HibernateServiceMBean.java | 4 +- .../org/hibernate/test/legacy/CacheTest.java | 159 --- .../schema/SchemaBasedMultiTenancyTest.java | 4 +- .../src/test/resources/hibernate.properties | 2 +- .../src/test/resources/log4j.properties | 2 +- .../org/hibernate/cache/internal/EhCache.java | 274 ----- .../cache/internal/EhCacheProvider.java | 171 --- .../internal/SingletonEhCacheProvider.java | 185 ---- .../test/cache/ehcache/EhCacheRegionTest.java | 16 +- .../test/cache/ehcache/EhCacheTest.java | 11 +- .../ejb/test/pack/cfgxmlpar/hibernate.cfg.xml | 2 +- .../defaultpar/META-INF/persistence.xml | 2 +- .../defaultpar_1_0/META-INF/persistence.xml | 2 +- .../excludehbmpar/META-INF/persistence.xml | 2 +- .../explicitpar/META-INF/persistence.xml | 2 +- .../explodedpar/META-INF/persistence.xml | 2 +- .../overridenpar/META-INF/persistence.xml | 2 +- .../space par/META-INF/persistence.xml | 2 +- .../WEB-INF/classes/META-INF/persistence.xml | 2 +- .../src/test/resources/hibernate.properties | 2 +- .../org/hibernate/ejb/test/hibernate.cfg.xml | 2 +- .../cache/HibernateSecLvlQueryCache.java | 4 +- .../AbstractReadWriteAccessStrategy.java | 360 +++++++ .../BaseCollectionRegionAccessStrategy.java | 39 +- .../cache/BaseEntityRegionAccessStrategy.java | 85 ++ .../testing/cache/BaseGeneralDataRegion.java | 91 ++ .../hibernate/testing/cache/BaseRegion.java | 71 +- .../cache/BaseRegionAccessStrategy.java | 109 ++ .../cache/BaseTransactionalDataRegion.java | 33 +- .../testing/cache/CachingRegionFactory.java | 116 +++ .../testing/cache/CollectionRegionImpl.java | 74 ++ .../testing/cache/EntityRegionImpl.java | 78 ++ ...adWriteCollectionRegionAccessStrategy.java | 38 +- ...ctReadWriteEntityRegionAccessStrategy.java | 57 +- ...eadOnlyCollectionRegionAccessStrategy.java | 59 ++ .../ReadOnlyEntityRegionAccessStrategy.java | 86 ++ ...adWriteCollectionRegionAccessStrategy.java | 47 +- .../ReadWriteEntityRegionAccessStrategy.java | 127 +++ .../junit4/BaseCoreFunctionalTestCase.java | 4 +- 67 files changed, 1958 insertions(+), 3990 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/internal/HashtableCache.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/internal/NoCacheProvider.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/CollectionAccessStrategyAdapter.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/CollectionRegionAdapter.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/EntityAccessStrategyAdapter.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/EntityRegionAdapter.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/RegionFactoryCacheProviderBridge.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/TimestampsRegionAdapter.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/spi/AbstractJndiBoundCacheProvider.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/spi/Cache.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/spi/CacheConcurrencyStrategy.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/spi/CacheProvider.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/spi/NonstrictReadWriteCache.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/spi/OptimisticCache.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/spi/ReadOnlyCache.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/spi/ReadWriteCache.java delete mode 100644 hibernate-core/src/main/java/org/hibernate/cache/spi/TransactionalCache.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/legacy/CacheTest.java delete mode 100644 hibernate-ehcache/src/main/java/org/hibernate/cache/internal/EhCache.java delete mode 100644 hibernate-ehcache/src/main/java/org/hibernate/cache/internal/EhCacheProvider.java delete mode 100644 hibernate-ehcache/src/main/java/org/hibernate/cache/internal/SingletonEhCacheProvider.java create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/cache/AbstractReadWriteAccessStrategy.java rename hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseGeneralDataRegionAdapter.java => hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseCollectionRegionAccessStrategy.java (55%) create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseEntityRegionAccessStrategy.java create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseGeneralDataRegion.java rename hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseRegionAdapter.java => hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegion.java (57%) create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegionAccessStrategy.java rename hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseTransactionalDataRegionAdapter.java => hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseTransactionalDataRegion.java (65%) create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/cache/CachingRegionFactory.java create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/cache/CollectionRegionImpl.java create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/cache/EntityRegionImpl.java rename hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/OptimisticCacheSourceAdapter.java => hibernate-testing/src/main/java/org/hibernate/testing/cache/NonstrictReadWriteCollectionRegionAccessStrategy.java (56%) rename hibernate-core/src/main/java/org/hibernate/cache/internal/HashtableCacheProvider.java => hibernate-testing/src/main/java/org/hibernate/testing/cache/NonstrictReadWriteEntityRegionAccessStrategy.java (50%) create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyCollectionRegionAccessStrategy.java create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyEntityRegionAccessStrategy.java rename hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/QueryResultsRegionAdapter.java => hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteCollectionRegionAccessStrategy.java (51%) create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteEntityRegionAccessStrategy.java diff --git a/documentation/src/main/docbook/manual/en-US/content/performance.xml b/documentation/src/main/docbook/manual/en-US/content/performance.xml index 6ca4d017d8..a156785683 100644 --- a/documentation/src/main/docbook/manual/en-US/content/performance.xml +++ b/documentation/src/main/docbook/manual/en-US/content/performance.xml @@ -750,9 +750,9 @@ Customer customer = (Customer) session.get( Customer.class, customerId ); - Hashtable (not intended for production use) + ConcurrentHashMap (only for testing purpose, in hibernate-testing module) - org.hibernate.cache.internal.HashtableCacheProvider + org.hibernate.testing.cache.CachingRegionFactory memory @@ -764,7 +764,7 @@ Customer customer = (Customer) session.get( Customer.class, customerId ); EHCache - org.hibernate.cache.internal.EhCacheProvider + org.hibernate.cache.internal.EhCacheRegionFactory memory, disk @@ -773,7 +773,7 @@ Customer customer = (Customer) session.get( Customer.class, customerId ); yes - + + + Infinispan + + org.hibernate.cache.infinispan.InfinispanRegionFactory + + clustered (ip multicast), transactional + + yes (replication or invalidation) + yes (clock sync req.) @@ -1114,7 +1125,7 @@ public SortedSet<Ticket> getTickets() { - Hashtable (not intended for production use) + ConcurrentHashMap (not intended for production use) yes @@ -1134,10 +1145,10 @@ public SortedSet<Ticket> getTickets() { yes - + yes - + + + Infinispan + + yes + + + + + yes diff --git a/etc/hibernate.properties b/etc/hibernate.properties index 668ea03a45..7609848e7b 100644 --- a/etc/hibernate.properties +++ b/etc/hibernate.properties @@ -492,13 +492,11 @@ hibernate.cache.region_prefix hibernate.test ## choose a cache implementation -#hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider -#hibernate.cache.provider_class org.hibernate.cache.EmptyCacheProvider -hibernate.cache.provider_class org.hibernate.cache.internal.HashtableCacheProvider -#hibernate.cache.provider_class org.hibernate.cache.TreeCacheProvider -#hibernate.cache.provider_class org.hibernate.cache.OSCacheProvider -#hibernate.cache.provider_class org.hibernate.cache.SwarmCacheProvider - +#hibernate.cache.region.factory_class org.hibernate.cache.infinispan.InfinispanRegionFactory +#hibernate.cache.region.factory_class org.hibernate.cache.infinispan.JndiInfinispanRegionFactory +#hibernate.cache.region.factory_class org.hibernate.cache.internal.EhCacheRegionFactory +#hibernate.cache.region.factory_class org.hibernate.cache.internal.SingletonEhCacheRegionFactory +hibernate.cache.region.factory_class org.hibernate.cache.internal.NoCachingRegionFactory ## choose a custom query cache implementation diff --git a/etc/hibernate.properties.template b/etc/hibernate.properties.template index 9445826986..8b7b015083 100644 --- a/etc/hibernate.properties.template +++ b/etc/hibernate.properties.template @@ -1,488 +1,487 @@ -###################### -### Query Language ### -###################### - -## define query language constants / function names - -hibernate.query.substitutions true 1, false 0, yes 'Y', no 'N' - - -## Query translator factory class - -hibernate.query.factory_class @QUERY_TRANSLATOR_FACTORY@ - -################# -### Platforms ### -################# - -hibernate.dialect @HIBERNATE_DIALECT@ -hibernate.connection.driver_class @DRIVER_CLASS@ -hibernate.connection.username @DB_USERNAME@ -hibernate.connection.password @DB_PASSWORD@ -hibernate.connection.url @DB_URL@ - -## JNDI Datasource - -#hibernate.connection.datasource jdbc/test -#hibernate.connection.username db2 -#hibernate.connection.password db2 - - -## HypersonicSQL - -#hibernate.dialect org.hibernate.dialect.HSQLDialect -#hibernate.connection.driver_class org.hsqldb.jdbcDriver -#hibernate.connection.username sa -#hibernate.connection.password -#hibernate.connection.url jdbc:hsqldb:hsql://localhost -#hibernate.connection.url jdbc:hsqldb:test -#hibernate.connection.url jdbc:hsqldb:. - - -## MySQL - - -#hibernate.dialect org.hibernate.dialect.MySQLDialect -#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect -#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect -#hibernate.connection.driver_class org.gjt.mm.mysql.Driver -#hibernate.connection.driver_class com.mysql.jdbc.Driver -#hibernate.connection.url jdbc:mysql:///test -#hibernate.connection.username gavin -#hibernate.connection.password - - -## Oracle - -#hibernate.dialect org.hibernate.dialect.OracleDialect -#hibernate.dialect org.hibernate.dialect.Oracle9Dialect -#hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver -#hibernate.connection.username ora -#hibernate.connection.password ora -#hibernate.connection.url jdbc:oracle:thin:@localhost:1521:test - - -## PostgreSQL - -#hibernate.dialect org.hibernate.dialect.PostgreSQLDialect -#hibernate.connection.driver_class org.postgresql.Driver -#hibernate.connection.url jdbc:postgresql:template1 -#hibernate.connection.username pg -#hibernate.connection.password -#hibernate.query.substitutions yes 'Y', no 'N' - - -## DB2 - -#hibernate.dialect org.hibernate.dialect.DB2Dialect -#hibernate.connection.driver_class COM.ibm.db2.jdbc.app.DB2Driver -#hibernate.connection.url jdbc:db2:test -#hibernate.connection.username db2 -#hibernate.connection.password db2 - -## TimesTen (not supported yet) - -#hibernate.dialect org.hibernate.dialect.TimesTenDialect -#hibernate.connection.driver_class com.timesten.jdbc.TimesTenDriver -#hibernate.connection.url jdbc:timesten:direct:test -#hibernate.connection.username -#hibernate.connection.password - -## DB2/400 - -#hibernate.dialect org.hibernate.dialect.DB2400Dialect -#hibernate.connection.username user -#hibernate.connection.password password - -## Native driver -#hibernate.connection.driver_class COM.ibm.db2.jdbc.app.DB2Driver -#hibernate.connection.url jdbc:db2://systemname - -## Toolbox driver -#hibernate.connection.driver_class com.ibm.as400.access.AS400JDBCDriver -#hibernate.connection.url jdbc:as400://systemname - - -## Derby (Not supported!) - -#hibernate.dialect org.hibernate.dialect.DerbyDialect -#hibernate.connection.driver_class org.apache.derby.jdbc.EmbeddedDriver -#hibernate.connection.username -#hibernate.connection.password -#hibernate.connection.url jdbc:derby:/test;create=true - - -## Sybase - -#hibernate.dialect org.hibernate.dialect.SybaseDialect -#hibernate.connection.driver_class com.sybase.jdbc2.jdbc.SybDriver -#hibernate.connection.username sa -#hibernate.connection.password sasasa -#hibernate.connection.url jdbc:sybase:Tds:co3061835-a:5000/tempdb - - -## Mckoi SQL - -#hibernate.dialect org.hibernate.dialect.MckoiDialect -#hibernate.connection.driver_class com.mckoi.JDBCDriver -#hibernate.connection.url jdbc:mckoi:/// -#hibernate.connection.url jdbc:mckoi:local://C:/mckoi1.00/db.conf -#hibernate.connection.username admin -#hibernate.connection.password nimda - - -## SAP DB - -#hibernate.dialect org.hibernate.dialect.SAPDBDialect -#hibernate.connection.driver_class com.sap.dbtech.jdbc.DriverSapDB -#hibernate.connection.url jdbc:sapdb://localhost/TST -#hibernate.connection.username TEST -#hibernate.connection.password TEST -#hibernate.query.substitutions yes 'Y', no 'N' - - -## MS SQL Server - -#hibernate.dialect org.hibernate.dialect.SQLServerDialect -#hibernate.connection.username sa -#hibernate.connection.password sa - -## JSQL Driver -#hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver -#hibernate.connection.url jdbc:JSQLConnect://1E1/test - -## JTURBO Driver -#hibernate.connection.driver_class com.newatlanta.jturbo.driver.Driver -#hibernate.connection.url jdbc:JTurbo://1E1:1433/test - -## WebLogic Driver -#hibernate.connection.driver_class weblogic.jdbc.mssqlserver4.Driver -#hibernate.connection.url jdbc:weblogic:mssqlserver4:1E1:1433 - -## Microsoft Driver (not recommended!) -#hibernate.connection.driver_class com.microsoft.jdbc.sqlserver.SQLServerDriver -#hibernate.connection.url jdbc:microsoft:sqlserver://1E1;DatabaseName=test;SelectMethod=cursor - -## jTDS (since version 0.9) -#hibernate.connection.driver_class net.sourceforge.jtds.jdbc.Driver -#hibernate.connection.url jdbc:jtds:sqlserver://1E1/test - -## Interbase - -#hibernate.dialect org.hibernate.dialect.InterbaseDialect -#hibernate.connection.username sysdba -#hibernate.connection.password masterkey - -## DO NOT specify hibernate.connection.sqlDialect - -## InterClient - -#hibernate.connection.driver_class interbase.interclient.Driver -#hibernate.connection.url jdbc:interbase://localhost:3060/C:/firebird/test.gdb - -## Pure Java - -#hibernate.connection.driver_class org.firebirdsql.jdbc.FBDriver -#hibernate.connection.url jdbc:firebirdsql:localhost/3050:/firebird/test.gdb - - -## Pointbase - -#hibernate.dialect org.hibernate.dialect.PointbaseDialect -#hibernate.connection.driver_class com.pointbase.jdbc.jdbcUniversalDriver -#hibernate.connection.url jdbc:pointbase:embedded:sample -#hibernate.connection.username PBPUBLIC -#hibernate.connection.password PBPUBLIC - - -## Ingres - -#hibernate.dialect org.hibernate.dialect.IngresDialect -#hibernate.connection.driver_class ca.edbc.jdbc.EdbcDriver -#hibernate.connection.url jdbc:edbc://localhost:II7/database -#hibernate.connection.username user -#hibernate.connection.password password - - -## Mimer SQL - -#hibernate.dialect org.hibernate.dialect.MimerSQLDialect -#hibernate.connection.driver_class com.mimer.jdbc.Driver -#hibernate.connection.url jdbc:mimer:multi1 -#hibernate.connection.username hibernate -#hibernate.connection.password hibernate - - - -################################# -### Hibernate Connection Pool ### -################################# - -hibernate.connection.pool_size 1 - - - -########################### -### C3P0 Connection Pool### -########################### - -#hibernate.c3p0.max_size 2 -#hibernate.c3p0.min_size 2 -#hibernate.c3p0.timeout 5000 -#hibernate.c3p0.max_statements 100 -#hibernate.c3p0.idle_test_period 3000 -#hibernate.c3p0.acquire_increment 2 -#hibernate.c3p0.validate false - - - -############################## -### Proxool Connection Pool### -############################## - -## Properties for external configuration of Proxool - -hibernate.proxool.pool_alias pool1 - -## Only need one of the following - -#hibernate.proxool.existing_pool true -#hibernate.proxool.xml proxool.xml -#hibernate.proxool.properties proxool.properties - - - -################################# -### Plugin ConnectionProvider ### -################################# - -## use a custom ConnectionProvider (if not set, Hibernate will choose a built-in ConnectionProvider using hueristics) - -#hibernate.connection.provider_class org.hibernate.connection.DriverManagerConnectionProvider -#hibernate.connection.provider_class org.hibernate.connection.DatasourceConnectionProvider -#hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider -#hibernate.connection.provider_class org.hibernate.connection.DBCPConnectionProvider -#hibernate.connection.provider_class org.hibernate.connection.ProxoolConnectionProvider - - - -####################### -### Transaction API ### -####################### - -## Enable automatic flush during the JTA beforeCompletion() callback -## (This setting is relevant with or without the Transaction API) - -#hibernate.transaction.flush_before_completion - - -## Enable automatic session close at the end of transaction -## (This setting is relevant with or without the Transaction API) - -#hibernate.transaction.auto_close_session - - -## the Transaction API abstracts application code from the underlying JTA or JDBC transactions - -#hibernate.transaction.factory_class org.hibernate.transaction.JTATransactionFactory -#hibernate.transaction.factory_class org.hibernate.transaction.JDBCTransactionFactory - - -## to use JTATransactionFactory, Hibernate must be able to locate the UserTransaction in JNDI -## default is java:comp/UserTransaction -## you do NOT need this setting if you specify hibernate.transaction.manager_lookup_class - -#jta.UserTransaction jta/usertransaction -#jta.UserTransaction javax.transaction.UserTransaction -#jta.UserTransaction UserTransaction - - -## to use the second-level cache with JTA, Hibernate must be able to obtain the JTA TransactionManager - -#hibernate.transaction.manager_lookup_class org.hibernate.transaction.JBossTransactionManagerLookup -#hibernate.transaction.manager_lookup_class org.hibernate.transaction.WeblogicTransactionManagerLookup -#hibernate.transaction.manager_lookup_class org.hibernate.transaction.WebSphereTransactionManagerLookup -#hibernate.transaction.manager_lookup_class org.hibernate.transaction.OrionTransactionManagerLookup -#hibernate.transaction.manager_lookup_class org.hibernate.transaction.ResinTransactionManagerLookup - - - -############################## -### Miscellaneous Settings ### -############################## - -## print all generated SQL to the console - -#hibernate.show_sql true - - -## add comments to the generated SQL - -#hibernate.use_sql_comments true - - -## generate statistics - -#hibernate.generate_statistics true - - -## auto schema export - -#hibernate.hbm2ddl.auto create-drop -#hibernate.hbm2ddl.auto create -#hibernate.hbm2ddl.auto update - - -## specify a default schema and catalog for unqualified tablenames - -#hibernate.default_schema test -#hibernate.default_catalog test - - -## enable ordering of SQL UPDATEs by primary key - -hibernate.order_updates true - - -## set the maximum depth of the outer join fetch tree - -hibernate.max_fetch_depth 1 - - -## set the default batch size for batch fetching - -hibernate.default_batch_fetch_size 8 - - -## rollback generated identifier values of deleted entities to default values - -#hibernate.use_identifer_rollback true - - -## enable bytecode reflection optimizer (disabled by default) - -#hibernate.bytecode.use_reflection_optimizer true - - - -##################### -### JDBC Settings ### -##################### - -## specify a JDBC isolation level - -#hibernate.connection.isolation 4 - - -## enable JDBC autocommit (not recommended!) - -#hibernate.connection.autocommit true - - -## set the JDBC fetch size - -#hibernate.jdbc.fetch_size 25 - - -## set the maximum JDBC 2 batch size (a nonzero value enables batching) - -#hibernate.jdbc.batch_size 5 - - -## enable batch updates even for versioned data - -hibernate.jdbc.batch_versioned_data true - - -## enable use of JDBC 2 scrollable ResultSets (specifying a Dialect will cause Hibernate to use a sensible default) - -#hibernate.jdbc.use_scrollable_resultset true - - -## use streams when writing binary types to / from JDBC - -hibernate.jdbc.use_streams_for_binary true - - -## use JDBC 3 PreparedStatement.getGeneratedKeys() to get the identifier of an inserted row - -#hibernate.jdbc.use_get_generated_keys false - - -## choose a custom JDBC batcher - -# hibernate.jdbc.factory_class - - -## choose a custom SQL exception converter - -#hibernate.jdbc.sql_exception_converter - - - -########################## -### Second-level Cache ### -########################## - -## optimize chache for minimal "puts" instead of minimal "gets" (good for clustered cache) - -#hibernate.cache.use_minimal_puts true - - -## set a prefix for cache region names - -hibernate.cache.region_prefix hibernate.test - - -## disable the second-level cache - -#hibernate.cache.use_second_level_cache false - - -## enable the query cache - -#hibernate.cache.use_query_cache true - - -## store the second-level cache entries in a more human-friendly format - -#hibernate.cache.use_structured_entries true - - -## choose a cache implementation - -#hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider -#hibernate.cache.provider_class org.hibernate.cache.EmptyCacheProvider -hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider -#hibernate.cache.provider_class org.hibernate.cache.TreeCacheProvider -#hibernate.cache.provider_class org.hibernate.cache.OSCacheProvider -#hibernate.cache.provider_class org.hibernate.cache.SwarmCacheProvider - - -## choose a custom query cache implementation - -#hibernate.cache.query_cache_factory - - - -############ -### JNDI ### -############ - -## specify a JNDI name for the SessionFactory - -#hibernate.session_factory_name hibernate/session_factory - - -## Hibernate uses JNDI to bind a name to a SessionFactory and to look up the JTA UserTransaction; -## if hibernate.jndi.* are not specified, Hibernate will use the default InitialContext() which -## is the best approach in an application server - -#file system -#hibernate.jndi.class com.sun.jndi.fscontext.RefFSContextFactory -#hibernate.jndi.url file:/ - -#WebSphere -#hibernate.jndi.class com.ibm.websphere.naming.WsnInitialContextFactory -#hibernate.jndi.url iiop://localhost:900/ - +###################### +### Query Language ### +###################### + +## define query language constants / function names + +hibernate.query.substitutions true 1, false 0, yes 'Y', no 'N' + + +## Query translator factory class + +hibernate.query.factory_class @QUERY_TRANSLATOR_FACTORY@ + +################# +### Platforms ### +################# + +hibernate.dialect @HIBERNATE_DIALECT@ +hibernate.connection.driver_class @DRIVER_CLASS@ +hibernate.connection.username @DB_USERNAME@ +hibernate.connection.password @DB_PASSWORD@ +hibernate.connection.url @DB_URL@ + +## JNDI Datasource + +#hibernate.connection.datasource jdbc/test +#hibernate.connection.username db2 +#hibernate.connection.password db2 + + +## HypersonicSQL + +#hibernate.dialect org.hibernate.dialect.HSQLDialect +#hibernate.connection.driver_class org.hsqldb.jdbcDriver +#hibernate.connection.username sa +#hibernate.connection.password +#hibernate.connection.url jdbc:hsqldb:hsql://localhost +#hibernate.connection.url jdbc:hsqldb:test +#hibernate.connection.url jdbc:hsqldb:. + + +## MySQL + + +#hibernate.dialect org.hibernate.dialect.MySQLDialect +#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect +#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect +#hibernate.connection.driver_class org.gjt.mm.mysql.Driver +#hibernate.connection.driver_class com.mysql.jdbc.Driver +#hibernate.connection.url jdbc:mysql:///test +#hibernate.connection.username gavin +#hibernate.connection.password + + +## Oracle + +#hibernate.dialect org.hibernate.dialect.OracleDialect +#hibernate.dialect org.hibernate.dialect.Oracle9Dialect +#hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver +#hibernate.connection.username ora +#hibernate.connection.password ora +#hibernate.connection.url jdbc:oracle:thin:@localhost:1521:test + + +## PostgreSQL + +#hibernate.dialect org.hibernate.dialect.PostgreSQLDialect +#hibernate.connection.driver_class org.postgresql.Driver +#hibernate.connection.url jdbc:postgresql:template1 +#hibernate.connection.username pg +#hibernate.connection.password +#hibernate.query.substitutions yes 'Y', no 'N' + + +## DB2 + +#hibernate.dialect org.hibernate.dialect.DB2Dialect +#hibernate.connection.driver_class COM.ibm.db2.jdbc.app.DB2Driver +#hibernate.connection.url jdbc:db2:test +#hibernate.connection.username db2 +#hibernate.connection.password db2 + +## TimesTen (not supported yet) + +#hibernate.dialect org.hibernate.dialect.TimesTenDialect +#hibernate.connection.driver_class com.timesten.jdbc.TimesTenDriver +#hibernate.connection.url jdbc:timesten:direct:test +#hibernate.connection.username +#hibernate.connection.password + +## DB2/400 + +#hibernate.dialect org.hibernate.dialect.DB2400Dialect +#hibernate.connection.username user +#hibernate.connection.password password + +## Native driver +#hibernate.connection.driver_class COM.ibm.db2.jdbc.app.DB2Driver +#hibernate.connection.url jdbc:db2://systemname + +## Toolbox driver +#hibernate.connection.driver_class com.ibm.as400.access.AS400JDBCDriver +#hibernate.connection.url jdbc:as400://systemname + + +## Derby (Not supported!) + +#hibernate.dialect org.hibernate.dialect.DerbyDialect +#hibernate.connection.driver_class org.apache.derby.jdbc.EmbeddedDriver +#hibernate.connection.username +#hibernate.connection.password +#hibernate.connection.url jdbc:derby:/test;create=true + + +## Sybase + +#hibernate.dialect org.hibernate.dialect.SybaseDialect +#hibernate.connection.driver_class com.sybase.jdbc2.jdbc.SybDriver +#hibernate.connection.username sa +#hibernate.connection.password sasasa +#hibernate.connection.url jdbc:sybase:Tds:co3061835-a:5000/tempdb + + +## Mckoi SQL + +#hibernate.dialect org.hibernate.dialect.MckoiDialect +#hibernate.connection.driver_class com.mckoi.JDBCDriver +#hibernate.connection.url jdbc:mckoi:/// +#hibernate.connection.url jdbc:mckoi:local://C:/mckoi1.00/db.conf +#hibernate.connection.username admin +#hibernate.connection.password nimda + + +## SAP DB + +#hibernate.dialect org.hibernate.dialect.SAPDBDialect +#hibernate.connection.driver_class com.sap.dbtech.jdbc.DriverSapDB +#hibernate.connection.url jdbc:sapdb://localhost/TST +#hibernate.connection.username TEST +#hibernate.connection.password TEST +#hibernate.query.substitutions yes 'Y', no 'N' + + +## MS SQL Server + +#hibernate.dialect org.hibernate.dialect.SQLServerDialect +#hibernate.connection.username sa +#hibernate.connection.password sa + +## JSQL Driver +#hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver +#hibernate.connection.url jdbc:JSQLConnect://1E1/test + +## JTURBO Driver +#hibernate.connection.driver_class com.newatlanta.jturbo.driver.Driver +#hibernate.connection.url jdbc:JTurbo://1E1:1433/test + +## WebLogic Driver +#hibernate.connection.driver_class weblogic.jdbc.mssqlserver4.Driver +#hibernate.connection.url jdbc:weblogic:mssqlserver4:1E1:1433 + +## Microsoft Driver (not recommended!) +#hibernate.connection.driver_class com.microsoft.jdbc.sqlserver.SQLServerDriver +#hibernate.connection.url jdbc:microsoft:sqlserver://1E1;DatabaseName=test;SelectMethod=cursor + +## jTDS (since version 0.9) +#hibernate.connection.driver_class net.sourceforge.jtds.jdbc.Driver +#hibernate.connection.url jdbc:jtds:sqlserver://1E1/test + +## Interbase + +#hibernate.dialect org.hibernate.dialect.InterbaseDialect +#hibernate.connection.username sysdba +#hibernate.connection.password masterkey + +## DO NOT specify hibernate.connection.sqlDialect + +## InterClient + +#hibernate.connection.driver_class interbase.interclient.Driver +#hibernate.connection.url jdbc:interbase://localhost:3060/C:/firebird/test.gdb + +## Pure Java + +#hibernate.connection.driver_class org.firebirdsql.jdbc.FBDriver +#hibernate.connection.url jdbc:firebirdsql:localhost/3050:/firebird/test.gdb + + +## Pointbase + +#hibernate.dialect org.hibernate.dialect.PointbaseDialect +#hibernate.connection.driver_class com.pointbase.jdbc.jdbcUniversalDriver +#hibernate.connection.url jdbc:pointbase:embedded:sample +#hibernate.connection.username PBPUBLIC +#hibernate.connection.password PBPUBLIC + + +## Ingres + +#hibernate.dialect org.hibernate.dialect.IngresDialect +#hibernate.connection.driver_class ca.edbc.jdbc.EdbcDriver +#hibernate.connection.url jdbc:edbc://localhost:II7/database +#hibernate.connection.username user +#hibernate.connection.password password + + +## Mimer SQL + +#hibernate.dialect org.hibernate.dialect.MimerSQLDialect +#hibernate.connection.driver_class com.mimer.jdbc.Driver +#hibernate.connection.url jdbc:mimer:multi1 +#hibernate.connection.username hibernate +#hibernate.connection.password hibernate + + + +################################# +### Hibernate Connection Pool ### +################################# + +hibernate.connection.pool_size 1 + + + +########################### +### C3P0 Connection Pool### +########################### + +#hibernate.c3p0.max_size 2 +#hibernate.c3p0.min_size 2 +#hibernate.c3p0.timeout 5000 +#hibernate.c3p0.max_statements 100 +#hibernate.c3p0.idle_test_period 3000 +#hibernate.c3p0.acquire_increment 2 +#hibernate.c3p0.validate false + + + +############################## +### Proxool Connection Pool### +############################## + +## Properties for external configuration of Proxool + +hibernate.proxool.pool_alias pool1 + +## Only need one of the following + +#hibernate.proxool.existing_pool true +#hibernate.proxool.xml proxool.xml +#hibernate.proxool.properties proxool.properties + + + +################################# +### Plugin ConnectionProvider ### +################################# + +## use a custom ConnectionProvider (if not set, Hibernate will choose a built-in ConnectionProvider using hueristics) + +#hibernate.connection.provider_class org.hibernate.connection.DriverManagerConnectionProvider +#hibernate.connection.provider_class org.hibernate.connection.DatasourceConnectionProvider +#hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider +#hibernate.connection.provider_class org.hibernate.connection.DBCPConnectionProvider +#hibernate.connection.provider_class org.hibernate.connection.ProxoolConnectionProvider + + + +####################### +### Transaction API ### +####################### + +## Enable automatic flush during the JTA beforeCompletion() callback +## (This setting is relevant with or without the Transaction API) + +#hibernate.transaction.flush_before_completion + + +## Enable automatic session close at the end of transaction +## (This setting is relevant with or without the Transaction API) + +#hibernate.transaction.auto_close_session + + +## the Transaction API abstracts application code from the underlying JTA or JDBC transactions + +#hibernate.transaction.factory_class org.hibernate.transaction.JTATransactionFactory +#hibernate.transaction.factory_class org.hibernate.transaction.JDBCTransactionFactory + + +## to use JTATransactionFactory, Hibernate must be able to locate the UserTransaction in JNDI +## default is java:comp/UserTransaction +## you do NOT need this setting if you specify hibernate.transaction.manager_lookup_class + +#jta.UserTransaction jta/usertransaction +#jta.UserTransaction javax.transaction.UserTransaction +#jta.UserTransaction UserTransaction + + +## to use the second-level cache with JTA, Hibernate must be able to obtain the JTA TransactionManager + +#hibernate.transaction.manager_lookup_class org.hibernate.transaction.JBossTransactionManagerLookup +#hibernate.transaction.manager_lookup_class org.hibernate.transaction.WeblogicTransactionManagerLookup +#hibernate.transaction.manager_lookup_class org.hibernate.transaction.WebSphereTransactionManagerLookup +#hibernate.transaction.manager_lookup_class org.hibernate.transaction.OrionTransactionManagerLookup +#hibernate.transaction.manager_lookup_class org.hibernate.transaction.ResinTransactionManagerLookup + + + +############################## +### Miscellaneous Settings ### +############################## + +## print all generated SQL to the console + +#hibernate.show_sql true + + +## add comments to the generated SQL + +#hibernate.use_sql_comments true + + +## generate statistics + +#hibernate.generate_statistics true + + +## auto schema export + +#hibernate.hbm2ddl.auto create-drop +#hibernate.hbm2ddl.auto create +#hibernate.hbm2ddl.auto update + + +## specify a default schema and catalog for unqualified tablenames + +#hibernate.default_schema test +#hibernate.default_catalog test + + +## enable ordering of SQL UPDATEs by primary key + +hibernate.order_updates true + + +## set the maximum depth of the outer join fetch tree + +hibernate.max_fetch_depth 1 + + +## set the default batch size for batch fetching + +hibernate.default_batch_fetch_size 8 + + +## rollback generated identifier values of deleted entities to default values + +#hibernate.use_identifer_rollback true + + +## enable bytecode reflection optimizer (disabled by default) + +#hibernate.bytecode.use_reflection_optimizer true + + + +##################### +### JDBC Settings ### +##################### + +## specify a JDBC isolation level + +#hibernate.connection.isolation 4 + + +## enable JDBC autocommit (not recommended!) + +#hibernate.connection.autocommit true + + +## set the JDBC fetch size + +#hibernate.jdbc.fetch_size 25 + + +## set the maximum JDBC 2 batch size (a nonzero value enables batching) + +#hibernate.jdbc.batch_size 5 + + +## enable batch updates even for versioned data + +hibernate.jdbc.batch_versioned_data true + + +## enable use of JDBC 2 scrollable ResultSets (specifying a Dialect will cause Hibernate to use a sensible default) + +#hibernate.jdbc.use_scrollable_resultset true + + +## use streams when writing binary types to / from JDBC + +hibernate.jdbc.use_streams_for_binary true + + +## use JDBC 3 PreparedStatement.getGeneratedKeys() to get the identifier of an inserted row + +#hibernate.jdbc.use_get_generated_keys false + + +## choose a custom JDBC batcher + +# hibernate.jdbc.factory_class + + +## choose a custom SQL exception converter + +#hibernate.jdbc.sql_exception_converter + + + +########################## +### Second-level Cache ### +########################## + +## optimize chache for minimal "puts" instead of minimal "gets" (good for clustered cache) + +#hibernate.cache.use_minimal_puts true + + +## set a prefix for cache region names + +hibernate.cache.region_prefix hibernate.test + + +## disable the second-level cache + +#hibernate.cache.use_second_level_cache false + + +## enable the query cache + +#hibernate.cache.use_query_cache true + + +## store the second-level cache entries in a more human-friendly format + +#hibernate.cache.use_structured_entries true + + +## choose a cache implementation + +#hibernate.cache.region.factory_class org.hibernate.cache.infinispan.InfinispanRegionFactory +#hibernate.cache.region.factory_class org.hibernate.cache.infinispan.JndiInfinispanRegionFactory +#hibernate.cache.region.factory_class org.hibernate.cache.internal.EhCacheRegionFactory +#hibernate.cache.region.factory_class org.hibernate.cache.internal.SingletonEhCacheRegionFactory +#hibernate.cache.region.factory_class org.hibernate.cache.internal.NoCachingRegionFactory + + +## choose a custom query cache implementation + +#hibernate.cache.query_cache_factory + + + +############ +### JNDI ### +############ + +## specify a JNDI name for the SessionFactory + +#hibernate.session_factory_name hibernate/session_factory + + +## Hibernate uses JNDI to bind a name to a SessionFactory and to look up the JTA UserTransaction; +## if hibernate.jndi.* are not specified, Hibernate will use the default InitialContext() which +## is the best approach in an application server + +#file system +#hibernate.jndi.class com.sun.jndi.fscontext.RefFSContextFactory +#hibernate.jndi.url file:/ + +#WebSphere +#hibernate.jndi.class com.ibm.websphere.naming.WsnInitialContextFactory +#hibernate.jndi.url iiop://localhost:900/ + diff --git a/hibernate-core/src/main/java/org/hibernate/MultiTenancyStrategy.java b/hibernate-core/src/main/java/org/hibernate/MultiTenancyStrategy.java index 999b584057..64f2c98337 100644 --- a/hibernate-core/src/main/java/org/hibernate/MultiTenancyStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/MultiTenancyStrategy.java @@ -25,7 +25,10 @@ package org.hibernate; import java.util.Map; +import org.jboss.logging.Logger; + import org.hibernate.cfg.Environment; +import org.hibernate.internal.CoreMessageLogger; /** * Describes the methods for multi-tenancy understood by Hibernate. @@ -33,6 +36,7 @@ import org.hibernate.cfg.Environment; * @author Steve Ebersole */ public enum MultiTenancyStrategy { + /** * Multi-tenancy implemented by use of discriminator columns. */ @@ -49,7 +53,10 @@ public enum MultiTenancyStrategy { * No multi-tenancy */ NONE; - + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, + MultiTenancyStrategy.class.getName() + ); public static MultiTenancyStrategy determineMultiTenancyStrategy(Map properties) { final Object strategy = properties.get( Environment.MULTI_TENANT ); if ( strategy == null ) { @@ -61,20 +68,11 @@ public enum MultiTenancyStrategy { } final String strategyName = strategy.toString(); - if ( MultiTenancyStrategy.DISCRIMINATOR.name().equals( strategyName ) ) { - return MultiTenancyStrategy.DISCRIMINATOR; + try { + return MultiTenancyStrategy.valueOf( strategyName.toUpperCase() ); } - else if ( MultiTenancyStrategy.SCHEMA.name().equals( strategyName ) ) { - return MultiTenancyStrategy.SCHEMA; - } - else if ( MultiTenancyStrategy.DATABASE.name().equals( strategyName ) ) { - return MultiTenancyStrategy.DATABASE; - } - else if ( MultiTenancyStrategy.NONE.name().equals( strategyName ) ) { - return MultiTenancyStrategy.NONE; - } - else { - // todo log? + catch ( RuntimeException e ) { + LOG.warn( "Unknown multi tenancy strategy [ " +strategyName +" ], using MultiTenancyStrategy.NONE." ); return MultiTenancyStrategy.NONE; } } diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/HashtableCache.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/HashtableCache.java deleted file mode 100644 index 519f9f5b2e..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/HashtableCache.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.internal; - -import java.util.Collections; -import java.util.Hashtable; -import java.util.Map; - -import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.Cache; - -/** - * A lightweight implementation of the Cache interface - * @author Gavin King - */ -public class HashtableCache implements Cache { - - private final Map hashtable = new Hashtable(); - private final String regionName; - - public HashtableCache(String regionName) { - this.regionName = regionName; - } - - public String getRegionName() { - return regionName; - } - - public Object read(Object key) throws CacheException { - return hashtable.get(key); - } - - public Object get(Object key) throws CacheException { - return hashtable.get(key); - } - - public void update(Object key, Object value) throws CacheException { - put(key, value); - } - - public void put(Object key, Object value) throws CacheException { - hashtable.put(key, value); - } - - public void remove(Object key) throws CacheException { - hashtable.remove(key); - } - - public void clear() throws CacheException { - hashtable.clear(); - } - - public void destroy() throws CacheException { - - } - - public void lock(Object key) throws CacheException { - // local cache, so we use synchronization - } - - public void unlock(Object key) throws CacheException { - // local cache, so we use synchronization - } - - public long nextTimestamp() { - return Timestamper.next(); - } - - public int getTimeout() { - return Timestamper.ONE_MS * 60000; //ie. 60 seconds - } - - public long getSizeInMemory() { - return -1; - } - - public long getElementCountInMemory() { - return hashtable.size(); - } - - public long getElementCountOnDisk() { - return 0; - } - - public Map toMap() { - return Collections.unmodifiableMap(hashtable); - } - - public String toString() { - return "HashtableCache(" + regionName + ')'; - } - -} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/NoCacheProvider.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/NoCacheProvider.java deleted file mode 100644 index 30c455a44d..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/NoCacheProvider.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.internal; - -import java.util.Properties; - -import org.hibernate.cache.CacheException; -import org.hibernate.cache.NoCachingEnabledException; -import org.hibernate.cache.spi.Cache; -import org.hibernate.cache.spi.CacheProvider; - -/** - * Implementation of NoCacheProvider. - * - * @author Steve Ebersole - */ -@Deprecated -public class NoCacheProvider implements CacheProvider { - /** - * Configure the cache - * - * @param regionName the name of the cache region - * @param properties configuration settings - * - * @throws org.hibernate.cache.CacheException - */ - public Cache buildCache(String regionName, Properties properties) throws CacheException { - throw new NoCachingEnabledException(); - } - - /** - * Generate a timestamp - */ - public long nextTimestamp() { - // This, is used by SessionFactoryImpl to hand to the generated SessionImpl; - // was the only reason I could see that we cannot just use null as - // Settings.cacheProvider - return System.currentTimeMillis() / 100; - } - - /** - * Callback to perform any necessary initialization of the underlying cache implementation during SessionFactory - * construction. - * - * @param properties current configuration settings. - */ - public void start(Properties properties) throws CacheException { - // this is called by SessionFactory irregardless; we just disregard here; - // could also add a check to SessionFactory to only conditionally call start - } - - /** - * Callback to perform any necessary cleanup of the underlying cache implementation during SessionFactory.close(). - */ - public void stop() { - // this is called by SessionFactory irregardless; we just disregard here; - // could also add a check to SessionFactory to only conditionally call stop - } - - public boolean isMinimalPutsEnabledByDefault() { - // this is called from SettingsFactory irregardless; trivial to simply disregard - return false; - } - -} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/Timestamper.java b/hibernate-core/src/main/java/org/hibernate/cache/internal/Timestamper.java index fc9c9d1e54..84494a75ba 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/Timestamper.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/internal/Timestamper.java @@ -23,29 +23,28 @@ */ package org.hibernate.cache.internal; +import java.util.concurrent.atomic.AtomicLong; + /** * Generates increasing identifiers (in a single VM only). Not valid across multiple VMs. Identifiers are not * necessarily strictly increasing, but usually are. */ public final class Timestamper { - private static short counter = 0; - private static long time; private static final int BIN_DIGITS = 12; public static final short ONE_MS = 1<must be threadsafe. - * - * @deprecated As of 3.3; see for details. - */ -public interface Cache { - /** - * Get an item from the cache - * @param key - * @return the cached object or null - * @throws org.hibernate.cache.CacheException - */ - public Object read(Object key) throws CacheException; - /** - * Get an item from the cache, nontransactionally - * @param key - * @return the cached object or null - * @throws CacheException - */ - public Object get(Object key) throws CacheException; - /** - * Add an item to the cache, nontransactionally, with - * failfast semantics - * @param key - * @param value - * @throws CacheException - */ - public void put(Object key, Object value) throws CacheException; - /** - * Add an item to the cache - * @param key - * @param value - * @throws CacheException - */ - public void update(Object key, Object value) throws CacheException; - /** - * Remove an item from the cache - */ - public void remove(Object key) throws CacheException; - /** - * Clear the cache - */ - public void clear() throws CacheException; - /** - * Clean up - */ - public void destroy() throws CacheException; - /** - * If this is a clustered cache, lock the item - */ - public void lock(Object key) throws CacheException; - /** - * If this is a clustered cache, unlock the item - */ - public void unlock(Object key) throws CacheException; - /** - * Generate a timestamp - */ - public long nextTimestamp(); - /** - * Get a reasonable "lock timeout" - */ - public int getTimeout(); - - /** - * Get the name of the cache region - */ - public String getRegionName(); - - /** - * The number of bytes is this cache region currently consuming in memory. - * - * @return The number of bytes consumed by this region; -1 if unknown or - * unsupported. - */ - public long getSizeInMemory(); - - /** - * The count of entries currently contained in the regions in-memory store. - * - * @return The count of entries in memory; -1 if unknown or unsupported. - */ - public long getElementCountInMemory(); - - /** - * The count of entries currently contained in the regions disk store. - * - * @return The count of entries on disk; -1 if unknown or unsupported. - */ - public long getElementCountOnDisk(); - - /** - * optional operation - */ - public Map toMap(); -} - - - - - - diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheConcurrencyStrategy.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheConcurrencyStrategy.java deleted file mode 100644 index ff593635fb..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheConcurrencyStrategy.java +++ /dev/null @@ -1,197 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.spi; - -import java.util.Comparator; - -import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.access.SoftLock; - -/** - * Implementors manage transactional access to cached data. Transactions - * pass in a timestamp indicating transaction start time. Two different - * implementation patterns are provided for.
    - *
  • A transaction-aware cache implementation might be wrapped by a - * "synchronous" concurrency strategy, where updates to the cache are written - * to the cache inside the transaction.
  • - *
  • A non transaction-aware cache would be wrapped by an "asynchronous" - * concurrency strategy, where items are merely "soft locked" during the - * transaction and then updated during the "after transaction completion" - * phase; the soft lock is not an actual lock on the database row - - * only upon the cached representation of the item.
  • - *
- *

- * In terms of entity caches, the expected call sequences are:

    - *
  • DELETES : {@link #lock} -> {@link #evict} -> {@link #release}
  • - *
  • UPDATES : {@link #lock} -> {@link #update} -> {@link #afterUpdate}
  • - *
  • INSERTS : {@link #insert} -> {@link #afterInsert}
  • - *
- *

- * In terms of collection caches, all modification actions actually just - * invalidate the entry(s). The call sequence here is: - * {@link #lock} -> {@link #evict} -> {@link #release} - *

- * Note that, for an asynchronous cache, cache invalidation must be a two - * step process (lock->release, or lock-afterUpdate), since this is the only - * way to guarantee consistency with the database for a nontransactional cache - * implementation. For a synchronous cache, cache invalidation is a single - * step process (evict, or update). Hence, this interface defines a three - * step process, to cater for both models. - *

- * Note that query result caching does not go through a concurrency strategy; they - * are managed directly against the underlying {@link Cache cache regions}. - * - * @deprecated As of 3.3; see for details. - */ -public interface CacheConcurrencyStrategy { - - /** - * Attempt to retrieve an object from the cache. Mainly used in attempting - * to resolve entities/collections from the second level cache. - * - * @param key - * @param txTimestamp a timestamp prior to the transaction start time - * @return the cached object or null - * @throws org.hibernate.cache.CacheException - */ - public Object get(Object key, long txTimestamp) throws CacheException; - - /** - * Attempt to cache an object, after loading from the database. - * - * @param key - * @param value - * @param txTimestamp a timestamp prior to the transaction start time - * @param version the item version number - * @param versionComparator a comparator used to compare version numbers - * @param minimalPut indicates that the cache should avoid a put is the item is already cached - * @return true if the object was successfully cached - * @throws CacheException - */ - public boolean put( - Object key, - Object value, - long txTimestamp, - Object version, - Comparator versionComparator, - boolean minimalPut) - throws CacheException; - - /** - * We are going to attempt to update/delete the keyed object. This - * method is used by "asynchronous" concurrency strategies. - *

- * The returned object must be passed back to release(), to release the - * lock. Concurrency strategies which do not support client-visible - * locks may silently return null. - * - * @param key - * @param version - * @throws CacheException - */ - public SoftLock lock(Object key, Object version) throws CacheException; - - /** - * Called after an item has become stale (before the transaction completes). - * This method is used by "synchronous" concurrency strategies. - */ - public void evict(Object key) throws CacheException; - - /** - * Called after an item has been updated (before the transaction completes), - * instead of calling evict(). - * This method is used by "synchronous" concurrency strategies. - */ - public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) throws CacheException; - - /** - * Called after an item has been inserted (before the transaction completes), - * instead of calling evict(). - * This method is used by "synchronous" concurrency strategies. - */ - public boolean insert(Object key, Object value, Object currentVersion) throws CacheException; - - - /** - * Called when we have finished the attempted update/delete (which may or - * may not have been successful), after transaction completion. - * This method is used by "asynchronous" concurrency strategies. - * @param key - * @throws CacheException - */ - public void release(Object key, SoftLock lock) throws CacheException; - /** - * Called after an item has been updated (after the transaction completes), - * instead of calling release(). - * This method is used by "asynchronous" concurrency strategies. - */ - public boolean afterUpdate(Object key, Object value, Object version, SoftLock lock) - throws CacheException; - /** - * Called after an item has been inserted (after the transaction completes), - * instead of calling release(). - * This method is used by "asynchronous" concurrency strategies. - */ - public boolean afterInsert(Object key, Object value, Object version) - throws CacheException; - - - /** - * Evict an item from the cache immediately (without regard for transaction - * isolation). - * @param key - * @throws CacheException - */ - public void remove(Object key) throws CacheException; - /** - * Evict all items from the cache immediately. - * @throws CacheException - */ - public void clear() throws CacheException; - /** - * Clean up all resources. - */ - public void destroy(); - /** - * Set the underlying cache implementation. - * @param cache - */ - public void setCache(Cache cache); - - /** - * Get the cache region name - */ - public String getRegionName(); - - /** - * Get the wrapped cache implementation - */ - public Cache getCache(); -} - - - - - - diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheProvider.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheProvider.java deleted file mode 100644 index 2e28daf04f..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/CacheProvider.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.spi; - -import java.util.Properties; - -import org.hibernate.cache.CacheException; - -/** - * Support for pluggable caches. - * - * @author Gavin King - * @deprecated As of 3.3; see for details. - */ -public interface CacheProvider { - - /** - * Configure the cache - * - * @param regionName the name of the cache region - * @param properties configuration settings - * @throws org.hibernate.cache.CacheException - */ - public Cache buildCache(String regionName, Properties properties) throws CacheException; - - /** - * Generate a timestamp - */ - public long nextTimestamp(); - - /** - * Callback to perform any necessary initialization of the underlying cache implementation - * during SessionFactory construction. - * - * @param properties current configuration settings. - */ - public void start(Properties properties) throws CacheException; - - /** - * Callback to perform any necessary cleanup of the underlying cache implementation - * during SessionFactory.close(). - */ - public void stop(); - - public boolean isMinimalPutsEnabledByDefault(); - -} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/NonstrictReadWriteCache.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/NonstrictReadWriteCache.java deleted file mode 100644 index efd34e0b1c..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/NonstrictReadWriteCache.java +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.spi; - -import java.util.Comparator; - -import org.jboss.logging.Logger; - -import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.access.SoftLock; -import org.hibernate.internal.CoreMessageLogger; - -/** - * Caches data that is sometimes updated without ever locking the cache. - * If concurrent access to an item is possible, this concurrency strategy - * makes no guarantee that the item returned from the cache is the latest - * version available in the database. Configure your cache timeout accordingly! - * This is an "asynchronous" concurrency strategy. - * - * @author Gavin King - * @see ReadWriteCache for a much stricter algorithm - */ -@Deprecated -public class NonstrictReadWriteCache implements CacheConcurrencyStrategy { - - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, - NonstrictReadWriteCache.class.getName()); - - private Cache cache; - - public NonstrictReadWriteCache() { - } - - public void setCache(Cache cache) { - this.cache = cache; - } - - public Cache getCache() { - return cache; - } - - /** - * Get the most recent version, if available. - */ - public Object get(Object key, long txTimestamp) throws CacheException { - LOG.debugf("Cache lookup: %s", key); - - Object result = cache.get( key ); - if (result != null) LOG.debugf("Cache hit: %s", key); - else LOG.debugf("Cache miss: %s", key); - return result; - } - - /** - * Add an item to the cache. - */ - public boolean put( - Object key, - Object value, - long txTimestamp, - Object version, - Comparator versionComparator, - boolean minimalPut) throws CacheException { - if ( minimalPut && cache.get( key ) != null ) { - LOG.debugf("Item already cached: %s", key); - return false; - } - LOG.debugf("Caching: %s", key); - - cache.put( key, value ); - return true; - - } - - /** - * Do nothing. - * - * @return null, no lock - */ - public SoftLock lock(Object key, Object version) throws CacheException { - return null; - } - - public void remove(Object key) throws CacheException { - LOG.debugf("Removing: %s", key); - cache.remove( key ); - } - - public void clear() throws CacheException { - LOG.debugf("Clearing"); - cache.clear(); - } - - public void destroy() { - try { - cache.destroy(); - } - catch ( Exception e ) { - LOG.unableToDestroyCache(e.getMessage()); - } - } - - /** - * Invalidate the item - */ - public void evict(Object key) throws CacheException { - LOG.debugf("Invalidating: %s", key); - cache.remove( key ); - } - - /** - * Invalidate the item - */ - public boolean insert(Object key, Object value, Object currentVersion) { - return false; - } - - /** - * Do nothing. - */ - public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) { - evict( key ); - return false; - } - - /** - * Invalidate the item (again, for safety). - */ - public void release(Object key, SoftLock lock) throws CacheException { - LOG.debugf("Invalidating: %s", key); - cache.remove( key ); - } - - /** - * Invalidate the item (again, for safety). - */ - public boolean afterUpdate(Object key, Object value, Object version, SoftLock lock) throws CacheException { - release( key, lock ); - return false; - } - - /** - * Do nothing. - */ - public boolean afterInsert(Object key, Object value, Object version) throws CacheException { - return false; - } - - public String getRegionName() { - return cache.getRegionName(); - } - - @Override - public String toString() { - return cache + "(nonstrict-read-write)"; - } -} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/OptimisticCache.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/OptimisticCache.java deleted file mode 100644 index 40dcaf6b9b..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/OptimisticCache.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.spi; - -/** - * A contract for transactional cache implementations which support - * optimistic locking of items within the cache. - *

- * The optimistic locking capabilities are only utilized for - * the entity cache regions. - *

- * Unlike the methods on the {@link Cache} interface, all the methods - * here will only ever be called from access scenarios where versioned - * data is actually a possibility (i.e., entity data). Be sure to consult - * with {@link OptimisticCacheSource#isVersioned()} to determine whether - * versioning is actually in effect. - * - * @author Steve Ebersole - */ -public interface OptimisticCache extends Cache { - /** - * Indicates the "source" of the cached data. Currently this will - * only ever represent an {@link org.hibernate.persister.entity.EntityPersister}. - *

- * Made available to the cache so that it can access certain information - * about versioning strategy. - * - * @param source The source. - */ - public void setSource(OptimisticCacheSource source); - - /** - * Called during {@link CacheConcurrencyStrategy#insert} processing for - * transactional strategies. Indicates we have just performed an insert - * into the DB and now need to cache that entity's data. - * - * @param key The cache key. - * @param value The data to be cached. - * @param currentVersion The entity's version; or null if not versioned. - */ - public void writeInsert(Object key, Object value, Object currentVersion); - - /** - * Called during {@link CacheConcurrencyStrategy#update} processing for - * transactional strategies. Indicates we have just performed an update - * against the DB and now need to cache the updated state. - * - * @param key The cache key. - * @param value The data to be cached. - * @param currentVersion The entity's current version - * @param previousVersion The entity's previous version (before the update); - * or null if not versioned. - */ - public void writeUpdate(Object key, Object value, Object currentVersion, Object previousVersion); - - /** - * Called during {@link CacheConcurrencyStrategy#put} processing for - * transactional strategies. Indicates we have just loaded an entity's - * state from the database and need it cached. - * - * @param key The cache key. - * @param value The data to be cached. - * @param currentVersion The entity's version; or null if not versioned. - */ - public void writeLoad(Object key, Object value, Object currentVersion); -} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/ReadOnlyCache.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/ReadOnlyCache.java deleted file mode 100644 index cf8d62d945..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/ReadOnlyCache.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.spi; - -import java.util.Comparator; - -import org.jboss.logging.Logger; - -import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.access.SoftLock; -import org.hibernate.internal.CoreMessageLogger; - -/** - * Caches data that is never updated. - * @see org.hibernate.cache.spi.CacheConcurrencyStrategy - */ -@Deprecated -public class ReadOnlyCache implements CacheConcurrencyStrategy { - - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, ReadOnlyCache.class.getName()); - - private Cache cache; - - public ReadOnlyCache() {} - - public void setCache(Cache cache) { - this.cache=cache; - } - - public Cache getCache() { - return cache; - } - - public String getRegionName() { - return cache.getRegionName(); - } - - public synchronized Object get(Object key, long timestamp) throws CacheException { - Object result = cache.get(key); - if (result != null) LOG.debugf("Cache hit: %s", key); - return result; - } - - /** - * Unsupported! - */ - public SoftLock lock(Object key, Object version) { - LOG.invalidEditOfReadOnlyItem(key); - throw new UnsupportedOperationException("Can't write to a readonly object"); - } - - public synchronized boolean put( - Object key, - Object value, - long timestamp, - Object version, - Comparator versionComparator, - boolean minimalPut) - throws CacheException { - if ( minimalPut && cache.get(key)!=null ) { - LOG.debugf("Item already cached: %s", key); - return false; - } - LOG.debugf("Caching: %s", key); - cache.put(key, value); - return true; - } - - /** - * Unsupported! - */ - public void release(Object key, SoftLock lock) { - LOG.invalidEditOfReadOnlyItem(key); - //throw new UnsupportedOperationException("Can't write to a readonly object"); - } - - public void clear() throws CacheException { - cache.clear(); - } - - public void remove(Object key) throws CacheException { - cache.remove(key); - } - - public void destroy() { - try { - cache.destroy(); - } - catch (Exception e) { - LOG.unableToDestroyCache(e.getMessage()); - } - } - - /** - * Unsupported! - */ - public boolean afterUpdate(Object key, Object value, Object version, SoftLock lock) throws CacheException { - LOG.invalidEditOfReadOnlyItem(key); - throw new UnsupportedOperationException("Can't write to a readonly object"); - } - - /** - * Do nothing. - */ - public boolean afterInsert(Object key, Object value, Object version) throws CacheException { - LOG.debugf("Caching after insert: %s", key); - cache.update(key, value); - return true; - } - - /** - * Do nothing. - */ - public void evict(Object key) throws CacheException { - // noop - } - - /** - * Do nothing. - */ - public boolean insert(Object key, Object value, Object currentVersion) { - return false; - } - - /** - * Unsupported! - */ - public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) { - LOG.invalidEditOfReadOnlyItem(key); - throw new UnsupportedOperationException("Can't write to a readonly object"); - } - - @Override - public String toString() { - return cache + "(read-only)"; - } - -} - - - - - - diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/ReadWriteCache.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/ReadWriteCache.java deleted file mode 100644 index 89ab5e0f97..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/ReadWriteCache.java +++ /dev/null @@ -1,500 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.spi; - -import java.io.Serializable; -import java.util.Comparator; - -import org.jboss.logging.Logger; - -import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.access.SoftLock; -import org.hibernate.internal.CoreMessageLogger; - -/** - * Caches data that is sometimes updated while maintaining the semantics of - * "read committed" isolation level. If the database is set to "repeatable - * read", this concurrency strategy almost maintains the semantics. - * Repeatable read isolation is compromised in the case of concurrent writes. - * This is an "asynchronous" concurrency strategy.
- *
- * If this strategy is used in a cluster, the underlying cache implementation - * must support distributed hard locks (which are held only momentarily). This - * strategy also assumes that the underlying cache implementation does not do - * asynchronous replication and that state has been fully replicated as soon - * as the lock is released. - * - * @see NonstrictReadWriteCache for a faster algorithm - * @see org.hibernate.cache.spi.CacheConcurrencyStrategy - */ -@Deprecated -public class ReadWriteCache implements CacheConcurrencyStrategy { - - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, ReadWriteCache.class.getName()); - - private Cache cache; - private int nextLockId; - - public ReadWriteCache() {} - - public void setCache(Cache cache) { - this.cache=cache; - } - - public Cache getCache() { - return cache; - } - - public String getRegionName() { - return cache.getRegionName(); - } - - /** - * Generate an id for a new lock. Uniqueness per cache instance is very - * desirable but not absolutely critical. Must be called from one of the - * synchronized methods of this class. - */ - private int nextLockId() { - if (nextLockId==Integer.MAX_VALUE) nextLockId = Integer.MIN_VALUE; - return nextLockId++; - } - - /** - * Do not return an item whose timestamp is later than the current - * transaction timestamp. (Otherwise we might compromise repeatable - * read unnecessarily.) Do not return an item which is soft-locked. - * Always go straight to the database instead.
- *
- * Note that since reading an item from that cache does not actually - * go to the database, it is possible to see a kind of phantom read - * due to the underlying row being updated after we have read it - * from the cache. This would not be possible in a lock-based - * implementation of repeatable read isolation. It is also possible - * to overwrite changes made and committed by another transaction - * after the current transaction read the item from the cache. This - * problem would be caught by the update-time version-checking, if - * the data is versioned or timestamped. - */ - public synchronized Object get(Object key, long txTimestamp) throws CacheException { - LOG.debugf("Cache lookup: %s", key); - Lockable lockable = (Lockable)cache.get(key); - boolean gettable = lockable != null && lockable.isGettable(txTimestamp); - if (gettable) { - LOG.debugf("Cache hit: %s", key); - return ((Item)lockable).getValue(); - } - if (lockable == null) LOG.debugf("Cache miss: %s", key); - else LOG.debugf("Cached item was locked: %s", key); - return null; - } - - /** - * Stop any other transactions reading or writing this item to/from - * the cache. Send them straight to the database instead. (The lock - * does time out eventually.) This implementation tracks concurrent - * locks of transactions which simultaneously attempt to write to an - * item. - */ - public synchronized SoftLock lock(Object key, Object version) throws CacheException { - LOG.debugf("Invalidating: %s", key); - try { - cache.lock(key); - - Lockable lockable = (Lockable) cache.get(key); - long timeout = cache.nextTimestamp() + cache.getTimeout(); - final Lock lock = (lockable==null) ? - new Lock( timeout, nextLockId(), version ) : - lockable.lock( timeout, nextLockId() ); - cache.update(key, lock); - return lock; - } - finally { - cache.unlock(key); - } - - } - - /** - * Do not add an item to the cache unless the current transaction - * timestamp is later than the timestamp at which the item was - * invalidated. (Otherwise, a stale item might be re-added if the - * database is operating in repeatable read isolation mode.) - * For versioned data, don't add the item unless it is the later - * version. - */ - public synchronized boolean put( - Object key, - Object value, - long txTimestamp, - Object version, - Comparator versionComparator, - boolean minimalPut) - throws CacheException { - LOG.debugf("Caching: %s", key); - - try { - cache.lock(key); - - Lockable lockable = (Lockable) cache.get(key); - - boolean puttable = lockable==null || - lockable.isPuttable(txTimestamp, version, versionComparator); - - if (puttable) { - cache.put( key, new Item( value, version, cache.nextTimestamp() ) ); - LOG.debugf("Cached: %s", key); - return true; - } - if (lockable.isLock()) LOG.debugf("Cached item was locked: %s", key); - else LOG.debugf("Item already cached: %s", key); - return false; - } - finally { - cache.unlock(key); - } - } - - /** - * decrement a lock and put it back in the cache - */ - private void decrementLock(Object key, Lock lock) throws CacheException { - //decrement the lock - lock.unlock( cache.nextTimestamp() ); - cache.update(key, lock); - } - - /** - * Release the soft lock on the item. Other transactions may now - * re-cache the item (assuming that no other transaction holds a - * simultaneous lock). - */ - public synchronized void release(Object key, SoftLock clientLock) throws CacheException { - LOG.debugf("Releasing: %s", key); - - try { - cache.lock(key); - - Lockable lockable = (Lockable) cache.get(key); - if ( isUnlockable(clientLock, lockable) ) { - decrementLock(key, (Lock) lockable); - } - else { - handleLockExpiry(key); - } - } - finally { - cache.unlock(key); - } - } - - void handleLockExpiry(Object key) throws CacheException { - LOG.expired(key); - long ts = cache.nextTimestamp() + cache.getTimeout(); - // create new lock that times out immediately - Lock lock = new Lock( ts, nextLockId(), null ); - lock.unlock(ts); - cache.update(key, lock); - } - - public void clear() throws CacheException { - cache.clear(); - } - - public void remove(Object key) throws CacheException { - cache.remove(key); - } - - public void destroy() { - try { - cache.destroy(); - } - catch (Exception e) { - LOG.unableToDestroyCache(e.getMessage()); - } - } - - /** - * Re-cache the updated state, if and only if there there are - * no other concurrent soft locks. Release our lock. - */ - public synchronized boolean afterUpdate(Object key, Object value, Object version, SoftLock clientLock) - throws CacheException { - - LOG.debugf("Updating: %s", key); - - try { - cache.lock(key); - - Lockable lockable = (Lockable) cache.get(key); - if ( isUnlockable(clientLock, lockable) ) { - Lock lock = (Lock) lockable; - if ( lock.wasLockedConcurrently() ) { - // just decrement the lock, don't recache - // (we don't know which transaction won) - decrementLock(key, lock); - return false; - } - // recache the updated state - cache.update(key, new Item(value, version, cache.nextTimestamp())); - LOG.debugf("Updated: %s", key); - return true; - } - handleLockExpiry(key); - return false; - } - finally { - cache.unlock(key); - } - } - - /** - * Add the new item to the cache, checking that no other transaction has - * accessed the item. - */ - public synchronized boolean afterInsert(Object key, Object value, Object version) - throws CacheException { - - LOG.debugf("Inserting: %s", key); - try { - cache.lock(key); - - Lockable lockable = (Lockable) cache.get(key); - if (lockable==null) { - cache.update( key, new Item( value, version, cache.nextTimestamp() ) ); - LOG.debugf("Inserted: %s", key); - return true; - } - return false; - } - finally { - cache.unlock(key); - } - } - - /** - * Do nothing. - */ - public void evict(Object key) throws CacheException { - // noop - } - - /** - * Do nothing. - */ - public boolean insert(Object key, Object value, Object currentVersion) { - return false; - } - - /** - * Do nothing. - */ - public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) { - return false; - } - - /** - * Is the client's lock commensurate with the item in the cache? - * If it is not, we know that the cache expired the original - * lock. - */ - private boolean isUnlockable(SoftLock clientLock, Lockable myLock) - throws CacheException { - //null clientLock is remotely possible but will never happen in practice - return myLock!=null && - myLock.isLock() && - clientLock!=null && - ( (Lock) clientLock ).getId()==( (Lock) myLock ).getId(); - } - - public static interface Lockable { - public Lock lock(long timeout, int id); - public boolean isLock(); - public boolean isGettable(long txTimestamp); - public boolean isPuttable(long txTimestamp, Object newVersion, Comparator comparator); - } - - /** - * An item of cached data, timestamped with the time it was cached,. - * @see ReadWriteCache - */ - public static final class Item implements Serializable, Lockable { - - private final long freshTimestamp; - private final Object value; - private final Object version; - - public Item(Object value, Object version, long currentTimestamp) { - this.value = value; - this.version = version; - freshTimestamp = currentTimestamp; - } - /** - * The timestamp on the cached data - */ - public long getFreshTimestamp() { - return freshTimestamp; - } - /** - * The actual cached data - */ - public Object getValue() { - return value; - } - - /** - * Lock the item - */ - public Lock lock(long timeout, int id) { - return new Lock(timeout, id, version); - } - /** - * Not a lock! - */ - public boolean isLock() { - return false; - } - /** - * Is this item visible to the timestamped - * transaction? - */ - public boolean isGettable(long txTimestamp) { - return freshTimestamp < txTimestamp; - } - - /** - * Don't overwite already cached items - */ - public boolean isPuttable(long txTimestamp, Object newVersion, Comparator comparator) { - // we really could refresh the item if it - // is not a lock, but it might be slower - //return freshTimestamp < txTimestamp - return version!=null && comparator.compare(version, newVersion) < 0; - } - - @Override - public String toString() { - return "Item{version=" + version + - ",freshTimestamp=" + freshTimestamp; - } - } - - /** - * A soft lock which supports concurrent locking, - * timestamped with the time it was released - * @author Gavin King - */ - public static final class Lock implements Serializable, Lockable, SoftLock { - private long unlockTimestamp = -1; - private int multiplicity = 1; - private boolean concurrentLock = false; - private long timeout; - private final int id; - private final Object version; - - public Lock(long timeout, int id, Object version) { - this.timeout = timeout; - this.id = id; - this.version = version; - } - - public long getUnlockTimestamp() { - return unlockTimestamp; - } - /** - * Increment the lock, setting the - * new lock timeout - */ - public Lock lock(long timeout, int id) { - concurrentLock = true; - multiplicity++; - this.timeout = timeout; - return this; - } - /** - * Decrement the lock, setting the unlock - * timestamp if now unlocked - * @param currentTimestamp - */ - public void unlock(long currentTimestamp) { - if ( --multiplicity == 0 ) { - unlockTimestamp = currentTimestamp; - } - } - - /** - * Can the timestamped transaction re-cache this - * locked item now? - */ - public boolean isPuttable(long txTimestamp, Object newVersion, Comparator comparator) { - if (timeout < txTimestamp) return true; - if (multiplicity>0) return false; - return version==null ? - unlockTimestamp < txTimestamp : - comparator.compare(version, newVersion) < 0; //by requiring <, we rely on lock timeout in the case of an unsuccessful update! - } - - /** - * Was this lock held concurrently by multiple - * transactions? - */ - public boolean wasLockedConcurrently() { - return concurrentLock; - } - /** - * Yes, this is a lock - */ - public boolean isLock() { - return true; - } - /** - * locks are not returned to the client! - */ - public boolean isGettable(long txTimestamp) { - return false; - } - - public int getId() { return id; } - - @Override - public String toString() { - return "Lock{id=" + id + - ",version=" + version + - ",multiplicity=" + multiplicity + - ",unlockTimestamp=" + unlockTimestamp; - } - - } - - @Override - public String toString() { - return cache + "(read-write)"; - } - -} - - - - - - diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/RegionFactory.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/RegionFactory.java index 15df7d6f39..8cce5ee857 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/RegionFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/spi/RegionFactory.java @@ -38,12 +38,7 @@ import org.hibernate.service.Service; *

  • MyRegionFactoryImpl()
  • * * Use the first when we need to read config properties prior to - * {@link #start(Settings, Properties)} being called. For an example, have a look at - * {@link org.hibernate.cache.internal.bridge.RegionFactoryCacheProviderBridge} - * where we need the properties in order to determine which legacy - * {@link CacheProvider} to use so that we can answer the - * {@link #isMinimalPutsEnabledByDefault()} question for the - * {@link org.hibernate.cfg.SettingsFactory}. + * {@link #start(Settings, Properties)} being called. * * @author Steve Ebersole */ diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/TransactionalCache.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/TransactionalCache.java deleted file mode 100644 index f408404f30..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/TransactionalCache.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.spi; - -import java.util.Comparator; - -import org.jboss.logging.Logger; - -import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.access.SoftLock; -import org.hibernate.internal.CoreMessageLogger; - -/** - * Support for fully transactional cache implementations like - * JBoss TreeCache. Note that this might be a less scalable - * concurrency strategy than ReadWriteCache. This is - * a "synchronous" concurrency strategy. - * - * @author Gavin King - */ -@Deprecated -public class TransactionalCache implements CacheConcurrencyStrategy { - - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, TransactionalCache.class.getName()); - - private Cache cache; - - public String getRegionName() { - return cache.getRegionName(); - } - - public Object get(Object key, long txTimestamp) throws CacheException { - LOG.debugf("Cache lookup: %s", key); - Object result = cache.read( key ); - if (result == null) LOG.debugf("Cache miss: %s", key); - else LOG.debugf("Cache hit: %s", key); - return result; - } - - public boolean put( - Object key, - Object value, - long txTimestamp, - Object version, - Comparator versionComparator, - boolean minimalPut) throws CacheException { - if ( minimalPut && cache.read( key ) != null ) { - LOG.debugf("Item already cached: %s", key); - return false; - } - LOG.debugf("Caching: %s", key); - if ( cache instanceof OptimisticCache ) { - ( ( OptimisticCache ) cache ).writeLoad( key, value, version ); - } - else { - cache.put( key, value ); - } - return true; - } - - /** - * Do nothing, returning null. - */ - public SoftLock lock(Object key, Object version) throws CacheException { - //noop - return null; - } - - /** - * Do nothing. - */ - public void release(Object key, SoftLock clientLock) throws CacheException { - //noop - } - - public boolean update( - Object key, - Object value, - Object currentVersion, - Object previousVersion) throws CacheException { - LOG.debugf("Updating: %s", key); - if ( cache instanceof OptimisticCache ) { - ( ( OptimisticCache ) cache ).writeUpdate( key, value, currentVersion, previousVersion ); - } - else { - cache.update( key, value ); - } - return true; - } - - public boolean insert( - Object key, - Object value, - Object currentVersion) throws CacheException { - LOG.debugf("Inserting: %s", key); - if ( cache instanceof OptimisticCache ) { - ( ( OptimisticCache ) cache ).writeInsert( key, value, currentVersion ); - } - else { - cache.update( key, value ); - } - return true; - } - - public void evict(Object key) throws CacheException { - cache.remove( key ); - } - - public void remove(Object key) throws CacheException { - LOG.debugf("Removing: %s", key); - cache.remove( key ); - } - - public void clear() throws CacheException { - LOG.debugf("Clearing"); - cache.clear(); - } - - public void destroy() { - try { - cache.destroy(); - } - catch ( Exception e ) { - LOG.unableToDestroyCache(e.getMessage()); - } - } - - public void setCache(Cache cache) { - this.cache = cache; - } - - public Cache getCache() { - return cache; - } - - /** - * Do nothing. - */ - public boolean afterInsert( - Object key, - Object value, - Object version) throws CacheException { - return false; - } - - /** - * Do nothing. - */ - public boolean afterUpdate( - Object key, - Object value, - Object version, - SoftLock clientLock) throws CacheException { - return false; - } - - @Override - public String toString() { - return cache + "(transactional)"; - } - -} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/access/EntityRegionAccessStrategy.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/access/EntityRegionAccessStrategy.java index faf27cc846..8ffb684d91 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/access/EntityRegionAccessStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/spi/access/EntityRegionAccessStrategy.java @@ -104,37 +104,4 @@ public interface EntityRegionAccessStrategy extends RegionAccessStrategy{ * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} */ public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) throws CacheException; - - /** - * Called after an item has become stale (before the transaction completes). - * This method is used by "synchronous" concurrency strategies. - * - * @param key The key of the item to remove - * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} - */ - public void remove(Object key) throws CacheException; - - /** - * Called to evict data from the entire region - * - * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} - */ - public void removeAll() throws CacheException; - - /** - * Forcibly evict an item from the cache immediately without regard for transaction - * isolation. - * - * @param key The key of the item to remove - * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} - */ - public void evict(Object key) throws CacheException; - - /** - * Forcibly evict all items from the cache immediately without regard for transaction - * isolation. - * - * @throws CacheException Propogated from underlying {@link org.hibernate.cache.spi.Region} - */ - public void evictAll() throws CacheException; } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java index d502a96ba0..2d1becf941 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java @@ -288,13 +288,6 @@ public interface AvailableSettings { @Deprecated public static final String USER_TRANSACTION = "jta.UserTransaction"; - /** - * The CacheProvider implementation class - * - * @deprecated See {@link #CACHE_REGION_FACTORY} - */ - public static final String CACHE_PROVIDER = "hibernate.cache.provider_class"; - /** * The {@link org.hibernate.cache.spi.RegionFactory} implementation class */ diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java b/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java index 01928b1b96..651daf23bb 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Environment.java @@ -75,11 +75,6 @@ import org.hibernate.internal.util.config.ConfigurationHelper; * classname of org.hibernate.dialect.Dialect subclass * * - * hibernate.cache.provider_class - * classname of org.hibernate.cache.spi.CacheProvider - * subclass (if not specified EHCache is used) - * - * * hibernate.connection.provider_class * classname of org.hibernate.service.jdbc.connections.spi.ConnectionProvider * subclass (if not specified hueristics are used) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/ExternalSessionFactoryConfig.java b/hibernate-core/src/main/java/org/hibernate/cfg/ExternalSessionFactoryConfig.java index 327e9eb91b..1dbc61e1fa 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/ExternalSessionFactoryConfig.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/ExternalSessionFactoryConfig.java @@ -55,7 +55,7 @@ public abstract class ExternalSessionFactoryConfig { private String querySubstitutions; private String showSqlEnabled; private String commentsEnabled; - private String cacheProviderClass; + private String cacheRegionFactory; private String cacheProviderConfig; private String cacheRegionPrefix; private String secondLevelCacheEnabled; @@ -209,12 +209,12 @@ public abstract class ExternalSessionFactoryConfig { this.secondLevelCacheEnabled = secondLevelCacheEnabled; } - public final String getCacheProviderClass() { - return cacheProviderClass; + public final String getCacheRegionFactory() { + return cacheRegionFactory; } - public final void setCacheProviderClass(String cacheProviderClass) { - this.cacheProviderClass = cacheProviderClass; + public final void setCacheRegionFactory(String cacheRegionFactory) { + this.cacheRegionFactory = cacheRegionFactory; } public String getCacheProviderConfig() { @@ -287,7 +287,7 @@ public abstract class ExternalSessionFactoryConfig { setUnlessNull( props, Environment.QUERY_SUBSTITUTIONS, querySubstitutions ); setUnlessNull( props, Environment.SHOW_SQL, showSqlEnabled ); setUnlessNull( props, Environment.USE_SQL_COMMENTS, commentsEnabled ); - setUnlessNull( props, Environment.CACHE_PROVIDER, cacheProviderClass ); + setUnlessNull( props, Environment.CACHE_REGION_FACTORY, cacheRegionFactory ); setUnlessNull( props, Environment.CACHE_PROVIDER_CONFIG, cacheProviderConfig ); setUnlessNull( props, Environment.CACHE_REGION_PREFIX, cacheRegionPrefix ); setUnlessNull( props, Environment.USE_MINIMAL_PUTS, minimalPutsEnabled ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/SettingsFactory.java b/hibernate-core/src/main/java/org/hibernate/cfg/SettingsFactory.java index 1286675c25..d3fa342450 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/SettingsFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/SettingsFactory.java @@ -34,9 +34,9 @@ import org.hibernate.EntityMode; import org.hibernate.HibernateException; import org.hibernate.MultiTenancyStrategy; import org.hibernate.cache.internal.NoCachingRegionFactory; +import org.hibernate.cache.internal.StandardQueryCacheFactory; import org.hibernate.cache.spi.QueryCacheFactory; import org.hibernate.cache.spi.RegionFactory; -import org.hibernate.cache.internal.bridge.RegionFactoryCacheProviderBridge; import org.hibernate.engine.jdbc.spi.ExtractedDatabaseMetaData; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.transaction.spi.TransactionFactory; @@ -305,7 +305,7 @@ public class SettingsFactory implements Serializable { protected QueryCacheFactory createQueryCacheFactory(Properties properties, ServiceRegistry serviceRegistry) { String queryCacheFactoryClassName = ConfigurationHelper.getString( - Environment.QUERY_CACHE_FACTORY, properties, "org.hibernate.cache.internal.StandardQueryCacheFactory" + Environment.QUERY_CACHE_FACTORY, properties, StandardQueryCacheFactory.class.getName() ); LOG.debugf( "Query cache factory: %s", queryCacheFactoryClassName ); try { @@ -318,18 +318,11 @@ public class SettingsFactory implements Serializable { } } - public static RegionFactory createRegionFactory(Properties properties, boolean cachingEnabled, ServiceRegistry serviceRegistry) { + private static RegionFactory createRegionFactory(Properties properties, boolean cachingEnabled, ServiceRegistry serviceRegistry) { String regionFactoryClassName = ConfigurationHelper.getString( Environment.CACHE_REGION_FACTORY, properties, null ); - if ( regionFactoryClassName == null && cachingEnabled ) { - String providerClassName = ConfigurationHelper.getString( Environment.CACHE_PROVIDER, properties, null ); - if ( providerClassName != null ) { - // legacy behavior, apply the bridge... - regionFactoryClassName = RegionFactoryCacheProviderBridge.class.getName(); - } - } - if ( regionFactoryClassName == null ) { + if ( regionFactoryClassName == null || !cachingEnabled) { regionFactoryClassName = DEF_CACHE_REG_FACTORY; } LOG.debugf( "Cache region factory : %s", regionFactoryClassName ); @@ -355,56 +348,49 @@ public class SettingsFactory implements Serializable { throw new HibernateException( "could not instantiate RegionFactory [" + regionFactoryClassName + "]", e ); } } + //todo remove this once we move to new metamodel + public static RegionFactory createRegionFactory(Properties properties, boolean cachingEnabled) { + // todo : REMOVE! THIS IS TOTALLY A TEMPORARY HACK FOR org.hibernate.cfg.AnnotationBinder which will be going away + String regionFactoryClassName = ConfigurationHelper.getString( + Environment.CACHE_REGION_FACTORY, properties, null + ); + if ( regionFactoryClassName == null ) { + regionFactoryClassName = DEF_CACHE_REG_FACTORY; + } + LOG.debugf( "Cache region factory : %s", regionFactoryClassName ); + try { + try { + return (RegionFactory) org.hibernate.internal.util.ReflectHelper.classForName( regionFactoryClassName ) + .getConstructor( Properties.class ) + .newInstance( properties ); + } + catch ( NoSuchMethodException e ) { + // no constructor accepting Properties found, try no arg constructor + LOG.debugf( + "%s did not provide constructor accepting java.util.Properties; attempting no-arg constructor.", + regionFactoryClassName + ); + return (RegionFactory) org.hibernate.internal.util.ReflectHelper.classForName( regionFactoryClassName ) + .newInstance(); + } + } + catch ( Exception e ) { + throw new HibernateException( "could not instantiate RegionFactory [" + regionFactoryClassName + "]", e ); + } + } protected QueryTranslatorFactory createQueryTranslatorFactory(Properties properties, ServiceRegistry serviceRegistry) { String className = ConfigurationHelper.getString( Environment.QUERY_TRANSLATOR, properties, "org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory" ); - LOG.debugf( "Query translator: %s", className ); + LOG.debugf( "Query translator: %s", className ); try { return (QueryTranslatorFactory) serviceRegistry.getService( ClassLoaderService.class ) .classForName( className ) .newInstance(); } - catch (Exception e) { + catch ( Exception e ) { throw new HibernateException( "could not instantiate QueryTranslatorFactory: " + className, e ); } } - - public static RegionFactory createRegionFactory(Properties properties, boolean cachingEnabled) { - // todo : REMOVE! THIS IS TOTALLY A TEMPORARY HACK FOR org.hibernate.cfg.AnnotationBinder which will be going away - String regionFactoryClassName = ConfigurationHelper.getString( - Environment.CACHE_REGION_FACTORY, properties, null - ); - if ( regionFactoryClassName == null && cachingEnabled ) { - String providerClassName = ConfigurationHelper.getString( Environment.CACHE_PROVIDER, properties, null ); - if ( providerClassName != null ) { - // legacy behavior, apply the bridge... - regionFactoryClassName = RegionFactoryCacheProviderBridge.class.getName(); - } - } - if ( regionFactoryClassName == null ) { - regionFactoryClassName = DEF_CACHE_REG_FACTORY; - } - LOG.debugf( "Cache region factory : %s", regionFactoryClassName ); - try { - try { - return (RegionFactory) org.hibernate.internal.util.ReflectHelper.classForName( regionFactoryClassName ) - .getConstructor( Properties.class ) - .newInstance( properties ); - } - catch ( NoSuchMethodException e ) { - // no constructor accepting Properties found, try no arg constructor - LOG.debugf( - "%s did not provide constructor accepting java.util.Properties; attempting no-arg constructor.", - regionFactoryClassName - ); - return (RegionFactory) org.hibernate.internal.util.ReflectHelper.classForName( regionFactoryClassName ) - .newInstance(); - } - } - catch ( Exception e ) { - throw new HibernateException( "could not instantiate RegionFactory [" + regionFactoryClassName + "]", e ); - } - } } diff --git a/hibernate-core/src/main/java/org/hibernate/jmx/HibernateServiceMBean.java b/hibernate-core/src/main/java/org/hibernate/jmx/HibernateServiceMBean.java index 6321c7d01c..2d2913d7a3 100644 --- a/hibernate-core/src/main/java/org/hibernate/jmx/HibernateServiceMBean.java +++ b/hibernate-core/src/main/java/org/hibernate/jmx/HibernateServiceMBean.java @@ -222,11 +222,11 @@ public interface HibernateServiceMBean { /** * Get the second-level cache provider class name */ - public String getCacheProviderClass(); + public String getCacheRegionFactory(); /** * Set the second-level cache provider class name */ - public void setCacheProviderClass(String providerClassName); + public void setCacheRegionFactory(String cacheRegionFactory); /** * For cache providers which support this setting, get the * provider's specific configuration resource. diff --git a/hibernate-core/src/test/java/org/hibernate/test/legacy/CacheTest.java b/hibernate-core/src/test/java/org/hibernate/test/legacy/CacheTest.java deleted file mode 100644 index 8d95027816..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/legacy/CacheTest.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2007-2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.test.legacy; - -import org.hibernate.cache.spi.Cache; -import org.hibernate.cache.spi.CacheConcurrencyStrategy; -import org.hibernate.cache.spi.CacheProvider; -import org.hibernate.cache.internal.HashtableCacheProvider; -import org.hibernate.cache.spi.ReadWriteCache; -import org.hibernate.cache.spi.access.SoftLock; - -import org.junit.Test; - -import org.hibernate.testing.junit4.BaseUnitTestCase; - -import static org.junit.Assert.assertTrue; - -public class CacheTest extends BaseUnitTestCase { - @Test - public void testCaches() throws Exception { - doTestCache( new HashtableCacheProvider() ); - } - - public void doTestCache(CacheProvider cacheProvider) throws Exception { - - Cache cache = cacheProvider.buildCache( String.class.getName(), System.getProperties() ); - - long longBefore = cache.nextTimestamp(); - - Thread.sleep( 15 ); - - long before = cache.nextTimestamp(); - - Thread.sleep( 15 ); - - //cache.setTimeout(1000); - CacheConcurrencyStrategy ccs = new ReadWriteCache(); - ccs.setCache( cache ); - - // cache something - - assertTrue( ccs.put( "foo", "foo", before, null, null, false ) ); - - Thread.sleep( 15 ); - - long after = cache.nextTimestamp(); - - assertTrue( ccs.get( "foo", longBefore ) == null ); - assertTrue( ccs.get( "foo", after ).equals( "foo" ) ); - - assertTrue( !ccs.put( "foo", "foo", before, null, null, false ) ); - - // update it: - - SoftLock lock = ccs.lock( "foo", null ); - - assertTrue( ccs.get( "foo", after ) == null ); - assertTrue( ccs.get( "foo", longBefore ) == null ); - - assertTrue( !ccs.put( "foo", "foo", before, null, null, false ) ); - - Thread.sleep( 15 ); - - long whileLocked = cache.nextTimestamp(); - - assertTrue( !ccs.put( "foo", "foo", whileLocked, null, null, false ) ); - - Thread.sleep( 15 ); - - ccs.release( "foo", lock ); - - assertTrue( ccs.get( "foo", after ) == null ); - assertTrue( ccs.get( "foo", longBefore ) == null ); - - assertTrue( !ccs.put( "foo", "bar", whileLocked, null, null, false ) ); - assertTrue( !ccs.put( "foo", "bar", after, null, null, false ) ); - - Thread.sleep( 15 ); - - long longAfter = cache.nextTimestamp(); - - assertTrue( ccs.put( "foo", "baz", longAfter, null, null, false ) ); - - assertTrue( ccs.get( "foo", after ) == null ); - assertTrue( ccs.get( "foo", whileLocked ) == null ); - - Thread.sleep( 15 ); - - long longLongAfter = cache.nextTimestamp(); - - assertTrue( ccs.get( "foo", longLongAfter ).equals( "baz" ) ); - - // update it again, with multiple locks: - - SoftLock lock1 = ccs.lock( "foo", null ); - SoftLock lock2 = ccs.lock( "foo", null ); - - assertTrue( ccs.get( "foo", longLongAfter ) == null ); - - Thread.sleep( 15 ); - - whileLocked = cache.nextTimestamp(); - - assertTrue( !ccs.put( "foo", "foo", whileLocked, null, null, false ) ); - - Thread.sleep( 15 ); - - ccs.release( "foo", lock2 ); - - Thread.sleep( 15 ); - - long betweenReleases = cache.nextTimestamp(); - - assertTrue( !ccs.put( "foo", "bar", betweenReleases, null, null, false ) ); - assertTrue( ccs.get( "foo", betweenReleases ) == null ); - - Thread.sleep( 15 ); - - ccs.release( "foo", lock1 ); - - assertTrue( !ccs.put( "foo", "bar", whileLocked, null, null, false ) ); - - Thread.sleep( 15 ); - - longAfter = cache.nextTimestamp(); - - assertTrue( ccs.put( "foo", "baz", longAfter, null, null, false ) ); - assertTrue( ccs.get( "foo", whileLocked ) == null ); - - Thread.sleep( 15 ); - - longLongAfter = cache.nextTimestamp(); - - assertTrue( ccs.get( "foo", longLongAfter ).equals( "baz" ) ); - - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/multitenancy/schema/SchemaBasedMultiTenancyTest.java b/hibernate-core/src/test/java/org/hibernate/test/multitenancy/schema/SchemaBasedMultiTenancyTest.java index ed38df34a7..9cb6d6fa80 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/multitenancy/schema/SchemaBasedMultiTenancyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/multitenancy/schema/SchemaBasedMultiTenancyTest.java @@ -29,7 +29,6 @@ import java.sql.SQLException; import org.hibernate.HibernateException; import org.hibernate.MultiTenancyStrategy; import org.hibernate.Session; -import org.hibernate.cache.internal.HashtableCacheProvider; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.engine.spi.SessionFactoryImplementor; @@ -40,6 +39,7 @@ import org.hibernate.service.jdbc.connections.spi.AbstractMultiTenantConnectionP import org.hibernate.service.jdbc.connections.spi.ConnectionProvider; import org.hibernate.service.jdbc.connections.spi.MultiTenantConnectionProvider; import org.hibernate.service.spi.ServiceRegistryImplementor; +import org.hibernate.testing.cache.CachingRegionFactory; import org.hibernate.tool.hbm2ddl.ConnectionHelper; import org.hibernate.tool.hbm2ddl.SchemaExport; @@ -86,7 +86,7 @@ public class SchemaBasedMultiTenancyTest extends BaseUnitTestCase { Configuration cfg = new Configuration(); cfg.getProperties().put( Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE ); - cfg.setProperty( Environment.CACHE_PROVIDER, HashtableCacheProvider.class.getName() ); + cfg.setProperty( Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName() ); cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); cfg.addAnnotatedClass( Customer.class ); diff --git a/hibernate-core/src/test/resources/hibernate.properties b/hibernate-core/src/test/resources/hibernate.properties index 721eef9ff8..fb9cc13c6a 100644 --- a/hibernate-core/src/test/resources/hibernate.properties +++ b/hibernate-core/src/test/resources/hibernate.properties @@ -33,4 +33,4 @@ hibernate.show_sql true hibernate.max_fetch_depth 5 hibernate.cache.region_prefix hibernate.test -hibernate.cache.provider_class org.hibernate.cache.internal.HashtableCacheProvider +hibernate.cache.region.factory_class org.hibernate.testing.cache.CachingRegionFactory diff --git a/hibernate-core/src/test/resources/log4j.properties b/hibernate-core/src/test/resources/log4j.properties index 55c7e4968d..1f7070d830 100644 --- a/hibernate-core/src/test/resources/log4j.properties +++ b/hibernate-core/src/test/resources/log4j.properties @@ -7,4 +7,4 @@ log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.rootLogger=info, stdout log4j.logger.org.hibernate.tool.hbm2ddl=debug - +log4j.logger.org.hibernate.testing.cache=debug diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/internal/EhCache.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/internal/EhCache.java deleted file mode 100644 index cf8173f459..0000000000 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/internal/EhCache.java +++ /dev/null @@ -1,274 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors.  All third-party contributions are - * distributed under license by Red Hat Middleware LLC. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.internal; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import net.sf.ehcache.CacheManager; -import net.sf.ehcache.Element; -import org.jboss.logging.Logger; - -import org.hibernate.cache.spi.Cache; -import org.hibernate.cache.CacheException; - -/** - * EHCache plugin for Hibernate - *

    - * EHCache uses a {@link net.sf.ehcache.store.MemoryStore} and a - * {@link net.sf.ehcache.store.DiskStore}. - * The {@link net.sf.ehcache.store.DiskStore} requires that both keys and values be {@link java.io.Serializable}. - * However the MemoryStore does not and in ehcache-1.2 nonSerializable Objects are permitted. They are discarded - * if an attempt it made to overflow them to Disk or to replicate them to remote cache peers. - * - * @author Greg Luck - * @author Emmanuel Bernard - */ -@Deprecated -public class EhCache implements Cache { - - private static final EhCacheMessageLogger LOG = Logger.getMessageLogger(EhCacheMessageLogger.class, EhCache.class.getName()); - - private static final int SIXTY_THOUSAND_MS = 60000; - - private net.sf.ehcache.Ehcache cache; - - /** - * Creates a new Hibernate pluggable cache based on a cache name. - *

    - * - * @param cache The underlying EhCache instance to use. - */ - public EhCache(net.sf.ehcache.Ehcache cache) { - this.cache = cache; - } - - /** - * Gets a value of an element which matches the given key. - * - * @param key the key of the element to return. - * @return The value placed into the cache with an earlier put, or null if not found or expired - * @throws org.hibernate.cache.CacheException - */ - public Object get(Object key) throws CacheException { - try { - LOG.debugf("Key: %s", key); - if (key == null) return null; - Element element = cache.get(key); - if (element == null) { - LOG.debugf("Element for %s is null", key); - return null; - } - return element.getObjectValue(); - } - catch (net.sf.ehcache.CacheException e) { - throw new CacheException( e ); - } - } - - public Object read(Object key) throws CacheException { - return get( key ); - } - - - /** - * Puts an object into the cache. - * - * @param key a key - * @param value a value - * @throws CacheException if the {@link CacheManager} - * is shutdown or another {@link Exception} occurs. - */ - public void update(Object key, Object value) throws CacheException { - put( key, value ); - } - - /** - * Puts an object into the cache. - * - * @param key a key - * @param value a value - * @throws CacheException if the {@link CacheManager} - * is shutdown or another {@link Exception} occurs. - */ - public void put(Object key, Object value) throws CacheException { - try { - Element element = new Element( key, value ); - cache.put( element ); - } - catch (IllegalArgumentException e) { - throw new CacheException( e ); - } - catch (IllegalStateException e) { - throw new CacheException( e ); - } - catch (net.sf.ehcache.CacheException e) { - throw new CacheException( e ); - } - - } - - /** - * Removes the element which matches the key. - *

    - * If no element matches, nothing is removed and no Exception is thrown. - * - * @param key the key of the element to remove - * @throws CacheException - */ - public void remove(Object key) throws CacheException { - try { - cache.remove( key ); - } - catch (ClassCastException e) { - throw new CacheException( e ); - } - catch (IllegalStateException e) { - throw new CacheException( e ); - } - catch (net.sf.ehcache.CacheException e) { - throw new CacheException( e ); - } - } - - /** - * Remove all elements in the cache, but leave the cache - * in a useable state. - * - * @throws CacheException - */ - public void clear() throws CacheException { - try { - cache.removeAll(); - } - catch (IllegalStateException e) { - throw new CacheException( e ); - } - catch (net.sf.ehcache.CacheException e) { - throw new CacheException( e ); - } - } - - /** - * Remove the cache and make it unuseable. - * - * @throws CacheException - */ - public void destroy() throws CacheException { - try { - cache.getCacheManager().removeCache( cache.getName() ); - } - catch (IllegalStateException e) { - throw new CacheException( e ); - } - catch (net.sf.ehcache.CacheException e) { - throw new CacheException( e ); - } - } - - /** - * Calls to this method should perform there own synchronization. - * It is provided for distributed caches. Because EHCache is not distributed - * this method does nothing. - */ - public void lock(Object key) throws CacheException { - } - - /** - * Calls to this method should perform there own synchronization. - * It is provided for distributed caches. Because EHCache is not distributed - * this method does nothing. - */ - public void unlock(Object key) throws CacheException { - } - - /** - * Gets the next timestamp; - */ - public long nextTimestamp() { - return Timestamper.next(); - } - - /** - * Returns the lock timeout for this cache. - */ - public int getTimeout() { - // 60 second lock timeout - return Timestamper.ONE_MS * SIXTY_THOUSAND_MS; - } - - public String getRegionName() { - return cache.getName(); - } - - /** - * Warning: This method can be very expensive to run. Allow approximately 1 second - * per 1MB of entries. Running this method could create liveness problems - * because the object lock is held for a long period - *

    - * - * @return the approximate size of memory ehcache is using for the MemoryStore for this cache - */ - public long getSizeInMemory() { - try { - return cache.calculateInMemorySize(); - } - catch (Throwable t) { - return -1; - } - } - - public long getElementCountInMemory() { - try { - return cache.getMemoryStoreSize(); - } - catch (net.sf.ehcache.CacheException ce) { - throw new CacheException( ce ); - } - } - - public long getElementCountOnDisk() { - return cache.getDiskStoreSize(); - } - - public Map toMap() { - try { - Map result = new HashMap(); - Iterator iter = cache.getKeys().iterator(); - while ( iter.hasNext() ) { - Object key = iter.next(); - result.put( key, cache.get( key ).getObjectValue() ); - } - return result; - } - catch (Exception e) { - throw new CacheException( e ); - } - } - - @Override - public String toString() { - return "EHCache(" + getRegionName() + ')'; - } - -} \ No newline at end of file diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/internal/EhCacheProvider.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/internal/EhCacheProvider.java deleted file mode 100644 index 5e5e367a28..0000000000 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/internal/EhCacheProvider.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2007-2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.cache.internal; - -import java.net.URL; -import java.util.Properties; -import net.sf.ehcache.CacheManager; - -import org.hibernate.cache.spi.Cache; -import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.CacheProvider; -import org.hibernate.cfg.Environment; -import org.hibernate.internal.util.ConfigHelper; -import org.hibernate.internal.util.StringHelper; -import org.jboss.logging.Logger; - -/** - * Cache Provider plugin for Hibernate - * - * Use hibernate.cache.provider_class=org.hibernate.cache.internal.EhCacheProvider - * in Hibernate 3.x or later - * - * Taken from EhCache 0.9 distribution - * @author Greg Luck - * @author Emmanuel Bernard - */ -/** - * Cache Provider plugin for ehcache-1.2. New in this provider are ehcache support for multiple - * Hibernate session factories, each with its own ehcache configuration, and non Serializable keys and values. - * Ehcache-1.2 also has many other features such as cluster support and listeners, which can be used seamlessly simply - * by configurion in ehcache.xml. - *

    - * Use hibernate.cache.provider_class=org.hibernate.cache.internal.EhCacheProvider in the Hibernate configuration - * to enable this provider for Hibernate's second level cache. - *

    - * When configuring multiple ehcache CacheManagers, as you would where you have multiple Hibernate Configurations and - * multiple SessionFactories, specify in each Hibernate configuration the ehcache configuration using - * the property hibernate.cache.provider_configuration_file_resource_path An example to set an ehcache configuration - * called ehcache-2.xml would be hibernate.cache.provider_configuration_file_resource_path=/ehcache-2.xml. If the leading - * slash is not there one will be added. The configuration file will be looked for in the root of the classpath. - *

    - * Updated for ehcache-1.2. Note this provider requires ehcache-1.2.jar. Make sure ehcache-1.1.jar or earlier - * is not in the classpath or it will not work. - *

    - * See http://ehcache.sf.net for documentation on ehcache - *

    - * - * @author Greg Luck - * @author Emmanuel Bernard - */ -@Deprecated -public class EhCacheProvider implements CacheProvider { - - private static final EhCacheMessageLogger LOG = Logger.getMessageLogger(EhCacheMessageLogger.class, EhCacheProvider.class.getName()); - - private CacheManager manager; - - /** - * Builds a Cache. - *

    - * Even though this method provides properties, they are not used. - * Properties for EHCache are specified in the ehcache.xml file. - * Configuration will be read from ehcache.xml for a cache declaration - * where the name attribute matches the name parameter in this builder. - * - * @param name the name of the cache. Must match a cache configured in ehcache.xml - * @param properties not used - * @return a newly built cache will be built and initialised - * @throws org.hibernate.cache.CacheException inter alia, if a cache of the same name already exists - */ - public Cache buildCache(String name, Properties properties) throws CacheException { - try { - net.sf.ehcache.Cache cache = manager.getCache(name); - if (cache == null) { - LOG.unableToFindConfiguration(name); - manager.addCache(name); - cache = manager.getCache(name); - LOG.debugf("Started EHCache region: %s", name); - } - return new EhCache(cache); - } - catch (net.sf.ehcache.CacheException e) { - throw new CacheException(e); - } - } - - /** - * Returns the next timestamp. - */ - public long nextTimestamp() { - return Timestamper.next(); - } - - /** - * Callback to perform any necessary initialization of the underlying cache implementation - * during SessionFactory construction. - * - * @param properties current configuration settings. - */ - public void start(Properties properties) throws CacheException { - if (manager != null) { - LOG.attemptToRestartAlreadyStartedEhCacheProvider(); - return; - } - try { - String configurationResourceName = null; - if (properties != null) { - configurationResourceName = (String) properties.get( Environment.CACHE_PROVIDER_CONFIG ); - } - if ( StringHelper.isEmpty( configurationResourceName ) ) { - manager = new CacheManager(); - } else { - URL url = loadResource(configurationResourceName); - manager = new CacheManager(url); - } - } catch (net.sf.ehcache.CacheException e) { - //yukky! Don't you have subclasses for that! - //TODO race conditions can happen here - if (e.getMessage().startsWith("Cannot parseConfiguration CacheManager. Attempt to create a new instance of " + - "CacheManager using the diskStorePath")) { - throw new CacheException("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() " + - " between repeated calls to buildSessionFactory. Consider using net.sf.ehcache.hibernate.SingletonEhCacheProvider." - , e ); - } - throw e; - } - } - - private URL loadResource(String configurationResourceName) { - URL url = ConfigHelper.locateConfig( configurationResourceName ); - LOG.debugf("Creating EhCacheProvider from a specified resource: %s Resolved to URL: %s", configurationResourceName, url); - return url; - } - - /** - * Callback to perform any necessary cleanup of the underlying cache implementation - * during SessionFactory.close(). - */ - public void stop() { - if (manager != null) { - manager.shutdown(); - manager = null; - } - } - - public boolean isMinimalPutsEnabledByDefault() { - return false; - } - -} diff --git a/hibernate-ehcache/src/main/java/org/hibernate/cache/internal/SingletonEhCacheProvider.java b/hibernate-ehcache/src/main/java/org/hibernate/cache/internal/SingletonEhCacheProvider.java deleted file mode 100644 index 86eec961ed..0000000000 --- a/hibernate-ehcache/src/main/java/org/hibernate/cache/internal/SingletonEhCacheProvider.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Middleware LLC. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program 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 distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - * - */ -package org.hibernate.cache.internal; -import java.net.URL; -import java.util.Properties; -import net.sf.ehcache.CacheManager; -import net.sf.ehcache.util.ClassLoaderUtil; -import org.jboss.logging.Logger; -import org.jboss.logging.Logger.Level; - -import org.hibernate.cache.spi.Cache; -import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.CacheProvider; - -/** - * Singleton cache Provider plugin for Hibernate 3.2 and ehcache-1.2. New in this provider is support for - * non Serializable keys and values. This provider works as a Singleton. No matter how many Hibernate Configurations - * you have, only one ehcache CacheManager is used. See EhCacheProvider for a non-singleton implementation. - *

    - * Ehcache-1.2 also has many other features such as cluster support and listeners, which can be used seamlessly simply - * by configurion in ehcache.xml. - *

    - * Use hibernate.cache.provider_class=net.sf.ehcache.hibernate.SingletonEhCacheProvider in the Hibernate configuration - * to enable this provider for Hibernate's second level cache. - *

    - * Updated for ehcache-1.2. Note this provider requires ehcache-1.2.jar. Make sure ehcache-1.1.jar or earlier - * is not in the classpath or it will not work. - *

    - * See http://ehcache.sf.net for documentation on ehcache - *

    - * - * @author Greg Luck - * @author Emmanuel Bernard - * @version $Id: SingletonEhCacheProvider.java 744 2008-08-16 20:10:49Z gregluck $ - */ -@Deprecated -public final class SingletonEhCacheProvider implements CacheProvider { - - /** - * The Hibernate system property specifying the location of the ehcache configuration file name. - *

    - * If set to say ehcache-1.xml, ehcache-1.xml will be looked for in the root of the classpath. - */ - public static final String NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME = "net.sf.ehcache.configurationResourceName"; - - private static final EhCacheMessageLogger LOG = Logger.getMessageLogger(EhCacheMessageLogger.class, SingletonEhCacheProvider.class.getName()); - - /** - * To be backwardly compatible with a lot of Hibernate code out there, allow multiple starts and stops on the - * one singleton CacheManager. Keep a count of references to only stop on when only one reference is held. - */ - private static int referenceCount; - - private CacheManager manager; - - - /** - * Builds a Cache. - *

    - * Even though this method provides properties, they are not used. - * Properties for EHCache are specified in the ehcache.xml file. - * Configuration will be read from ehcache.xml for a cache declaration - * where the name attribute matches the name parameter in this builder. - * - * @param name the name of the cache. Must match a cache configured in ehcache.xml - * @param properties not used - * - * @return a newly built cache will be built and initialised - * - * @throws org.hibernate.cache.CacheException - * inter alia, if a cache of the same name already exists - */ - public final Cache buildCache(String name, Properties properties) throws CacheException { - try { - net.sf.ehcache.Ehcache cache = manager.getEhcache( name ); - if ( cache == null ) { - LOG.unableToFindEhCacheConfiguration(name); - manager.addCache( name ); - cache = manager.getEhcache( name ); - LOG.debugf("Started EHCache region: %s", name); - } - return new EhCache( cache ); - } - catch ( net.sf.ehcache.CacheException e ) { - throw new CacheException( e ); - } - } - - /** - * Returns the next timestamp. - */ - public final long nextTimestamp() { - return Timestamper.next(); - } - - /** - * Callback to perform any necessary initialization of the underlying cache implementation - * during SessionFactory construction. - *

    - * - * @param properties current configuration settings. - */ - public final void start(Properties properties) throws CacheException { - String configurationResourceName = null; - if ( properties != null ) { - configurationResourceName = ( String ) properties.get( NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME ); - } - if ( configurationResourceName == null || configurationResourceName.length() == 0 ) { - manager = CacheManager.create(); - referenceCount++; - } - else { - if ( !configurationResourceName.startsWith( "/" ) ) { - configurationResourceName = "/" + configurationResourceName; - if (LOG.isDebugEnabled()) { - LOG.debugf("Prepending / to %s. It should be placed in the root of the classpath rather than in a package.", - configurationResourceName); - } - } - URL url = loadResource( configurationResourceName ); - manager = CacheManager.create( url ); - referenceCount++; - } - } - - private URL loadResource(String configurationResourceName) { - ClassLoader standardClassloader = ClassLoaderUtil.getStandardClassLoader(); - URL url = null; - if (standardClassloader != null) url = standardClassloader.getResource(configurationResourceName); - if (url == null) url = this.getClass().getResource(configurationResourceName); - if (LOG.isDebugEnabled()) LOG.debugf("Creating EhCacheProvider from a specified resource: %s Resolved to URL: %s", - configurationResourceName, - url); - if (url == null && LOG.isEnabled(Level.WARN)) LOG.unableToLoadConfiguration(configurationResourceName); - return url; - } - - /** - * Callback to perform any necessary cleanup of the underlying cache implementation - * during SessionFactory.close(). - */ - public void stop() { - if ( manager != null ) { - referenceCount--; - if ( referenceCount == 0 ) { - manager.shutdown(); - } - manager = null; - } - } - - /** - * Not sure what this is supposed to do. - * - * @return false to be safe - */ - public final boolean isMinimalPutsEnabledByDefault() { - return false; - } - -} diff --git a/hibernate-ehcache/src/test/java/org/hibernate/test/cache/ehcache/EhCacheRegionTest.java b/hibernate-ehcache/src/test/java/org/hibernate/test/cache/ehcache/EhCacheRegionTest.java index 0ae210e9b8..62588a4ed6 100644 --- a/hibernate-ehcache/src/test/java/org/hibernate/test/cache/ehcache/EhCacheRegionTest.java +++ b/hibernate-ehcache/src/test/java/org/hibernate/test/cache/ehcache/EhCacheRegionTest.java @@ -1,31 +1,31 @@ package org.hibernate.test.cache.ehcache; -import org.hibernate.cache.spi.ReadWriteCache.Item; -import org.hibernate.cache.internal.EhCacheProvider; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import java.util.Map; +import net.sf.ehcache.hibernate.EhCacheRegionFactory; + /** * @author Alex Snaps */ public class EhCacheRegionTest extends EhCacheTest { @Override protected void configCache(final Configuration cfg) { - cfg.setProperty( Environment.CACHE_PROVIDER, EhCacheProvider.class.getName() ); + cfg.setProperty( Environment.CACHE_REGION_FACTORY, EhCacheRegionFactory.class.getName() ); cfg.setProperty( Environment.CACHE_PROVIDER_CONFIG, "ehcache.xml" ); } @Override protected Map getMapFromCacheEntry(final Object entry) { final Map map; - if ( entry instanceof Item ) { - map = (Map) ( (Item) entry ).getValue(); - } - else { +// if ( entry instanceof Item ) { +// map = (Map) ( (Item) entry ).getValue(); +// } +// else { map = (Map) entry; - } +// } return map; } } diff --git a/hibernate-ehcache/src/test/java/org/hibernate/test/cache/ehcache/EhCacheTest.java b/hibernate-ehcache/src/test/java/org/hibernate/test/cache/ehcache/EhCacheTest.java index 8df295409f..c0190c8b0c 100644 --- a/hibernate-ehcache/src/test/java/org/hibernate/test/cache/ehcache/EhCacheTest.java +++ b/hibernate-ehcache/src/test/java/org/hibernate/test/cache/ehcache/EhCacheTest.java @@ -26,8 +26,6 @@ package org.hibernate.test.cache.ehcache; import java.util.Map; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.cache.internal.EhCacheProvider; -import org.hibernate.cache.spi.ReadWriteCache; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory; @@ -184,10 +182,11 @@ public abstract class EhCacheTest extends BaseCoreFunctionalTestCase { Object entry = slcs.getEntries().get( item.getId() ); Long cachedVersionValue; - if ( entry instanceof ReadWriteCache.Lock ) { - //FIXME don't know what to test here - cachedVersionValue = Long.valueOf( ((ReadWriteCache.Lock) entry).getUnlockTimestamp() ); - } else if(entry.getClass().getName().equals("net.sf.ehcache.hibernate.strategy.AbstractReadWriteEhcacheAccessStrategy$Lock")) { +// if ( entry instanceof ReadWriteCache.Lock ) { +// //FIXME don't know what to test here +// cachedVersionValue = Long.valueOf( ((ReadWriteCache.Lock) entry).getUnlockTimestamp() ); +// } else + if(entry.getClass().getName().equals("net.sf.ehcache.hibernate.strategy.AbstractReadWriteEhcacheAccessStrategy$Lock")) { //FIXME don't know what to test here } else { cachedVersionValue = ( Long ) getMapFromCacheEntry(entry).get( "_version" ); diff --git a/hibernate-entitymanager/src/test/bundles/cfgxmlpar/org/hibernate/ejb/test/pack/cfgxmlpar/hibernate.cfg.xml b/hibernate-entitymanager/src/test/bundles/cfgxmlpar/org/hibernate/ejb/test/pack/cfgxmlpar/hibernate.cfg.xml index afcd6d6778..9a806c5241 100644 --- a/hibernate-entitymanager/src/test/bundles/cfgxmlpar/org/hibernate/ejb/test/pack/cfgxmlpar/hibernate.cfg.xml +++ b/hibernate-entitymanager/src/test/bundles/cfgxmlpar/org/hibernate/ejb/test/pack/cfgxmlpar/hibernate.cfg.xml @@ -16,7 +16,7 @@ 3 create-drop true - org.hibernate.cache.internal.HashtableCacheProvider + org.hibernate.testing.cache.CachingRegionFactory diff --git a/hibernate-entitymanager/src/test/bundles/defaultpar/META-INF/persistence.xml b/hibernate-entitymanager/src/test/bundles/defaultpar/META-INF/persistence.xml index 5172fcaa65..bc9e2a2052 100644 --- a/hibernate-entitymanager/src/test/bundles/defaultpar/META-INF/persistence.xml +++ b/hibernate-entitymanager/src/test/bundles/defaultpar/META-INF/persistence.xml @@ -14,7 +14,7 @@ - + diff --git a/hibernate-entitymanager/src/test/bundles/defaultpar_1_0/META-INF/persistence.xml b/hibernate-entitymanager/src/test/bundles/defaultpar_1_0/META-INF/persistence.xml index ed4065d754..c64713de3a 100644 --- a/hibernate-entitymanager/src/test/bundles/defaultpar_1_0/META-INF/persistence.xml +++ b/hibernate-entitymanager/src/test/bundles/defaultpar_1_0/META-INF/persistence.xml @@ -13,7 +13,7 @@ - + diff --git a/hibernate-entitymanager/src/test/bundles/excludehbmpar/META-INF/persistence.xml b/hibernate-entitymanager/src/test/bundles/excludehbmpar/META-INF/persistence.xml index 7e0e9a8755..8b77ab369d 100644 --- a/hibernate-entitymanager/src/test/bundles/excludehbmpar/META-INF/persistence.xml +++ b/hibernate-entitymanager/src/test/bundles/excludehbmpar/META-INF/persistence.xml @@ -13,7 +13,7 @@ - + diff --git a/hibernate-entitymanager/src/test/bundles/explicitpar/META-INF/persistence.xml b/hibernate-entitymanager/src/test/bundles/explicitpar/META-INF/persistence.xml index 25d45dbd85..34eca4c6da 100644 --- a/hibernate-entitymanager/src/test/bundles/explicitpar/META-INF/persistence.xml +++ b/hibernate-entitymanager/src/test/bundles/explicitpar/META-INF/persistence.xml @@ -26,7 +26,7 @@ - + diff --git a/hibernate-entitymanager/src/test/bundles/explodedpar/META-INF/persistence.xml b/hibernate-entitymanager/src/test/bundles/explodedpar/META-INF/persistence.xml index 7420f88654..1c40c44874 100644 --- a/hibernate-entitymanager/src/test/bundles/explodedpar/META-INF/persistence.xml +++ b/hibernate-entitymanager/src/test/bundles/explodedpar/META-INF/persistence.xml @@ -12,7 +12,7 @@ - + diff --git a/hibernate-entitymanager/src/test/bundles/overridenpar/META-INF/persistence.xml b/hibernate-entitymanager/src/test/bundles/overridenpar/META-INF/persistence.xml index 75e34638a5..84f4d72bac 100644 --- a/hibernate-entitymanager/src/test/bundles/overridenpar/META-INF/persistence.xml +++ b/hibernate-entitymanager/src/test/bundles/overridenpar/META-INF/persistence.xml @@ -8,7 +8,7 @@ - + diff --git a/hibernate-entitymanager/src/test/bundles/space par/META-INF/persistence.xml b/hibernate-entitymanager/src/test/bundles/space par/META-INF/persistence.xml index 879b62bb74..f7ffd56cc5 100644 --- a/hibernate-entitymanager/src/test/bundles/space par/META-INF/persistence.xml +++ b/hibernate-entitymanager/src/test/bundles/space par/META-INF/persistence.xml @@ -12,7 +12,7 @@ - + diff --git a/hibernate-entitymanager/src/test/bundles/war/WEB-INF/classes/META-INF/persistence.xml b/hibernate-entitymanager/src/test/bundles/war/WEB-INF/classes/META-INF/persistence.xml index 6b575739ee..0c5d311825 100644 --- a/hibernate-entitymanager/src/test/bundles/war/WEB-INF/classes/META-INF/persistence.xml +++ b/hibernate-entitymanager/src/test/bundles/war/WEB-INF/classes/META-INF/persistence.xml @@ -13,7 +13,7 @@ - + diff --git a/hibernate-entitymanager/src/test/resources/hibernate.properties b/hibernate-entitymanager/src/test/resources/hibernate.properties index 1425248df7..8b06686d9f 100644 --- a/hibernate-entitymanager/src/test/resources/hibernate.properties +++ b/hibernate-entitymanager/src/test/resources/hibernate.properties @@ -33,6 +33,6 @@ hibernate.show_sql true hibernate.max_fetch_depth 5 hibernate.cache.region_prefix hibernate.test -hibernate.cache.provider_class org.hibernate.cache.internal.HashtableCacheProvider +hibernate.cache.region.factory_class org.hibernate.testing.cache.CachingRegionFactory hibernate.jdbc.batch_size 0 diff --git a/hibernate-entitymanager/src/test/resources/org/hibernate/ejb/test/hibernate.cfg.xml b/hibernate-entitymanager/src/test/resources/org/hibernate/ejb/test/hibernate.cfg.xml index 6de66dd45c..c540676068 100644 --- a/hibernate-entitymanager/src/test/resources/org/hibernate/ejb/test/hibernate.cfg.xml +++ b/hibernate-entitymanager/src/test/resources/org/hibernate/ejb/test/hibernate.cfg.xml @@ -16,7 +16,7 @@ 3 create-drop true - org.hibernate.cache.internal.HashtableCacheProvider + org.hibernate.testing.cache.CachingRegionFactory diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/cache/HibernateSecLvlQueryCache.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/cache/HibernateSecLvlQueryCache.java index a3a399f54b..817f614ed6 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/cache/HibernateSecLvlQueryCache.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/cache/HibernateSecLvlQueryCache.java @@ -1,7 +1,7 @@ package org.hibernate.envers.test.integration.cache; import org.hibernate.MappingException; -import org.hibernate.cache.internal.EhCacheProvider; +import org.hibernate.cache.internal.EhCacheRegionFactory; import org.hibernate.cfg.Environment; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.envers.RevisionType; @@ -26,7 +26,7 @@ public class HibernateSecLvlQueryCache extends AbstractSessionTest { config.addAnnotatedClass(StrTestEntity.class); config.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true"); config.setProperty(Environment.USE_QUERY_CACHE, "true"); - config.setProperty(Environment.CACHE_PROVIDER, EhCacheProvider.class.getName()); + config.setProperty(Environment.CACHE_REGION_FACTORY, EhCacheRegionFactory.class.getName()); config.setProperty(Environment.CACHE_PROVIDER_CONFIG, "ehcache-test.xml"); } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/AbstractReadWriteAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/AbstractReadWriteAccessStrategy.java new file mode 100644 index 0000000000..62b9cc960e --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/AbstractReadWriteAccessStrategy.java @@ -0,0 +1,360 @@ +package org.hibernate.testing.cache; + +import java.io.Serializable; +import java.util.Comparator; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.jboss.logging.Logger; + +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.internal.CoreMessageLogger; + +/** + * @author Strong Liu + */ +abstract class AbstractReadWriteAccessStrategy extends BaseRegionAccessStrategy { + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, AbstractReadWriteAccessStrategy.class.getName() + ); + private final UUID uuid = UUID.randomUUID(); + private final AtomicLong nextLockId = new AtomicLong(); + private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(); + protected java.util.concurrent.locks.Lock readLock = reentrantReadWriteLock.readLock(); + protected java.util.concurrent.locks.Lock writeLock = reentrantReadWriteLock.writeLock(); + + /** + * Returns null if the item is not readable. Locked items are not readable, nor are items created + * after the start of this transaction. + */ + @Override + public final Object get(Object key, long txTimestamp) throws CacheException { + try { + readLock.lock(); + Lockable item = (Lockable) getInternalRegion().get( key ); + + boolean readable = item != null && item.isReadable( txTimestamp ); + if ( readable ) { + return item.getValue(); + } + else { + return null; + } + } + finally { + readLock.unlock(); + } + } + + abstract Comparator getVersionComparator(); + + /** + * Returns false and fails to put the value if there is an existing un-writeable item mapped to this + * key. + */ + @Override + public final boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride) + throws CacheException { + try { + writeLock.lock(); + Lockable item = (Lockable) getInternalRegion().get( key ); + boolean writeable = item == null || item.isWriteable( txTimestamp, version, getVersionComparator() ); + if ( writeable ) { + getInternalRegion().put( key, new Item( value, version, getInternalRegion().nextTimestamp() ) ); + return true; + } + else { + return false; + } + } + finally { + writeLock.unlock(); + } + } + + /** + * Soft-lock a cache item. + */ + public final SoftLock lockItem(Object key, Object version) throws CacheException { + + try { + writeLock.lock(); + Lockable item = (Lockable) getInternalRegion().get( key ); + long timeout = getInternalRegion().nextTimestamp() + getInternalRegion().getTimeout(); + final Lock lock = ( item == null ) ? new Lock( timeout, uuid, nextLockId(), version ) : item.lock( + timeout, + uuid, + nextLockId() + ); + getInternalRegion().put( key, lock ); + return lock; + } + finally { + writeLock.unlock(); + } + } + + /** + * Soft-unlock a cache item. + */ + public final void unlockItem(Object key, SoftLock lock) throws CacheException { + + try { + writeLock.lock(); + Lockable item = (Lockable) getInternalRegion().get( key ); + + if ( ( item != null ) && item.isUnlockable( lock ) ) { + decrementLock( key, (Lock) item ); + } + else { + handleLockExpiry( key, item ); + } + } + finally { + writeLock.unlock(); + } + } + + private long nextLockId() { + return nextLockId.getAndIncrement(); + } + + /** + * Unlock and re-put the given key, lock combination. + */ + protected void decrementLock(Object key, Lock lock) { + lock.unlock( getInternalRegion().nextTimestamp() ); + getInternalRegion().put( key, lock ); + } + + /** + * Handle the timeout of a previous lock mapped to this key + */ + protected void handleLockExpiry(Object key, Lockable lock) { + LOG.expired(key); + long ts = getInternalRegion().nextTimestamp() + getInternalRegion().getTimeout(); + // create new lock that times out immediately + Lock newLock = new Lock( ts, uuid, nextLockId.getAndIncrement(), null ); + newLock.unlock( ts ); + getInternalRegion().put( key, newLock ); + } + + /** + * Interface type implemented by all wrapper objects in the cache. + */ + protected static interface Lockable { + + /** + * Returns true if the enclosed value can be read by a transaction started at the given time. + */ + public boolean isReadable(long txTimestamp); + + /** + * Returns true if the enclosed value can be replaced with one of the given version by a + * transaction started at the given time. + */ + public boolean isWriteable(long txTimestamp, Object version, Comparator versionComparator); + + /** + * Returns the enclosed value. + */ + public Object getValue(); + + /** + * Returns true if the given lock can be unlocked using the given SoftLock instance as a handle. + */ + public boolean isUnlockable(SoftLock lock); + + /** + * Locks this entry, stamping it with the UUID and lockId given, with the lock timeout occuring at the specified + * time. The returned Lock object can be used to unlock the entry in the future. + */ + public Lock lock(long timeout, UUID uuid, long lockId); + } + + /** + * Wrapper type representing unlocked items. + */ + protected final static class Item implements Serializable, Lockable { + + private static final long serialVersionUID = 1L; + private final Object value; + private final Object version; + private final long timestamp; + + /** + * Creates an unlocked item wrapping the given value with a version and creation timestamp. + */ + Item(Object value, Object version, long timestamp) { + this.value = value; + this.version = version; + this.timestamp = timestamp; + } + + /** + * {@inheritDoc} + */ + public boolean isReadable(long txTimestamp) { + return txTimestamp > timestamp; + } + + /** + * {@inheritDoc} + */ + public boolean isWriteable(long txTimestamp, Object newVersion, Comparator versionComparator) { + return version != null && versionComparator.compare( version, newVersion ) < 0; + } + + /** + * {@inheritDoc} + */ + public Object getValue() { + return value; + } + + /** + * {@inheritDoc} + */ + public boolean isUnlockable(SoftLock lock) { + return false; + } + + /** + * {@inheritDoc} + */ + public Lock lock(long timeout, UUID uuid, long lockId) { + return new Lock( timeout, uuid, lockId, version ); + } + } + + /** + * Wrapper type representing locked items. + */ + protected final static class Lock implements Serializable, Lockable, SoftLock { + + private static final long serialVersionUID = 2L; + + private final UUID sourceUuid; + private final long lockId; + private final Object version; + + private long timeout; + private boolean concurrent; + private int multiplicity = 1; + private long unlockTimestamp; + + /** + * Creates a locked item with the given identifiers and object version. + */ + Lock(long timeout, UUID sourceUuid, long lockId, Object version) { + this.timeout = timeout; + this.lockId = lockId; + this.version = version; + this.sourceUuid = sourceUuid; + } + + /** + * {@inheritDoc} + */ + public boolean isReadable(long txTimestamp) { + return false; + } + + /** + * {@inheritDoc} + */ + public boolean isWriteable(long txTimestamp, Object newVersion, Comparator versionComparator) { + if ( txTimestamp > timeout ) { + // if timedout then allow write + return true; + } + if ( multiplicity > 0 ) { + // if still locked then disallow write + return false; + } + return version == null ? txTimestamp > unlockTimestamp : versionComparator.compare( + version, + newVersion + ) < 0; + } + + /** + * {@inheritDoc} + */ + public Object getValue() { + return null; + } + + /** + * {@inheritDoc} + */ + public boolean isUnlockable(SoftLock lock) { + return equals( lock ); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object o) { + if ( o == this ) { + return true; + } + else if ( o instanceof Lock ) { + return ( lockId == ( (Lock) o ).lockId ) && sourceUuid.equals( ( (Lock) o ).sourceUuid ); + } + else { + return false; + } + } + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { + int hash = ( sourceUuid != null ? sourceUuid.hashCode() : 0 ); + int temp = (int) lockId; + for ( int i = 1; i < Long.SIZE / Integer.SIZE; i++ ) { + temp ^= ( lockId >>> ( i * Integer.SIZE ) ); + } + return hash + temp; + } + + /** + * Returns true if this Lock has been concurrently locked by more than one transaction. + */ + public boolean wasLockedConcurrently() { + return concurrent; + } + + /** + * {@inheritDoc} + */ + public Lock lock(long timeout, UUID uuid, long lockId) { + concurrent = true; + multiplicity++; + this.timeout = timeout; + return this; + } + + /** + * Unlocks this Lock, and timestamps the unlock event. + */ + public void unlock(long timestamp) { + if ( --multiplicity == 0 ) { + unlockTimestamp = timestamp; + } + } + + /** + * {@inheritDoc} + */ + public String toString() { + StringBuilder sb = new StringBuilder( "Lock Source-UUID:" + sourceUuid + " Lock-ID:" + lockId ); + return sb.toString(); + } + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseGeneralDataRegionAdapter.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseCollectionRegionAccessStrategy.java similarity index 55% rename from hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseGeneralDataRegionAdapter.java rename to hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseCollectionRegionAccessStrategy.java index 37a17fb75f..ad785d1837 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseGeneralDataRegionAdapter.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseCollectionRegionAccessStrategy.java @@ -1,7 +1,7 @@ /* * Hibernate, Relational Persistence for Idiomatic Java * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. @@ -21,37 +21,34 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.cache.internal.bridge; +package org.hibernate.testing.cache; -import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.Cache; +import org.hibernate.cache.spi.CollectionRegion; import org.hibernate.cache.spi.GeneralDataRegion; -import org.hibernate.cfg.Settings; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; /** - * {@inheritDoc} - * - * @author Steve Ebersole + * @author Strong Liu */ -public abstract class BaseGeneralDataRegionAdapter extends BaseRegionAdapter implements GeneralDataRegion { +class BaseCollectionRegionAccessStrategy extends BaseRegionAccessStrategy implements CollectionRegionAccessStrategy { + private final CollectionRegionImpl region; - protected BaseGeneralDataRegionAdapter(Cache underlyingCache, Settings settings) { - super( underlyingCache, settings ); + @Override + protected BaseGeneralDataRegion getInternalRegion() { + return region; } - public Object get(Object key) throws CacheException { - return underlyingCache.get( key ); + @Override + protected boolean isDefaultMinimalPutOverride() { + return region.getSettings().isMinimalPutsEnabled(); } - public void put(Object key, Object value) throws CacheException { - underlyingCache.put( key, value ); + @Override + public CollectionRegion getRegion() { + return region; } - public void evict(Object key) throws CacheException { - underlyingCache.remove( key ); - } - - public void evictAll() throws CacheException { - underlyingCache.clear(); + BaseCollectionRegionAccessStrategy(CollectionRegionImpl region) { + this.region = region; } } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseEntityRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseEntityRegionAccessStrategy.java new file mode 100644 index 0000000000..80d47c7f9d --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseEntityRegionAccessStrategy.java @@ -0,0 +1,85 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.cache; + +import org.jboss.logging.Logger; + +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.EntityRegion; +import org.hibernate.cache.spi.GeneralDataRegion; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; +import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.internal.CoreMessageLogger; + +/** + * @author Strong Liu + */ +class BaseEntityRegionAccessStrategy extends BaseRegionAccessStrategy implements EntityRegionAccessStrategy { + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, BaseEntityRegionAccessStrategy.class.getName() + ); + private final EntityRegionImpl region; + + BaseEntityRegionAccessStrategy(EntityRegionImpl region) { + this.region = region; + } + + + @Override + public EntityRegion getRegion() { + return region; + } + + @Override + public boolean insert(Object key, Object value, Object version) throws CacheException { + return putFromLoad( key, value, 0, version ); + } + + @Override + public boolean afterInsert(Object key, Object value, Object version) throws CacheException { + return true; + } + + @Override + public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) + throws CacheException { + return false; + } + + @Override + public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) + throws CacheException { + return false; + } + + @Override + protected BaseGeneralDataRegion getInternalRegion() { + return region; + } + + @Override + protected boolean isDefaultMinimalPutOverride() { + return region.getSettings().isMinimalPutsEnabled(); + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseGeneralDataRegion.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseGeneralDataRegion.java new file mode 100644 index 0000000000..112b29e4bf --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseGeneralDataRegion.java @@ -0,0 +1,91 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.cache; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.jboss.logging.Logger; + +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.GeneralDataRegion; +import org.hibernate.internal.CoreMessageLogger; + +/** + * @author Strong Liu + */ +class BaseGeneralDataRegion extends BaseRegion implements GeneralDataRegion { + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, BaseGeneralDataRegion.class.getName() + ); + + BaseGeneralDataRegion(String name) { + super( name ); + } + + @Override + public Object get(Object key) throws CacheException { + LOG.debugf( "Cache lookup : key[%s]", key ); + if ( key == null ) { + return null; + } + Object result = cache.get( key ); + if ( result != null ) { + LOG.debugf( "Cache hit: %s", key ); + } + return result; + } + + @Override + public void put(Object key, Object value) throws CacheException { + LOG.debugf( "Caching : [%s] -> [%s]", key, value ); + if ( key == null || value == null ) { + LOG.debug( "Key or Value is null" ); + return; + } + cache.put( key, value ); + } + + @Override + public void evict(Object key) throws CacheException { + LOG.debugf( "Invalidating: %s", key ); + if ( key == null ) { + LOG.debug( "Key is null" ); + return; + } + cache.remove( key ); + } + + @Override + public void evictAll() throws CacheException { + LOG.debug( "evict cache" ); + cache.clear(); + } + + + + + + +} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseRegionAdapter.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegion.java similarity index 57% rename from hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseRegionAdapter.java rename to hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegion.java index 36d5408666..d55932b5f8 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseRegionAdapter.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegion.java @@ -1,7 +1,7 @@ /* * Hibernate, Relational Persistence for Idiomatic Java * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. @@ -21,67 +21,72 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.cache.internal.bridge; +package org.hibernate.testing.cache; +import java.util.Collections; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.Cache; +import org.hibernate.cache.internal.Timestamper; import org.hibernate.cache.spi.Region; -import org.hibernate.cfg.Settings; /** - * Basic adapter bridging between {@link Region} and {@link Cache}. - * - * @author Steve Ebersole + * @author Strong Liu */ -public abstract class BaseRegionAdapter implements Region { - protected final Cache underlyingCache; - protected final Settings settings; +class BaseRegion implements Region { + protected final Map cache = new ConcurrentHashMap(); + private final String name; - protected BaseRegionAdapter(Cache underlyingCache, Settings settings) { - this.underlyingCache = underlyingCache; - this.settings = settings; - } - - public String getName() { - return underlyingCache.getRegionName(); - } - - public void clear() throws CacheException { - underlyingCache.clear(); - } - - public void destroy() throws CacheException { - underlyingCache.destroy(); + BaseRegion(String name) { + this.name = name; } + @Override public boolean contains(Object key) { - // safer to utilize the toMap() as oposed to say get(key) != null - return underlyingCache.toMap().containsKey( key ); + return key != null ? cache.containsKey( key ) : false; } + @Override + public String getName() { + return name; + } + + @Override + public void destroy() throws CacheException { + cache.clear(); + } + + @Override public long getSizeInMemory() { - return underlyingCache.getSizeInMemory(); + return -1; } + @Override public long getElementCountInMemory() { - return underlyingCache.getElementCountInMemory(); + return cache.size(); } + @Override public long getElementCountOnDisk() { - return underlyingCache.getElementCountOnDisk(); + return 0; } + @Override public Map toMap() { - return underlyingCache.toMap(); + return Collections.unmodifiableMap( cache ); } + @Override public long nextTimestamp() { - return underlyingCache.nextTimestamp(); + return Timestamper.next(); } + @Override public int getTimeout() { - return underlyingCache.getTimeout(); + return Timestamper.ONE_MS * 60000; } + } + + diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegionAccessStrategy.java new file mode 100644 index 0000000000..5df9e342f2 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseRegionAccessStrategy.java @@ -0,0 +1,109 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.cache; + +import org.jboss.logging.Logger; + +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.GeneralDataRegion; +import org.hibernate.cache.spi.access.RegionAccessStrategy; +import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.internal.CoreMessageLogger; + +/** + * @author Strong Liu + */ +abstract class BaseRegionAccessStrategy implements RegionAccessStrategy { + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, BaseRegionAccessStrategy.class.getName() + ); + + protected abstract BaseGeneralDataRegion getInternalRegion(); + protected abstract boolean isDefaultMinimalPutOverride(); + @Override + public Object get(Object key, long txTimestamp) throws CacheException { + return getInternalRegion().get( key ); + } + + @Override + public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version) throws CacheException { + return putFromLoad( key, value, txTimestamp, version, isDefaultMinimalPutOverride() ); + } + + @Override + public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride) + throws CacheException { + + if ( key == null || value == null ) { + return false; + } + if ( minimalPutOverride && getInternalRegion().contains( key ) ) { + LOG.debugf( "Item already cached: %s", key ); + return false; + } + LOG.debugf( "Caching: %s", key ); + getInternalRegion().put( key, value ); + return true; + + } + + @Override + public SoftLock lockItem(Object key, Object version) throws CacheException { + return null; + } + + @Override + public SoftLock lockRegion() throws CacheException { + return null; + } + + @Override + public void unlockItem(Object key, SoftLock lock) throws CacheException { + } + + @Override + public void unlockRegion(SoftLock lock) throws CacheException { + evictAll(); + } + + @Override + public void remove(Object key) throws CacheException { + evict( key ); + } + + @Override + public void removeAll() throws CacheException { + evictAll(); + } + + @Override + public void evict(Object key) throws CacheException { + getInternalRegion().evict( key ); + } + + @Override + public void evictAll() throws CacheException { + getInternalRegion().evictAll(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseTransactionalDataRegionAdapter.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseTransactionalDataRegion.java similarity index 65% rename from hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseTransactionalDataRegionAdapter.java rename to hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseTransactionalDataRegion.java index aedcd4f77a..ae0a7be761 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/BaseTransactionalDataRegionAdapter.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/BaseTransactionalDataRegion.java @@ -1,7 +1,7 @@ /* * Hibernate, Relational Persistence for Idiomatic Java * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. @@ -21,35 +21,30 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.cache.internal.bridge; +package org.hibernate.testing.cache; -import org.hibernate.cache.spi.Cache; import org.hibernate.cache.spi.CacheDataDescription; -import org.hibernate.cache.spi.TransactionAwareCache; import org.hibernate.cache.spi.TransactionalDataRegion; -import org.hibernate.cfg.Settings; /** - * {@inheritDoc} - * - * @author Steve Ebersole + * @author Strong Liu */ -public abstract class BaseTransactionalDataRegionAdapter - extends BaseRegionAdapter - implements TransactionalDataRegion { +class BaseTransactionalDataRegion extends BaseGeneralDataRegion implements TransactionalDataRegion { + private final CacheDataDescription metadata; - protected final CacheDataDescription metadata; - - protected BaseTransactionalDataRegionAdapter(Cache underlyingCache, Settings settings, CacheDataDescription metadata) { - super( underlyingCache, settings ); + BaseTransactionalDataRegion(String name, CacheDataDescription metadata) { + super( name ); this.metadata = metadata; } - public boolean isTransactionAware() { - return underlyingCache instanceof TransactionAwareCache; - } - + @Override public CacheDataDescription getCacheDataDescription() { return metadata; } + + @Override + public boolean isTransactionAware() { + return false; + } + } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/CachingRegionFactory.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/CachingRegionFactory.java new file mode 100644 index 0000000000..b942455456 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/CachingRegionFactory.java @@ -0,0 +1,116 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.cache; + +import java.util.Properties; + +import org.jboss.logging.Logger; + +import org.hibernate.cache.CacheException; +import org.hibernate.cache.internal.Timestamper; +import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.CollectionRegion; +import org.hibernate.cache.spi.EntityRegion; +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.hibernate.internal.CoreMessageLogger; + +/** + * @author Strong Liu + */ +public class CachingRegionFactory implements RegionFactory { + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, CachingRegionFactory.class.getName() + ); + private Settings settings; + public CachingRegionFactory() { + LOG.warn( "CachingRegionFactory should be only used for testing." ); + } + + public CachingRegionFactory(Properties properties) { + //add here to avoid run into catch + LOG.warn( "CachingRegionFactory should be only used for testing." ); + } + + @Override + public void start(Settings settings, Properties properties) throws CacheException { + this.settings=settings; + } + + @Override + public void stop() { + } + + @Override + public boolean isMinimalPutsEnabledByDefault() { + return false; + } + + @Override + public AccessType getDefaultAccessType() { + return AccessType.NONSTRICT_READ_WRITE; + } + + @Override + public long nextTimestamp() { + return Timestamper.next(); + } + + @Override + public EntityRegion buildEntityRegion(String regionName, Properties properties, CacheDataDescription metadata) + throws CacheException { + return new EntityRegionImpl( regionName, metadata, settings ); + } + + @Override + public CollectionRegion buildCollectionRegion(String regionName, Properties properties, CacheDataDescription metadata) + throws CacheException { + return new CollectionRegionImpl( regionName, metadata, settings ); + } + + @Override + public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties properties) throws CacheException { + return new QueryResultsRegionImpl( regionName ); + } + + @Override + public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties) throws CacheException { + return new TimestampsRegionImpl( regionName ); + } + + private static class QueryResultsRegionImpl extends BaseGeneralDataRegion implements QueryResultsRegion { + QueryResultsRegionImpl(String name) { + super( name ); + } + } + + private static class TimestampsRegionImpl extends BaseGeneralDataRegion implements TimestampsRegion { + TimestampsRegionImpl(String name) { + super( name ); + } + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/CollectionRegionImpl.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/CollectionRegionImpl.java new file mode 100644 index 0000000000..5158cde72a --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/CollectionRegionImpl.java @@ -0,0 +1,74 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.cache; + +import org.jboss.logging.Logger; + +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.CollectionRegion; +import org.hibernate.cache.spi.access.AccessType; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; +import org.hibernate.cfg.Settings; +import org.hibernate.internal.CoreMessageLogger; + +/** + * @author Strong Liu + */ +class CollectionRegionImpl extends BaseTransactionalDataRegion implements CollectionRegion { + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, CollectionRegionImpl.class.getName() + ); + private final Settings settings; + CollectionRegionImpl(String name, CacheDataDescription metadata, Settings settings) { + super( name, metadata ); + this.settings=settings; + } + + public Settings getSettings() { + return settings; + } + + @Override + public CollectionRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException { + switch ( accessType ) { + case READ_ONLY: + if ( getCacheDataDescription().isMutable() ) { + LOG.warnf( "read-only cache configured for mutable collection [ %s ]", getName() ); + } + return new ReadOnlyCollectionRegionAccessStrategy( this ); + case READ_WRITE: + return new ReadWriteCollectionRegionAccessStrategy( this ); + case NONSTRICT_READ_WRITE: + return new NonstrictReadWriteCollectionRegionAccessStrategy( this ); + case TRANSACTIONAL: + + throw new UnsupportedOperationException( "doesn't support this access strategy" ); + default: + throw new IllegalArgumentException( "unrecognized access strategy type [" + accessType + "]" ); + } + } + + +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/EntityRegionImpl.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/EntityRegionImpl.java new file mode 100644 index 0000000000..2c5d6e66d4 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/EntityRegionImpl.java @@ -0,0 +1,78 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.cache; + +import org.jboss.logging.Logger; + +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.EntityRegion; +import org.hibernate.cache.spi.access.AccessType; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; +import org.hibernate.cfg.Settings; +import org.hibernate.internal.CoreMessageLogger; + +/** + * @author Strong Liu + */ +class EntityRegionImpl extends BaseTransactionalDataRegion implements EntityRegion { + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, EntityRegionImpl.class.getName() + ); + private final Settings settings; + + + EntityRegionImpl(String name, CacheDataDescription metadata, Settings settings) { + super( name, metadata ); + this.settings = settings; + + } + + public Settings getSettings() { + return settings; + } + + @Override + public EntityRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException { + switch ( accessType ) { + case READ_ONLY: + if ( getCacheDataDescription().isMutable() ) { + LOG.warnf( "read-only cache configured for mutable entity [ %s ]", getName() ); + } + return new ReadOnlyEntityRegionAccessStrategy( this ); + case READ_WRITE: + return new ReadWriteEntityRegionAccessStrategy( this ); + case NONSTRICT_READ_WRITE: + return new NonstrictReadWriteEntityRegionAccessStrategy( this ); + case TRANSACTIONAL: +// throw new UnsupportedOperationException( "doesn't support this access strategy" ); + return new NonstrictReadWriteEntityRegionAccessStrategy( this ); + + default: + throw new IllegalArgumentException( "unrecognized access strategy type [" + accessType + "]" ); + } + + } + +} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/OptimisticCacheSourceAdapter.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/NonstrictReadWriteCollectionRegionAccessStrategy.java similarity index 56% rename from hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/OptimisticCacheSourceAdapter.java rename to hibernate-testing/src/main/java/org/hibernate/testing/cache/NonstrictReadWriteCollectionRegionAccessStrategy.java index 2fe6592e21..b92d45d655 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/OptimisticCacheSourceAdapter.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/NonstrictReadWriteCollectionRegionAccessStrategy.java @@ -1,7 +1,7 @@ /* * Hibernate, Relational Persistence for Idiomatic Java * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. @@ -21,30 +21,28 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.cache.internal.bridge; +package org.hibernate.testing.cache; -import java.util.Comparator; +import org.jboss.logging.Logger; -import org.hibernate.cache.spi.CacheDataDescription; -import org.hibernate.cache.spi.OptimisticCacheSource; +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.internal.CoreMessageLogger; /** - * {@inheritDoc} -* -* @author Steve Ebersole -*/ -public class OptimisticCacheSourceAdapter implements OptimisticCacheSource { - private final CacheDataDescription dataDescription; + * @author Strong Liu + */ +class NonstrictReadWriteCollectionRegionAccessStrategy extends BaseCollectionRegionAccessStrategy { + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, NonstrictReadWriteCollectionRegionAccessStrategy.class.getName() + ); - public OptimisticCacheSourceAdapter(CacheDataDescription dataDescription) { - this.dataDescription = dataDescription; + NonstrictReadWriteCollectionRegionAccessStrategy(CollectionRegionImpl region) { + super( region ); + } + @Override + public void unlockItem(Object key, SoftLock lock) throws CacheException { + evict( key ); } - public boolean isVersioned() { - return dataDescription.isVersioned(); - } - - public Comparator getVersionComparator() { - return dataDescription.getVersionComparator(); - } } diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/HashtableCacheProvider.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/NonstrictReadWriteEntityRegionAccessStrategy.java similarity index 50% rename from hibernate-core/src/main/java/org/hibernate/cache/internal/HashtableCacheProvider.java rename to hibernate-testing/src/main/java/org/hibernate/testing/cache/NonstrictReadWriteEntityRegionAccessStrategy.java index ea4830d43e..49127e60ff 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/HashtableCacheProvider.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/NonstrictReadWriteEntityRegionAccessStrategy.java @@ -1,7 +1,7 @@ /* * Hibernate, Relational Persistence for Idiomatic Java * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. @@ -21,48 +21,41 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.cache.internal; +package org.hibernate.testing.cache; -import java.util.Properties; +import org.jboss.logging.Logger; import org.hibernate.cache.CacheException; -import org.hibernate.cache.spi.Cache; -import org.hibernate.cache.spi.CacheProvider; +import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.internal.CoreMessageLogger; /** - * A simple in-memory Hashtable-based cache impl. - * - * @author Gavin King + * @author Strong Liu */ -public class HashtableCacheProvider implements CacheProvider { - - public Cache buildCache(String regionName, Properties properties) throws CacheException { - return new HashtableCache( regionName ); +class NonstrictReadWriteEntityRegionAccessStrategy extends BaseEntityRegionAccessStrategy { + NonstrictReadWriteEntityRegionAccessStrategy(EntityRegionImpl region) { + super( region ); } - public long nextTimestamp() { - return Timestamper.next(); + @Override + public void unlockItem(Object key, SoftLock lock) throws CacheException { + evict( key ); } - /** - * Callback to perform any necessary initialization of the underlying cache implementation - * during SessionFactory construction. - * - * @param properties current configuration settings. - */ - public void start(Properties properties) throws CacheException { - } - - /** - * Callback to perform any necessary cleanup of the underlying cache implementation - * during SessionFactory.close(). - */ - public void stop() { - } - - public boolean isMinimalPutsEnabledByDefault() { + @Override + public boolean insert(Object key, Object value, Object version) throws CacheException { return false; } -} + @Override + public boolean afterInsert(Object key, Object value, Object version) throws CacheException { + return false; + } + @Override + public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) + throws CacheException { + evict( key ); + return false; + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyCollectionRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyCollectionRegionAccessStrategy.java new file mode 100644 index 0000000000..0af0438aa1 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyCollectionRegionAccessStrategy.java @@ -0,0 +1,59 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.cache; + +import org.jboss.logging.Logger; + +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.internal.CoreMessageLogger; + +/** + * @author Strong Liu + */ +class ReadOnlyCollectionRegionAccessStrategy extends BaseCollectionRegionAccessStrategy { + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, ReadOnlyCollectionRegionAccessStrategy.class.getName() + ); + + ReadOnlyCollectionRegionAccessStrategy(CollectionRegionImpl region) { + super( region ); + } + + @Override + public void unlockItem(Object key, SoftLock lock) throws CacheException { + LOG.invalidEditOfReadOnlyItem( key ); + } + + @Override + public SoftLock lockItem(Object key, Object version) throws CacheException { + LOG.invalidEditOfReadOnlyItem( key ); + throw new UnsupportedOperationException( "Can't write to a readonly object" ); + } + + @Override + public void remove(Object key) throws CacheException { + } + +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyEntityRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyEntityRegionAccessStrategy.java new file mode 100644 index 0000000000..33e3177016 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadOnlyEntityRegionAccessStrategy.java @@ -0,0 +1,86 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.cache; + +import org.jboss.logging.Logger; + +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.internal.CoreMessageLogger; + +/** + * @author Strong Liu + */ +class ReadOnlyEntityRegionAccessStrategy extends BaseEntityRegionAccessStrategy { + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, ReadOnlyEntityRegionAccessStrategy.class.getName() + ); + + ReadOnlyEntityRegionAccessStrategy(EntityRegionImpl region) { + super( region ); + } + + @Override + public void remove(Object key) throws CacheException { + } + + @Override + public boolean insert(Object key, Object value, Object version) throws CacheException { + return false; //wait until tx complete, see afterInsert(). + } + + @Override + public boolean afterInsert(Object key, Object value, Object version) throws CacheException { + getInternalRegion().put( key, value ); //save into cache since the tx is completed + return true; + } + + + @Override + public void unlockItem(Object key, SoftLock lock) throws CacheException { + LOG.invalidEditOfReadOnlyItem( key ); + } + + @Override + public SoftLock lockItem(Object key, Object version) throws CacheException { + LOG.invalidEditOfReadOnlyItem( key ); + throw new UnsupportedOperationException( "Can't write to a readonly object" ); + } + + @Override + public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) + throws CacheException { + LOG.invalidEditOfReadOnlyItem( key ); + throw new UnsupportedOperationException( "Can't write to a readonly object" ); + } + + @Override + public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) + throws CacheException { + LOG.invalidEditOfReadOnlyItem( key ); + throw new UnsupportedOperationException( "Can't write to a readonly object" ); + } + + +} diff --git a/hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/QueryResultsRegionAdapter.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteCollectionRegionAccessStrategy.java similarity index 51% rename from hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/QueryResultsRegionAdapter.java rename to hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteCollectionRegionAccessStrategy.java index 16bf631dee..2876074584 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/internal/bridge/QueryResultsRegionAdapter.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteCollectionRegionAccessStrategy.java @@ -1,7 +1,7 @@ /* * Hibernate, Relational Persistence for Idiomatic Java * - * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. @@ -21,19 +21,44 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.cache.internal.bridge; +package org.hibernate.testing.cache; -import org.hibernate.cache.spi.Cache; -import org.hibernate.cache.spi.QueryResultsRegion; -import org.hibernate.cfg.Settings; +import java.util.Comparator; + +import org.hibernate.cache.spi.CollectionRegion; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; /** - * Adapter specifically briding {@link org.hibernate.cache.spi.QueryResultsRegion} to {@link Cache}. -* -* @author Steve Ebersole + * @author Strong Liu */ -public class QueryResultsRegionAdapter extends BaseGeneralDataRegionAdapter implements QueryResultsRegion { - protected QueryResultsRegionAdapter(Cache underlyingCache, Settings settings) { - super( underlyingCache, settings ); +class ReadWriteCollectionRegionAccessStrategy extends AbstractReadWriteAccessStrategy + implements CollectionRegionAccessStrategy { + + private final CollectionRegionImpl region; + + ReadWriteCollectionRegionAccessStrategy(CollectionRegionImpl region) { + this.region = region; } + + @Override + Comparator getVersionComparator() { + return region.getCacheDataDescription().getVersionComparator(); + } + + @Override + protected BaseGeneralDataRegion getInternalRegion() { + return region; + } + + @Override + protected boolean isDefaultMinimalPutOverride() { + return region.getSettings().isMinimalPutsEnabled(); + } + + @Override + public CollectionRegion getRegion() { + return region; + } + + } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteEntityRegionAccessStrategy.java b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteEntityRegionAccessStrategy.java new file mode 100644 index 0000000000..99bb1c0bc9 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/cache/ReadWriteEntityRegionAccessStrategy.java @@ -0,0 +1,127 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010-2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program 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 distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.cache; + +import java.util.Comparator; + +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.EntityRegion; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; +import org.hibernate.cache.spi.access.SoftLock; + +/** + * @author Strong Liu + */ +class ReadWriteEntityRegionAccessStrategy extends AbstractReadWriteAccessStrategy + implements EntityRegionAccessStrategy { + private final EntityRegionImpl region; + + ReadWriteEntityRegionAccessStrategy(EntityRegionImpl region) { + this.region = region; + } + + @Override + public boolean insert(Object key, Object value, Object version) throws CacheException { + return false; + } + + @Override + public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) + throws CacheException { + return false; + } + + @Override + public void evict(Object key) throws CacheException { + } + + @Override + public boolean afterInsert(Object key, Object value, Object version) throws CacheException { + + try { + writeLock.lock(); + Lockable item = (Lockable) region.get( key ); + if ( item == null ) { + region.put( key, new Item( value, version, region.nextTimestamp() ) ); + return true; + } + else { + return false; + } + } + finally { + writeLock.unlock(); + } + } + + + @Override + public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock) + throws CacheException { + try { + writeLock.lock(); + Lockable item = (Lockable) region.get( key ); + + if ( item != null && item.isUnlockable( lock ) ) { + Lock lockItem = (Lock) item; + if ( lockItem.wasLockedConcurrently() ) { + decrementLock( key, lockItem ); + return false; + } + else { + region.put( key, new Item( value, currentVersion, region.nextTimestamp() ) ); + return true; + } + } + else { + handleLockExpiry( key, item ); + return false; + } + } + finally { + writeLock.unlock(); + } + } + + + @Override + protected BaseGeneralDataRegion getInternalRegion() { + return region; + } + + @Override + protected boolean isDefaultMinimalPutOverride() { + return region.getSettings().isMinimalPutsEnabled(); + } + + @Override + Comparator getVersionComparator() { + return region.getCacheDataDescription().getVersionComparator(); + } + + @Override + public EntityRegion getRegion() { + return region; + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java index fa2073e3ed..0e0a67bb0a 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java @@ -37,7 +37,6 @@ import java.util.Properties; import org.hibernate.HibernateException; import org.hibernate.Interceptor; import org.hibernate.Session; -import org.hibernate.cache.internal.HashtableCacheProvider; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; @@ -62,6 +61,7 @@ import org.hibernate.testing.BeforeClassOnce; import org.hibernate.testing.OnExpectedFailure; import org.hibernate.testing.OnFailure; import org.hibernate.testing.SkipLog; +import org.hibernate.testing.cache.CachingRegionFactory; import static org.junit.Assert.fail; @@ -130,7 +130,7 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { protected Configuration constructConfiguration() { Configuration configuration = new Configuration() - .setProperty( Environment.CACHE_PROVIDER, HashtableCacheProvider.class.getName() ); + .setProperty(Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName() ); configuration.setProperty( AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true" ); if ( createSchema() ) { configuration.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );