首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用java锁和条件对象

使用java锁和条件对象
EN

Stack Overflow用户
提问于 2014-12-11 14:54:04
回答 1查看 206关注 0票数 1

我在下面构建了这个数据库对象。

我希望方法更新和查询是: 1.以多线程方式工作(这意味着两个线程可以同时访问它们--同步防止的--这就是为什么我不能将它添加为mehtod singutare)。2.如果有一个线程启动了数据库,而第二个线程(在它之后启动)试图使用该数据库--我不想看到第二个线程在数据库尚未打开时访问数据库的情况。换句话说,我希望它通常是多线程的,但是当一个线程第一次打开数据库时,线程是安全的(原子的)。

谢谢,下面的类是我需要插入逻辑的地方(更具体一些--查询和更新方法)。

代码语言:javascript
复制
public class SingeltonDB {
    private static DBconnImpl db = null;
    private static SingeltonDB singalDb = null;

    private SingeltonDB(String username, String password) {
        db = new DBconnImpl();
    }

    public static boolean isOpen() {
        return (db != null);
    }

    public synchronized static SingeltonDB getInstance(String username,
            String password) throws Exception {
        if (db != null) {
            throw (new Exception("The database is  open"));
        } else {
            System.out.println("The database is now open");
            singalDb = new SingeltonDB(username, password);
        }
        db.connect(username, password);
        System.out.println("The database was connected");
        return singalDb;
    }

    public synchronized static SingeltonDB getInstance() throws Exception {
        if (db == null) {
            throw (new Exception("The database is not open"));
        }

        return singalDb;
    }

    public void create(String tableName) throws Exception {
        db.create(tableName);
    }

    public  User query(String tableName, int rowID) throws Exception {
        if (db == null) {
            System.out.println("Error: the database is not open");
            return null;
        }
        return (db.query(tableName, rowID));
    }

    public  void update(String tableName, User user) throws Exception {
        if (db == null) {
            System.out.println("Error: the database is not open");
            return;
        }
        db.update(tableName, user);
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-11 15:31:48

我会将连接调用从getInstance静态方法移动到SingeltonDB构造函数。这将确保每当您获得对静态db字段的引用时,数据库就会被打开。我还会将db ==空检查添加到所有非静态方法中。

代码语言:javascript
复制
public class SingeltonDB {
    private static DBconnImpl db = null;
    private static SingeltonDB singalDb = null;

    private SingeltonDB(String username, String password) {
        db = new DBconnImpl();
        db.connect(username, password);
        System.out.println("The database was connected");
    }

    public static boolean isOpen() {
        return (db != null);
    }

    public synchronized static SingeltonDB getInstance(String username,
            String password) throws Exception {
        if (db != null) {
            throw (new Exception("The database is  open"));
        } else {
            System.out.println("The database is now open");
            singalDb = new SingeltonDB(username, password);
        }
        return singalDb;
    }

    public synchronized static SingeltonDB getInstance() throws Exception {
        if (db == null) {
            throw (new Exception("The database is not open"));
        }

        return singalDb;
    }

    private static void checkDbOpened() throws Exception {
        if (db == null) {
            throw new Exception("The database is not open");
        }
    }

    public void create(String tableName) throws Exception {
        checkDbOpened();
        db.create(tableName);
    }

    public  User query(String tableName, int rowID) throws Exception {
        checkDbOpened();
        return (db.query(tableName, rowID));
    }

    public  void update(String tableName, User user) throws Exception {
        checkDbOpened();
        db.update(tableName, user);
    }

}

下面是一个更新的单例,它允许您确定是否创建了一个表

代码语言:javascript
复制
public class SingeltonDB {
    private static DBconnImpl db = null;
    private static SingeltonDB singalDb = null;
    private static ConcurrentSkipListSet<String> tableNames = new ConcurrentSkipListSet<String>();

    private SingeltonDB(String username, String password) {
        db = new DBconnImpl();
        db.connect(username, password);
        System.out.println("The database was connected");
    }

    public static boolean isOpen() {
        return (db != null);
    }

    public synchronized static SingeltonDB getInstance(String username,
            String password) throws Exception {
        if (db != null) {
            throw (new Exception("The database is  open"));
        } else {
            System.out.println("The database is now open");
            singalDb = new SingeltonDB(username, password);
        }
        return singalDb;
    }

    public synchronized static SingeltonDB getInstance() throws Exception {
        if (db == null) {
            throw (new Exception("The database is not open"));
        }

        return singalDb;
    }

    private static void checkDbOpened() throws Exception {
        if (db == null) {
            throw new Exception("The database is not open");
        }
    }

    private static void checkForTable(String tableName) {
        if (tableNames.add(tableName)) {
           db.create(tableName);
        }
    }

    public void create(String tableName) throws Exception {
        checkDbOpened();
        checkForTable(tableName);
    }

    public  User query(String tableName, int rowID) throws Exception {
        checkDbOpened();
        checkForTable(tableName);
        return (db.query(tableName, rowID));
    }

    public  void update(String tableName, User user) throws Exception {
        checkDbOpened();
        checkForTable(tableName);
        db.update(tableName, user);
    }

}

checkForTable函数将确定给定的tableName是否已经创建。如果不是,那么它将创建该表。此更新将确保在使用表之前创建该表。此代码的问题是它不能跨进程工作,而只能在单个进程中工作,除非db类知道如何管理跨进程边界创建的表。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27425705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档