试着总结一下这两个概念之间的区别(因为当我看到人们在一个句子中使用它们时,我真的很困惑,比如"非阻塞异步IO",我试图找出它是什么意思).
因此,在我的理解中,非阻塞IO主要是处理IO的OS机制,如果有任何数据就绪,否则只返回错误/什么都不做.
在异步IO中,您只需提供回调,并在数据可用时通知您的应用程序.
那么实际上什么是"非阻塞异步IO"?以及它们如何用Java实现(标准JDK,没有外部库,我知道有java.nio.channels.{Channels, Selector, SelectorKey}
和java.nio.channels.{AsynchronousSocketChannel}
):非阻塞IO,异步IO和非阻塞异步IO(如果有这样的东西)?
这是代码:
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {}
Run Code Online (Sandbox Code Playgroud)
来自Spring Data JPA项目的JpaRepository.
这是测试代码:
public class JpaAccountRepositoryTest extends JpaRepositoryTest {
@Inject
private AccountRepository accountRepository;
@Inject
private Account account;
@Test
@Transactional
public void createAccount() {
Account returnedAccount = accountRepository.save(account);
System.out.printf("account ID is %d and for returned account ID is %d\n", account.getId(), returnedAccount.getId());
}
}
Run Code Online (Sandbox Code Playgroud)
结果如下:
account ID is 0 and for returned account ID is 1
Run Code Online (Sandbox Code Playgroud)
这是来自CrudReporsitory.save()javadoc:
保存给定的实体.使用返回的实例进行进一步操作,因为save操作可能已完全更改了实体实例.
以下是Spring Data JPA中SimpleJpaRepository的实际代码:
@Transactional
public T save(T entity) {
if (entityInformation.isNew(entity)) { …
Run Code Online (Sandbox Code Playgroud) 您知道,自Java 5发布以来,在Java中编写Singleton模式的推荐方法是使用枚举.
public enum Singleton {
INSTANCE;
}
Run Code Online (Sandbox Code Playgroud)
但是,我不喜欢这个 - 是强制客户端使用Singleton.INSTANCE来访问单例实例.也许,更好的方法是在普通类中隐藏Singleton,并提供更好的单例设施访问:
public class ApplicationSingleton {
private static enum Singleton {
INSTANCE;
private ResourceBundle bundle;
private Singleton() {
System.out.println("Singleton instance is created: " +
System.currentTimeMillis());
bundle = ResourceBundle.getBundle("application");
}
private ResourceBundle getResourceBundle() {
return bundle;
}
private String getResourceAsString(String name) {
return bundle.getString(name);
}
};
private ApplicationSingleton() {}
public static ResourceBundle getResourceBundle() {
return Singleton.INSTANCE.getResourceBundle();
}
public static String getResourceAsString(String name) {
return Singleton.INSTANCE.getResourceAsString(name);
}
}
Run Code Online (Sandbox Code Playgroud)
所以,客户现在可以简单地写:
ApplicationSingleton.getResourceAsString("application.name")
Run Code Online (Sandbox Code Playgroud)
例如.哪个好多了:
Singleton.INSTANCE.getResourceAsString("application.name")
Run Code Online (Sandbox Code Playgroud)
所以,问题是:这是正确的方法吗?此代码是否有任何问题(线程安全?)?它具有"enum …
我发现使用原始类型作为JPA的对象@Id与Spring Data JPA一起使用的问题.我在父母方面与Cascade.ALL有父/子关系,而且孩子有PK,同时也是父母的FK.
class Parent {
@Id
private long id;
@OneToOne(mappedBy = "parent", cascade = ALL)
private Child child;
}
class Child {
@Id
@OneToOne
private Parent parent;
}
Run Code Online (Sandbox Code Playgroud)
所以,当我跑:
...
Parent parent = new Parent();
Child child = new Child(parent);
parent.setChild(child);
em.persist(parent)
...
Run Code Online (Sandbox Code Playgroud)
一切正常.但我使用Spring Data JPA来持久保存实体,所以我改为运行:
parentRepository.save(parent); // instead of em.persist(parent);
Run Code Online (Sandbox Code Playgroud)
而且这个失败了以下例外:
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Parent
Run Code Online (Sandbox Code Playgroud)
问题是Spring Data JPA save()方法检查实体是否是新的,如果是new,则使用em.persist(),否则使用em.merge().
这里有趣的部分是Spring如何检查实体是否是新的: …
请问任何人,请解释两者之间的区别:评估规范与构建任何可用JSR流程实现的规范,例如,JSR 299.
有时这是有价值的信息来源,但对于我作为开发人员,我应该下载和阅读哪一个?令人烦恼的是 - 有时这两个是相同的.
有什么想法吗?
考虑以下示例来说明问题(它只是为了解释问题而构建的,但我在书中看到了类似的代码以及实际项目中的代码):
package main
import (
"strconv"
"fmt"
"log"
)
func main() {
n1, err := strconv.Atoi("1")
if err != nil {
log.Panicf("%v", err)
}
n2, err := strconv.Atoi("2")
if err != nil {
log.Panicf("%v", err)
}
// err := fmt.Errorf("new error") <- line 1
// n1, err := strconv.Atoi("3") <- line 2
fmt.Printf("n1 = %d, n2 = %d\n", n1, n2)
}
Run Code Online (Sandbox Code Playgroud)
编译器不抱怨的重新定义err
,但如果我取消<- line 1
或者<- line 2
,它会抱怨no new variable on left side of := …
不确定谁对此错误负责:
Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
Run Code Online (Sandbox Code Playgroud)
当我尝试从IntelliJ IDEA 10.5(我的项目是maven格式项目)在调试模式下运行我的Spring Web应用程序时,会发生此错误.
从独立的Tomcat 7 Web服务器运行相同的应用程序(将战争放入webapps文件夹)时,它可以正常工作.另外从mvn clean install t7:run-forked应用程序也可以正常工作.
我的应用程序是使用Hibernate作为JPA提供程序的Spring JPA应用程序,c3p0用于连接池(从bonecp切换到它,认为bonecp是导致此错误的原因,但它仍然可以与c3p0重现),使用Spring TomcatInstrumentationLoading用于Tomcat上的JPA支持.
我的操作系统是Debian,Linux.
当apply plugin: 'java'
在摇篮,它定义了其他任务processResources
。我想在processResources
. 我在我的中定义了以下复制任务build.gradle
:
import org.apache.tools.ant.filters.ReplaceTokens
task initConfig(type: Copy) {
from("src/resources/assets/js") {
include 'config.js'
filter(ReplaceTokens, tokens: [host: "${System.env.HOST ?: 'localhost:58080'}" as String])
}
into "$sourceSets.main.output.resourcesDir/assets/js"
}
Run Code Online (Sandbox Code Playgroud)
如果我运行它工作正常gradle -q run initConfig
。我想要的是这个任务作为processResources
任务的一部分运行,我试图把相同的逻辑如下:
processResources << {
// same code goes here
}
Run Code Online (Sandbox Code Playgroud)
或者
processResources {
doLast {
// same code goes here
}
}
Run Code Online (Sandbox Code Playgroud)
它们都不起作用。所以,如果运行gradle -q clean processResources
资源被复制,但令牌没有被替换。
我不想initConfig
在运行构建时强制显式运行/添加,所以理想情况下gradle -q build
应该做所有必要的事情。
我相信这是可能的,因为build
任务本身定义了在执行期间运行多个任务的方式。还没有找到如何。
网上有一些文章提到了使用Stream
-s 而不是旧的loop
-s 的一些缺点:
But is there any impact from the GC perspective? As I assume (is it correct?) that every stream call creates some short-lived objects underneath. If the particular code fragment which uses streams is called frequently by the underlying system could it cause eventually some performance issue from the GC perspective or put extra pressure on GC? Or the impact is minimal and could be ignored most of the time?
Are there …
java performance garbage-collection premature-optimization java-stream
我有 2 个实体,比如 Car 和 Engine(只是示例名称,但逻辑是相同的)。
@Entity
public class Car {
@Id
private Long id;
@OneToOne(mappedBy = "car", cascade = Cascade.ALL)
private Engine engine;
...
}
@Entity
public class Engine {
@Id
private Long id; // 1
@MapsId // 2
@OneToOne
@JoinColumn(name = "car_id") // join column is optional here
private Car car;
...
}
Run Code Online (Sandbox Code Playgroud)
那么,我这样做:
em.save(car); // successfully saved, data is in the database, but (see below)
TypedQuery<Engine> query = em.createQuery("select engine from Engine engine where engine.car = :car", …
Run Code Online (Sandbox Code Playgroud) java ×8
jpa ×3
spring ×2
asynchronous ×1
build.gradle ×1
enums ×1
go ×1
gradle ×1
java-stream ×1
jpa-2.0 ×1
jpql ×1
jsr ×1
nonblocking ×1
parent-child ×1
performance ×1
singleton ×1
spring-data ×1
tomcat7 ×1