问题描述:
配置完成TERRA-COTTA的config.xml之后,能够正常启动,并且通过其console,主从服务器均正常显示。但是每当运行时持久化时会出现主键冲突,tc—config.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <con:tc-config xmlns:con="http://www.terracotta.org/config"> <servers> <server host="192.168.7.73" name="73"> <dso-port bind="192.168.7.73">9510</dso-port> <jmx-port bind="192.168.7.73">9520</jmx-port> <data>terracotta/server-data</data> <logs>terracotta/server-logs</logs> <statistics>terracotta/cluster-statistics</statistics> </server> <server host="192.168.7.39" name="39"> <dso-port bind="192.168.7.39">9510</dso-port> <jmx-port bind="192.168.7.39">9520</jmx-port> <data>terracotta/server-data</data> <logs>terracotta/server-logs</logs> <statistics>terracotta/cluster-statistics</statistics> </server> </servers> <clients> <logs>terracotta/client-logs</logs> </clients> <application> <dso> <instrumented-classes> ....................... <include> <class-expression>cfca.xfraud.common.domain.Activity</class-expression> </include> ....................... </instrumented-classes> <locks> ....................... <autolock auto-synchronized="false"> <method-expression>* cfca.xfraud.common.domain.Activity.*(..)</method-expression> <lock-level>write</lock-level> </autolock> ........................ </locks> <roots> ........................ <root> <field-name>cfca.xfraud.common.domain.Activity.someFields</field-name> </root> ........................ </roots> </dso> </application> </con:tc-config>
解决思路:
Activity的someFields,被共享后将会变成全局变量,因而升级为类变量,Terra-cotta在执行时,判断Activity的某个属性(hasPersisted<boolean>)时,假定共享修改其为true,而实例变量为false,将会导致该分布式缓存场景下所有activity的hasPersisted的访问值均被覆盖为共享值,也就是说为true(即使其对象属性为false)。
由此观之:读共享变量的优先级大于每个JVM中实例变量的读取优先级。
经验:
一般来说在分布式缓存中的需要共享的变量往往是那些在本地JVM中就设置为static类型的变量