关于DBForum的困惑

04-03-02 mac306


有个问题一直没有找到答案,在Jive2。6中的 DBForum.java里, 有如下更新数据库的方法:

/**
* Saves forum data to the database.
*/
private synchronized void saveToDb() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(SAVE_FORUM);
pstmt.setString(1, name);
pstmt.setString(2, description);
pstmt.setInt(3, modDefaultThreadValue);
pstmt.setInt(4, modDefaultMessageValue);
pstmt.setInt(5, modMinThreadValue);
pstmt.setInt(6, modMinMessageValue);
pstmt.setString(7, StringUtils.dateToMillis(creationDate));
pstmt.setString(8, StringUtils.dateToMillis(modifiedDate));
pstmt.setLong(9, id);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}

// Re-add forum to cache.
factory.cacheManager.forumCache.put(new Long(this.id), this);
}


我的问题是:对于这样的方法,有必要同步么? 为什么要同步?

mybillliu
2004-03-02 13:43
可否把2.6的源代码给我一份

pigangel
2004-03-02 15:36
俺对synchronized的机制一直还很模糊,是像加了锁一样,进行资源独占么?比方这个函数加了同步,是否意味着这个方法每个时刻只能有一个线程调用运行呢?函数里面用到的资源有没有加锁呢?

还是应该怎么说的呢,达人们教育教育俺吧

banq
2004-03-03 09:55
更新数据一般都需要同步,安全性考虑,但是会带来性能影响、

mac306
2004-03-03 13:45
banq提到的,我明白。但是我想知道关于这具体动作,为什么需要同步?

因为,我找不到任何同步的理由。同时,DBForum.java中的其他几个更新DB的方法并没有设置为同步方法。