我在我的项目中使用hibernate,我得到随机的表观死锁,用于非常简单的数据库操作.
有一个堆栈跟踪:https://gist.github.com/knyttl/8999006 - 让我困惑的是,第一个Exception是RollbackException,然后是LockAquisition Exceptions.
问题经常出现在类似的条款中:
@Transactional
public void setLastActivity() {
User user = em.findById(...);
user.setLastActivity(new Date());
em.merge(user);
em.flush();
}
Run Code Online (Sandbox Code Playgroud)
我很困惑,因为我不知道它是Hibernate,MySQL还是C3P0的问题.
我的Hibernate配置:
<prop key="hibernate.dialect">${database.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${database.structure}</prop>
<prop key="hibernate.connection.url">${database.connection}</prop>
<prop key="hibernate.connection.username">${database.username}</prop>
<prop key="hibernate.connection.password">${database.password}</prop>
<prop key="hibernate.connection.driver_class">${database.driver}</prop>
<prop key="hibernate.connection.shutdown">true</prop>
<prop key="hibernate.connection.writedelay">0</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.show_sql">${database.show_sql}</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.ejb.metamodel.generation">disabled</prop>
<!-- Use the C3P0 connection pool provider -->
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.c3p0.min_size">0</prop>
<prop key="hibernate.c3p0.max_size">50</prop>
<prop key="hibernate.c3p0.timeout">120</prop>
<prop key="hibernate.c3p0.max_statements">0</prop>
<prop key="hibernate.c3p0.max_statementsPerConnection">0</prop>
<prop key="hibernate.c3p0.maxStatementsPerConnection">0</prop>
<prop key="hibernate.c3p0.idle_test_period">120</prop>
<prop key="hibernate.c3p0.acquire_increment">1</prop>
<prop key="hibernate.c3p0.numHelperThreads">8</prop>
Run Code Online (Sandbox Code Playgroud)
EDIT1:
我试图找到一种方法来从maven存储库中获取库并运行它.我想通过命令行定义所有内容并忽略pom.我正在努力追随:
mvn exec:exec -Dexec.mainClass="org.main.Class" -Dspring.profiles.active=test
Run Code Online (Sandbox Code Playgroud)
当我尝试使用pom.xml运行它时,它开始获取pom中描述的所有依赖项.我真的不想要.当我在没有pom.xml的情况下运行它时,它说Goal requires a project to execute but there is no POM in this directory. Please verify you invoked Maven from the correct directory.有没有办法在没有pom的情况下运行它或者至少忽略它?
我的目标只是从没有源或罐的任何地方开始我的应用程序.
有没有办法将单个.ts文件编译到不同的目录?
从手动命令编译到不同目录的唯一方法是通过--out命令,但它也连接相关文件,我不想要:
--out FILE|DIRECTORY Concatenate and emit output to single file | Redirect output structure to the directory
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以重定向输出而不连接输入文件?
默认情况下,Spring MVC假定@RequestParam是必需的.考虑这种方法(在Kotlin中):
fun myMethod(@RequestParam list: List<String>) { ... }
Run Code Online (Sandbox Code Playgroud)
从javaScript传递空列表时,我们会调用类似于:
$.post("myMethod", {list: []}, ...)
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,由于列表为空,无法序列化空列表,因此参数基本上消失,因此不满足所需参数的条件.一种是被迫使用required: false的@RequestParam注解.这不好,因为我们永远不会收到空列表,但是null.
有没有办法强制Spring MVC在这种情况下总是假设空列表而不是null?
考虑拥有两个实体经理工厂:
<bean id="writeEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">...</bean>
<bean id="readOnlyEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">...</bean>
Run Code Online (Sandbox Code Playgroud)
然后我想要两个Beans,我将注入正确的持久化上下文:
<bean id="readOnlyManager" class="..MyDatabaseManager">
<bean id="writeManager" class="..MyDatabaseManager">
Run Code Online (Sandbox Code Playgroud)
这个bean看起来像:
public class MyDatabaseManager {
private javax.persistence.EntityManager em;
public EntityManager(javax.persistence.EntityManager em) {
this.em = em;
}
...
}
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,因为EntityManager不是bean,不能以这种方式注入:
No qualifying bean of type 'javax.persistence.EntityManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Run Code Online (Sandbox Code Playgroud)
如何在bean中限定正确的EntityManager?我以前使用@PersistenceContext注释,但这不可用,因为我需要注入它.
如何为这样的Bean指定PersistenceContext?
更新:我的问题是如何通过XML而不是通过注释注入带有限定符的PersistenceContext.
我编写了一个带有变量定义的 terraform 配置,例如:
variable "GOOGLE_CLOUD_REGION" {
type = string
}
Run Code Online (Sandbox Code Playgroud)
当我运行时,terraform plan系统会要求我填写此变量,即使此变量是在我的环境中设置的。
有没有办法告诉 terraform 使用当前的环境变量?或者我是否必须导出它们并以某种方式手动将它们一一传递?
在我的Web应用程序中,我尝试使用Java SDK7 WatchService创建目录轮询bean.我想要实现的是在自己的线程中运行此bean,以便它不会阻止应用程序.就像是:
<bean id="directoryPoller" class="org...MyDirectoryPoller" scope="thread"/>
Run Code Online (Sandbox Code Playgroud) 由于c3p0连接池的问题,我想看看替代方案,并决定哪一个可能更适用于我的情况.HikariCP看起来非常有前景,但没有文档说明如何将它与Hibernate一起使用.
到目前为止,我使用c3p0如下:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${database.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${database.structure}</prop>
<prop key="hibernate.connection.url">${database.connection}</prop>
<prop key="hibernate.connection.username">${database.username}</prop>
<prop key="hibernate.connection.password">${database.password}</prop>
<prop key="hibernate.connection.driver_class">${database.driver}</prop>
<prop key="hibernate.connection.shutdown">true</prop>
<prop key="hibernate.connection.writedelay">0</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.show_sql">${database.show_sql}</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.ejb.metamodel.generation">disabled</prop>
<!-- Use the C3P0 connection pool provider -->
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">30</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">600</prop>
</props>
</property>
Run Code Online (Sandbox Code Playgroud)
有人能指出我如何以这种方式配置HikariCP吗?
我在这里玩BarCode扫描仪的原始示例:
他们可以在相机工厂内启动AutoFocus/Flash,如下所示:
// Creates and starts the camera. Note that this uses a higher resolution in comparison
// to other detection examples to enable the barcode detector to detect small barcodes
// at long distances.
CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedPreviewSize(1600, 1024)
.setRequestedFps(15.0f);
// make sure that auto focus is an available option
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
builder = builder.setFocusMode(
autoFocus ? Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE : null);
}
mCameraSource = builder
.setFlashMode(useFlash ? Camera.Parameters.FLASH_MODE_TORCH : null)
.build();
Run Code Online (Sandbox Code Playgroud)
但是,cameraSource构建器上的此方法在当前版本中已消失,因此无法访问此设置.此外,我需要在使用过程中更改FlashMode,因此这也不是这样做的方法.我发现这个丑陋的解决方案来访问相机: …
考虑一个Java方法,它通过Java类推断出它的类型,如下所示:
public <T> T readJson(Class<T> c) throws IOException {
Run Code Online (Sandbox Code Playgroud)
这允许做这样的事情:
Map<String, String> map = foo.readJson(Map.class);
Run Code Online (Sandbox Code Playgroud)
在java中它将警告未经检查的强制转换,但它将正常工作.但是在Kotlin中,这不会那么容易,可以尝试使用:
foo.readJson(Map::class.java)
Run Code Online (Sandbox Code Playgroud)
但是,如果Map<String, String>需要,它将无法工作:
Type inference failed. Expected type mismatch.
required Map<String, String>
found Map<*, *>!
Run Code Online (Sandbox Code Playgroud)
我还尝试定义一个接口StringMap:
interface StringMap : Map<String, String>
Run Code Online (Sandbox Code Playgroud)
然而,这也不起作用,它将导致这样的例外:
Cannot cast ...LinkedTreeMap to ...StringMap
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
hibernate ×3
java ×3
c3p0 ×2
spring ×2
android ×1
camera-flash ×1
hikaricp ×1
jpa ×1
kotlin ×1
maven ×1
mysql ×1
spring-mvc ×1
terraform ×1
tsc ×1
typescript ×1