- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Hibernate(尝试学习)和 Derby;我使用 Squirrel 和 SQL 脚本创建并检查数据库,并在程序中向其中写入 3 条记录。
我收到“事务仍处于 Activity 状态时无法关闭连接”;它似乎出现在 hibernate 配置过程中,但我在数据库中创建的记录无论如何都会写在那里。我尝试过非“40”驱动程序,我将用户名和密码作为 URL 参数而不是在 hibernate 配置中单独放入,我无法弄清楚可能正在运行哪些其他事务。我在循环中对 create() 的调用处放置了一个断点,当它被击中时,错误已经出现在控制台中,因此它与我正在写出的记录无关。大家还有其他想法吗?
主要程序代码如下:
package samplerc;
import sample.CreateUser;
public class CreateRecords
{
public static void main(String[] args)
{
try
{
String[][] userList =
{ { "rc", "1" },
{ "pbh", "2" },
{ "jbc", "3" }
};
CreateUser userCreator = new CreateUser();
userCreator.addUserList(userList);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
这是 CreateUser:
package sample;
import org.hibernate.HibernateException;
import sample.dao.DAO;
import sample.entity.User;
public class CreateUser extends DAO
{
public void addUserList(String[][] userList)
throws Exception
{
try
{
for (int i=0; i<userList.length; i++)
{
create(userList[i][0], userList[i][1]);
System.out.println("Created user");
DAO.close();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public User create(String username, String password) throws Exception
{
try
{
begin();
User user = new User(username, password);
getSession().save(user);
commit();
return user;
}
catch (HibernateException e)
{
throw new Exception("Could not create user " + username, e);
}
}
}
DAO 基类曾经在这里;它现在位于下面的编辑中,因为我替换了它:
hibernate 配置文件:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url"> jdbc:derby://localhost:1527/C:/Users/rcook/workspaceGalileoLaptop/BHChap3/BegHibernateDB</property>
<property name="hibernate.connection.driver_class"> org.apache.derby.jdbc.ClientDriver40 </property>
<property name="hibernate.connection.username">admin</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.dialect"> org.hibernate.dialect.DerbyDialect </property>
<property name="hibernate.connection.pool_size">0</property>
<property name="hibernate.show_sql">false</property>
<!-- "Import" the mapping resources here -->
<mapping class="sample.entity.Message"/>
<mapping class="sample.entity.Advert"/>
<mapping class="sample.entity.Category"/>
<mapping class="sample.entity.User"/>
</session-factory>
</hibernate-configuration>
日志文件曾经在这里;由于发帖限制,我已将其删除,因为我在下面的编辑中有另一个日志。
-- 在 Guido 回答后更新
Guido,这是一个非常好的主意,我确信它是正确的。不幸的是,我所做的改变似乎并没有解决它。这是新的 DAO 类,以及您建议的更改:
package sample.dao;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class DAO
{
private static final Logger log = Logger.getAnonymousLogger();
private static SessionFactory sessionFactory = null;
private static final ThreadLocal threadLocalSession = new ThreadLocal();
protected DAO()
{
}
private static synchronized SessionFactory getSessionFactory()
{
if (sessionFactory == null)
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
return sessionFactory;
}
public static Session getSession()
{
String threadName = Thread.currentThread().getName();
System.out.println(String.format("getting session, thread = '%s'", threadName));
Session localSession = (Session) DAO.threadLocalSession.get();
if (localSession == null)
{
try
{
localSession = getSessionFactory().openSession();
DAO.threadLocalSession.set(localSession);
}
catch (HibernateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return localSession;
}
protected void begin()
{
try
{
getSession().beginTransaction();
}
catch (HibernateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void commit()
{
try
{
getSession().getTransaction().commit();
}
catch (HibernateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void rollback()
{
try
{
getSession().getTransaction().rollback();
}
catch (HibernateException e)
{
log.log(Level.WARNING, "Cannot rollback", e);
}
try
{
getSession().close();
}
catch (HibernateException e)
{
log.log(Level.WARNING, "Cannot close", e);
}
DAO.threadLocalSession.set(null);
}
public static void close()
{
getSession().close();
// try { Thread.sleep(5000); } catch (InterruptedException ie) {}
DAO.threadLocalSession.set(null);
}
}
这是新的日志文件。我陷入了跟踪语句中,发现它的输出与其余输出相比很有趣。欢迎您(或其他任何人)提出任何其他想法。
Starting...
getting session, thread = 'main'
Feb 19, 2013 10:25:21 AM org.hibernate.annotations.common.Version <clinit>
INFO: Hibernate Commons Annotations 3.2.0.Final
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.6.10.Final
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : javassist
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Feb 19, 2013 10:25:21 AM org.hibernate.util.DTDEntityResolver resolveEntity
WARNING: recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: sample.entity.Message
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity sample.entity.Message on table Message
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: sample.entity.Advert
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity sample.entity.Advert on table Advert
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: sample.entity.Category
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity sample.entity.Category on table Category
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: sample.entity.User
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity sample.entity.User on table AdUser
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.Configuration applyHibernateValidatorLegacyConstraintsOnDDL
INFO: Hibernate Validator not found: ignoring
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.search.HibernateSearchEventListenerRegister enableHibernateSearch
INFO: Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
Feb 19, 2013 10:25:21 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
Feb 19, 2013 10:25:21 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 0
Feb 19, 2013 10:25:21 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
Feb 19, 2013 10:25:21 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: org.apache.derby.jdbc.ClientDriver40 at URL: jdbc:derby://localhost:1527/BegHibernateDB
Feb 19, 2013 10:25:21 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=admin, password=****}
Feb 19, 2013 10:25:21 AM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.DerbyDialect
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Database ->
name : Apache Derby
version : 10.8.1.2 - (1095077)
major : 10
minor : 8
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Driver ->
name : Apache Derby Network Client JDBC Driver
version : 10.8.1.2 - (1095077)
major : 10
minor : 8
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
WARNING: Could not obtain connection to query metadata
java.sql.SQLException: Cannot close a connection while a transaction is still active.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.Connection.closeResourcesX(Unknown Source)
at org.apache.derby.client.am.Connection.closeX(Unknown Source)
at org.apache.derby.client.net.NetConnection.closeX(Unknown Source)
at org.apache.derby.client.am.Connection.close(Unknown Source)
at org.apache.derby.client.net.NetConnection.close(Unknown Source)
at org.apache.derby.client.net.NetConnection40.close(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.closeConnection(DriverManagerConnectionProvider.java:160)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:143)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2863)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
at sample.dao.DAO.getSessionFactory(DAO.java:25)
at sample.dao.DAO.getSession(DAO.java:39)
at sample.dao.DAO.begin(DAO.java:55)
at sample.CreateUser.create(CreateUser.java:77)
at sample.CreateUser.addUserList(CreateUser.java:61)
at samplerc.CreateRecords.main(CreateRecords.java:19)
Caused by: org.apache.derby.client.am.SqlException: Cannot close a connection while a transaction is still active.
at org.apache.derby.client.am.Connection.checkForTransactionInProgress(Unknown Source)
... 17 more
Feb 19, 2013 10:25:21 AM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.DerbyDialect
Feb 19, 2013 10:25:21 AM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
Feb 19, 2013 10:25:21 AM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
Feb 19, 2013 10:25:21 AM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory createRegionFactory
INFO: Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
Feb 19, 2013 10:25:21 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Check Nullability in Core (should be disabled when Bean Validation is on): enabled
Feb 19, 2013 10:25:22 AM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Feb 19, 2013 10:25:22 AM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [characters_clob] overrides previous : org.hibernate.type.PrimitiveCharacterArrayClobType@494eaec9
Feb 19, 2013 10:25:22 AM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [materialized_clob] overrides previous : org.hibernate.type.MaterializedClobType@4cb533b8
Feb 19, 2013 10:25:22 AM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [wrapper_materialized_blob] overrides previous : org.hibernate.type.WrappedMaterializedBlobType@c569c60
Feb 19, 2013 10:25:22 AM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [materialized_blob] overrides previous : org.hibernate.type.MaterializedBlobType@a3468f4
Feb 19, 2013 10:25:22 AM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [blob] overrides previous : org.hibernate.type.BlobType@76d67067
Feb 19, 2013 10:25:22 AM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [java.sql.Blob] overrides previous : org.hibernate.type.BlobType@76d67067
Feb 19, 2013 10:25:22 AM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [clob] overrides previous : org.hibernate.type.ClobType@786db724
Feb 19, 2013 10:25:22 AM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [java.sql.Clob] overrides previous : org.hibernate.type.ClobType@786db724
Feb 19, 2013 10:25:22 AM org.hibernate.type.BasicTypeRegistry register
INFO: Type registration [wrapper_characters_clob] overrides previous : org.hibernate.type.CharacterArrayClobType@11c7865b
Feb 19, 2013 10:25:22 AM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
getting session, thread = 'main'
getting session, thread = 'main'
Created user
getting session, thread = 'main'
getting session, thread = 'main'
getting session, thread = 'main'
getting session, thread = 'main'
Created user
getting session, thread = 'main'
getting session, thread = 'main'
getting session, thread = 'main'
getting session, thread = 'main'
Created user
getting session, thread = 'main'
最佳答案
这是因为在扩展 DAO 的四个映射类中的每一个的初始化( <clinit>
,而不是构造函数)中,都会执行以下操作:
private static final SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
编写一个合适的 HibernateUtil 类(例如 here 中的类);或者做类似的事情:
private static final SessionFactory sessionFactory;
private static synchronized SessionFactory getSessionFactory() {
if (sessionFactory == null) {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
return sessionFactory;
}
然后只能通过getSessionFactory()引用sessionFactory。
编辑:看看这个:https://forum.hibernate.org/viewtopic.php?f=6&t=953499
这正是您的问题,请检查您的数据库和该数据库的架构实际上位于预期位置。
编辑2:阅读评论here在第101行,然后尝试设置属性 hibernate.temp.use_jdbc_metadata_defaults
属性为 false。
该属性已从 hibernate 4.x 中删除
关于java - hibernate 与 Derby : Cannot close a connection while a transaction is still active,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14949428/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!