此代码是否解决了Java中的双重检查锁定问题?
public class DBAccessService() {
private static DBAccessService INSTANCE;
private DBAccessService() {}
public static DBAccessService getInstance() {
if (INSTANCE != null) {
return INSTANCE;
}
return createInstance();
}
private static synchronized DBAccessService createInstance() {
if (INSTANCE != null) {
return INSTANCE;
}
DBAccessService instance = new DBAccessService();
INSTANCE = instance;
return INSTANCE;
}
}
Run Code Online (Sandbox Code Playgroud)
有两个方面需要注意:
getInstance()是不同步的,所以INSTANCE被初始化之后没有用于同步没有成本createInstance() 是同步的所以,问题是:这段代码有什么问题吗?它合法且始终是线程安全的吗?
编辑:已 解决 .我找到了困扰我的事情.我使用pgadmin创建表和其他数据库内部,立即检查:如果名称中至少有一个字母(表名,列名,pk名称等)是大写的,那么pgadmin在SQL创建脚本中使用它实际上,使用双引号,因此PostgreSQL会在编写时解释名称.如果运行以下脚本:
CREATE TABLE SAMPLE
(
ID integer NOT NULL,
TITLE character varying(100) NOT NULL,
CONSTRAINT SAMPLE_ID_PK PRIMARY KEY (ID)
)
WITH (
OIDS=FALSE
);
ALTER TABLE SAMPLE
OWNER TO postgres;_
Run Code Online (Sandbox Code Playgroud)
它以小写形式创建所有内容,原始的Sample.java版本工作正常.
persistence.xml中:
<persistence-unit name="com.sample.persistence.jpa" transaction-type="RESOURCE_LOCAL">
<class>com.sample.persistence.Sample</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:postgresql:sample"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.password" value="postgres"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)
Sample.java:
@Entity
@Table(name = "SAMPLE")
public class Sample {
@Id
@Column(name …Run Code Online (Sandbox Code Playgroud) 正如Commitlog数据被刷新到磁盘定期每10秒后(通过控制commitlog_sync_period_in_ms),因此,如果在10秒内所有副本崩溃,我将失去所有这些数据呢?从理论上讲,Cassandra Cluster是否会丢失数据?
有许多资源用于描述客户端,Facebook/LinkedIn/Twitter API用法方面的OAuth使用情况.还行吧.但我对OAuth服务器实现感兴趣.目标是让Web应用程序也可以被移动设备(本机应用程序)访问,因此我需要在我的后端Java服务器上设置OAuth.所以我想知道LinkedIn/Facebook/Twitter如何在服务器端实现OAuth,并在auth_token-s之间区分用户并授予相应的访问权限(某种数据库映射 - auth_token =用户身份?).
或者可能有更好的方法来验证移动用户(我将为后端使用REST风格的服务)?
在Clojure中进行集成测试的技术和库是什么?特别是与数据库,环应用程序,core.async通道,产生副作用的任何东西的交互.
如何在一行中写下以下代码:
(-> 10 pow9)
Run Code Online (Sandbox Code Playgroud)
pow9是哪里的:
(def pow9 (partial #(Math/pow % 9)))
Run Code Online (Sandbox Code Playgroud)
如果我写,(-> 10 (partial #(Math/pow % 9)))我会回来#<core$partial$fn__4228 clojure.core$partial$fn__4228@62330c23>,写作(-> 10 #(Math/pow % 9))失败CompilerException java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.ISeq, compiling:(NO_SOURCE_PATH:1:1),
虽然(-> 10 pow9)工作正常.
更一般的问题是如何使用 - > with function接受多个参数,即如何使其工作(-> 10 #(+ % 10))?
Clojure代码
(def fmt (java.text.SimpleDateFormat. "yyyy-MM-dd"))
#'user/fmt
user=> (.parse fmt "2015-07-10")
#inst "2015-07-09T22:00:00.000-00:00"
Run Code Online (Sandbox Code Playgroud)
类似的Java代码:
public class DateFmt {
public static void main(String[] args) throws ParseException {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
final Date date = sdf.parse("2015-07-10");
System.out.println(date);
}
}
Run Code Online (Sandbox Code Playgroud)
虽然Java代码打印:2015年7月10日00:00:00 CEST 2015(这是我的预期),但Clojure转移了2个小时?