欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Class could not be loaded or is not persistent: java.sql.Timestamp(BDB JE)

程序员文章站 2022-04-28 11:24:25
...

问题背景

最近在使用Berkeley DB 持久化一个实体类,这个实体类中有个字段是java.sql.TimeStamp类型

然后程序运行就出现了如下错误:

Class could not be loaded or is not persistent: java.sql.Timestamp

故障分析

出现这个错误是因为Berkeley DB 里面也有一个Timestamp类型,这个类型 和java.sql.Timestamp 字段出现了不兼容故障。

解决思路是设计一个代理类来解决。

故障解除

  1. 设计一个代理类
import com.sleepycat.persist.model.Persistent;
import com.sleepycat.persist.model.PersistentProxy;

import java.sql.Timestamp;

/**
 * 功能:
 * 作者: 星云
 * 时间: 2019/9/6 14:20
 */
@Persistent(proxyFor = Timestamp.class)
public class ProxyForTimeStamp implements PersistentProxy<Timestamp> {

    Timestamp timestamp;
    @Override
    public void initializeProxy(Timestamp timestamp) {
        this.timestamp=timestamp;
    }

    @Override
    public Timestamp convertProxy() {
        return this.timestamp;
    }
}

2.然后在环境配置中加载这个代理类

  EntityModel entityModel=new AnnotationModel();
  entityModel.registerClass(ProxyForTimeStamp.class);
  myStoreConfig.setModel(entityModel);

完整代码如下:

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.StoreConfig;
import com.sleepycat.persist.model.AnnotationModel;
import com.sleepycat.persist.model.EntityModel;
import com.xingyun.berkeleydb.ProxyForTimeStamp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * 功能: Berkeley Database Java Edition 环境管理器
 * 作者: 星云
 * 时间: 2019/9/6 9:15
 */
@SuppressWarnings("unused")
public final class BDBEnvironmentManager {
    //禁用构造方法
    private BDBEnvironmentManager() {}

    private final static Logger LOGGER = LoggerFactory.getLogger(BDBEnvironmentManager.class);

    private static volatile BDBEnvironmentManager bdbEnvironmentManager = null;

    // 数据库环境对象
    private static Environment myEnvironment = null;
    // 数据存储基本单元
    private static EntityStore myEntityStore = null;

    /**
     * @return
     */
    public static Environment getMyEnvironment() {
        if (myEnvironment != null) {
            return myEnvironment;
        } else {
            return null;
        }
    }

    /**
     * @return
     */
    public static EntityStore getMyEntityStore() {
        if (myEntityStore != null) {
            return myEntityStore;
        } else {
            return null;
        }
    }

    // 懒汉式

    /**
     * @param envHome
     * @param readOnly
     * @return
     */
    public static BDBEnvironmentManager getInstance(File envHome, Boolean readOnly) {

        if (envHome == null) {
            return null;
        }
        if (bdbEnvironmentManager == null) {
            // 添加同步锁,会更安全高效
            synchronized (BDBEnvironmentManager.class) {
                if (bdbEnvironmentManager == null) {

                    // 代码在这里执行确保应用程序中只有一个实例
                    bdbEnvironmentManager = new BDBEnvironmentManager();
                    // 创建一个BDB 环境配置对象
                    EnvironmentConfig myEnvConfig = new EnvironmentConfig();
                    // 创建一个数据存储配置对象
                    StoreConfig myStoreConfig = new StoreConfig();

                    if (readOnly == null) {
                        readOnly = false;
                    }

                    EntityModel entityModel=new AnnotationModel();
                    entityModel.registerClass(ProxyForTimeStamp.class);
                    myStoreConfig.setModel(entityModel);

                    // 设置该环境是否为只读,true 为只读,false 为可读写
                    myEnvConfig.setReadOnly(readOnly);
                    // 设置数据存储配置是否为只读,true 为只读,false 为可读写
                    myStoreConfig.setReadOnly(readOnly);

                    // 如果该环境不存在是否重建,true 允许重建,false 不可重建
                    myEnvConfig.setAllowCreate(!readOnly);
                    // 如果该存储配置不存在是否重建,true 允许重建,false 不可重建
                    myStoreConfig.setAllowCreate(!readOnly);

                    //数据库环境是否支持事务
                    myEnvConfig.setTransactional(!readOnly);
                    //存储环境是否支持事务
                    myStoreConfig.setTransactional(!readOnly);


                    // 如果文件不存在则创建
                    if (!envHome.exists()) {
                        try {
                            envHome.mkdir();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                    // 打开 environment 和 entity store
                    if (myEnvironment == null || myEntityStore == null) {
                        try {
                            myEnvironment = new Environment(envHome, myEnvConfig);
                            myEntityStore = new EntityStore(myEnvironment, "EntityStore", myStoreConfig);
                        } catch (DatabaseException e) {
                            LOGGER.error("Database Exception",e);
                        }
                    }
                }
            }
        }
        return bdbEnvironmentManager;
    }

    // Close the store and environment.

    /**
     * Close the store and environment
     */
    public static void close() {
        // 判断存储对象是否为空
        if (myEntityStore != null) {
            try {
                // 尝试关闭存储对象
                myEntityStore.close();
            } catch (DatabaseException dbe) {
                LOGGER.error("Error closing store: " + dbe.toString());
            }
        }
        // 判断环境是否为空
        if (myEnvironment != null) {
            try {
                // 关闭环境
                myEnvironment.close();
            } catch (DatabaseException dbe) {
                LOGGER.error("Error DatabaseException: " + dbe.toString());
            }
        }
    }
}

参考资料: