- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
出于培训目的,我目前正在重写我的应用程序,以使用 Spring 和 Hibernate 的组合来访问我的本地 HSQLDB,而不是仅通过 JDBC 来访问。
我认为我已经完成了大部分工作,当我出于测试目的在主要方法上执行一些方法时,一切似乎都工作正常。但是,一旦我想通过 @Transactional 启用事务处理,整个事情就会停止工作,告诉我不存在连接。
我已经尝试了 stackoverflow 上类似问题中给出的一些解决方案,但没有一个对我有用。
首先是完整的堆栈跟踪:http://pastebin.com/ACkp7Ysj
这是我的类和配置文件:
数据源配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url" value="jdbc:hsqldb:file:C:\\dev\\source\\productionDB\\productionDB;shutdown=true" />
<property name="username" value="admin" />
<property name="password" value="admin" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="exercise.java.basics.storage" />
<property name="mappingLocations">
<list>
<value>classpath:hibernate/Warehouse.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
DAO 配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="warehouseDAO" class="exercise.java.basics.storage.WarehouseDAOImpl">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
模块配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<import resource="database/Spring-Datasource.xml" />
<import resource="warehouse/Spring-Warehouse.xml" />
</beans>
hibernate 映射:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="exercise.java.basics.storage.WarehouseNew" table="WAREHOUSE">
<id name="product_ID" type="integer">
<column name="product_ID" not-null="true"/>
<generator class="identity" />
</id>
<property name="product_name" type="string">
<column name="product_name" length="100"/>
</property>
<property name="product_count" type="integer">
<column name="product_count"/>
</property>
</class>
</hibernate-mapping>
DAO 模式:
public interface WarehouseDAO {
public void initializeWarehouse();
public void storeProducts( final Product product, final int count );
public void removeProducts( final Product product, final int count );
public void updateStock();
}
DAO 实现:
@Transactional
@Repository
public class WarehouseDAOImpl implements WarehouseDAO {
private DataSource dataSource;
@Autowired
private SessionFactory sessionFactory;
public void setDataSource( final DataSource dataSource ) {
this.dataSource = dataSource;
}
public void setSessionFactory( final SessionFactory sessionFactory ) {
this.sessionFactory = sessionFactory;
}
public void initializeWarehouse() {
String initProductsSQL = "INSERT INTO WAREHOUSE(product_name, product_count) VALUES (?, 0)";
Connection conn = null;
try {
conn = DataSourceUtils.getConnection( this.dataSource );
Statement stmt = conn.createStatement();
PreparedStatement ps = conn.prepareStatement( initProductsSQL );
ps.setString( 1, String.valueOf( Product.NAIL ) );
ps.executeUpdate();
ps.setString( 1, String.valueOf( Product.SCREW ) );
ps.executeUpdate();
ps.setString( 1, String.valueOf( Product.FINALPRODUCT ) );
ps.executeUpdate();
ps.close();
} catch ( SQLException e ) {
throw new RuntimeException( e );
} finally {
if ( conn != null ) {
try {
conn.close();
} catch ( SQLException e ) {
e.printStackTrace();
}
}
}
}
public void storeProducts( final Product product, final int count ) {
String updateProductCountSQL = "UPDATE WAREHOUSE SET product_count = product_count + " + count
+ " WHERE product_name = '" + product + "'";
Connection conn = null;
try {
conn = DataSourceUtils.getConnection( this.dataSource );
Statement stmt = conn.createStatement();
stmt.execute( updateProductCountSQL );
stmt.close();
} catch ( SQLException e ) {
throw new RuntimeException( e );
} finally {
if ( conn != null ) {
try {
conn.close();
} catch ( SQLException e ) {
e.printStackTrace();
}
}
}
}
public void removeProducts( final Product product, final int count ) {
String updateProductCountSQL = "UPDATE WAREHOUSE SET product_count = product_count - " + count
+ " WHERE product_name = '" + product + "'";
Connection conn = null;
try {
conn = DataSourceUtils.getConnection( this.dataSource );
Statement stmt = conn.createStatement();
stmt.execute( updateProductCountSQL );
stmt.close();
} catch ( SQLException e ) {
throw new RuntimeException( e );
} finally {
if ( conn != null ) {
try {
conn.close();
} catch ( SQLException e ) {
e.printStackTrace();
}
}
}
}
public void updateStock() {
String getUpdatedStocksSQL = "SELECT * FROM WAREHOUSE WHERE product_name = ? ;";
Connection conn = null;
try {
conn = DataSourceUtils.getConnection( this.dataSource );
PreparedStatement ps = conn.prepareStatement( getUpdatedStocksSQL );
WarehouseNew warehouse = exercise.java.basics.storage.WarehouseNew.getInstance();
ps.setString( 1, String.valueOf( Product.NAIL ) );
ResultSet rsNail = ps.executeQuery();
ps.setString( 1, String.valueOf( Product.SCREW ) );
ResultSet rsScrew = ps.executeQuery();
ps.setString( 1, String.valueOf( Product.FINALPRODUCT ) );
ResultSet rsFinalProduct = ps.executeQuery();
rsNail.next();
rsScrew.next();
rsFinalProduct.next();
warehouse.setNailCount( rsNail.getString( 3 ) );
warehouse.setScrewCount( rsNail.getString( 3 ) );
warehouse.setFinalProductCount( rsNail.getString( 3 ) );
//Debug
System.out.println( rsNail.getInt( 3 ) );
System.out.println( rsScrew.getInt( 3 ) );
System.out.println( rsFinalProduct.getInt( 3 ) );
rsNail.close();
rsScrew.close();
rsFinalProduct.close();
ps.close();
} catch ( SQLException e ) {
throw new RuntimeException( e );
} finally {
if ( conn != null ) {
try {
conn.close();
} catch ( SQLException e ) {
e.printStackTrace();
}
}
}
}
public static void main( final String[] args ) {
ApplicationContext context = new ClassPathXmlApplicationContext( "Spring-Module.xml" );
WarehouseDAO warehouseDAO = (WarehouseDAO) context.getBean( "warehouseDAO" );
// warehouseDAO.initializeWarehouse();
warehouseDAO.updateStock();
warehouseDAO.storeProducts( Product.NAIL, 10 );
warehouseDAO.storeProducts( Product.SCREW, 20 );
warehouseDAO.storeProducts( Product.FINALPRODUCT, 30 );
warehouseDAO.updateStock();
}
}
模型类:
public class WarehouseNew {
private int nailCount;
private int screwCount;
private int finalProductCount;
private int product_ID;
private String product_name;
private int product_count;
private static volatile WarehouseNew instance = null;
public static WarehouseNew getInstance() {
if ( instance == null ) {
synchronized ( WarehouseNew.class ) {
if ( instance == null ) {
instance = new WarehouseNew();
}
}
}
return instance;
}
/**
* Constructor for ...
*/
private WarehouseNew() {
}
/**
* @return the nailCount
*/
public int getNailCount() {
return this.nailCount;
}
/**
* @param string the nailCount to set
*/
public void setNailCount( final String string ) {
this.nailCount = Integer.parseInt( string );
}
/**
* @return the screwCount
*/
public int getScrewCount() {
return this.screwCount;
}
/**
* @param string the screwCount to set
*/
public void setScrewCount( final String string ) {
this.screwCount = Integer.parseInt( string );
}
/**
* @return the finalProductCount
*/
public int getFinalProductCount() {
return this.finalProductCount;
}
/**
* @param string the finalProductCount to set
*/
public void setFinalProductCount( final String string ) {
this.finalProductCount = Integer.parseInt( string );
}
/**
* @return the product_id
*/
public int getProduct_ID() {
return this.product_ID;
}
/**
* @param product_id the product_id to set
*/
public void setProduct_ID( final int product_ID ) {
this.product_ID = product_ID;
}
/**
* @return the product_name
*/
public String getProduct_name() {
return this.product_name;
}
/**
* @param product_name the product_name to set
*/
public void setProduct_name( final String product_name ) {
this.product_name = product_name;
}
/**
* @return the product_count
*/
public int getProduct_count() {
return this.product_count;
}
/**
* @param product_count the product_count to set
*/
public void setProduct_count( final int product_count ) {
this.product_count = product_count;
}
}
我希望有人能指出我正确的方向。 Spring 框架和 Hibernate 对我来说是全新的,我只是希望它能够工作。
我真的不明白为什么当我不使用 @Transactional 注释时数据库访问会起作用,但随后又说使用它时没有连接......对我来说毫无意义。
最诚挚的问候达扎
编辑:根据使用 sessionFactory 的建议进行更改。但错误仍然相同。
// Experimental
this.sessionFactory.getCurrentSession().doWork(
new Work() {
public void execute( final Connection connection ) throws SQLException {
try {
String getUpdatedStocksSQL = "SELECT * FROM WAREHOUSE WHERE product_name = ? ;";
PreparedStatement ps = connection.prepareStatement( getUpdatedStocksSQL );
WarehouseNew warehouse = exercise.java.basics.storage.WarehouseNew.getInstance();
ps.setString( 1, String.valueOf( Product.NAIL ) );
ResultSet rsNail = ps.executeQuery();
ps.setString( 1, String.valueOf( Product.SCREW ) );
ResultSet rsScrew = ps.executeQuery();
ps.setString( 1, String.valueOf( Product.FINALPRODUCT ) );
ResultSet rsFinalProduct = ps.executeQuery();
rsNail.next();
rsScrew.next();
rsFinalProduct.next();
warehouse.setNailCount( rsNail.getString( 3 ) );
warehouse.setScrewCount( rsNail.getString( 3 ) );
warehouse.setFinalProductCount( rsNail.getString( 3 ) );
System.out.println( rsNail.getInt( 3 ) );
System.out.println( rsScrew.getInt( 3 ) );
System.out.println( rsFinalProduct.getInt( 3 ) );
rsNail.close();
rsScrew.close();
rsFinalProduct.close();
ps.close();
} catch ( SQLException e ) {
throw new RuntimeException( e );
} finally {
if ( connection != null ) {
try {
connection.close();
} catch ( SQLException e ) {
e.printStackTrace();
}
}
}
}
} );
// !Experimental
最佳答案
我认为您的 jdbc 驱动程序 URL 不正确:
对于 HSQL 来说是
org.hsqldb.jdbcDriver
你似乎有
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
这是一个 MS-SQL/Sybase 驱动程序类。
您的 @Transaction 注释此时失败的主要原因是您正在使用 hibernate 事务管理器(因此仅在您开始使用 session 工厂时才起作用)。
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
为了使旧的 jdbc 代码正常工作,您需要使用类似的东西:
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
另一种方法是将 DataSourceUtils.getConnection( this.dataSource 替换为 sessionFactory.getCurrentSession().connection()。这实际上是 deprectated 所以你最好使用 sessionFactory.getCurrentSession.doWork() 来代替。
关于java - Spring/Hibernate/HSQLDB 组合的 session /事务问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21598773/
有没有办法截断HSQLDB中的所有表? 如果这不可能,是否有任何方法可以级联删除具有外键引用的表? 最佳答案 可以截断模式中的所有表: 截断模式并提交 此命令有一些选项在指南中有描述: http://
DB 中的表数量有限制吗? 模式内的表数量是否有一些限制? 最佳答案 希望这可以帮助 What are the limitations of the database (size of columns
我想在 .script 文件中评论一个查询,我该怎么做?我用 # 测试过, -- , ({}) , , ::没有任何效果。我收到有关意外 token 的广告异常。 最佳答案 Spring 嵌入式数据
我想以编程方式启动 HSQLdb 服务器。 我创建了一个如下所示的可运行对象,然后在一个线程中启动它。 Runnable hsqlRunnable = new Runnable() {
我正在尝试使用 Liquibase 1.9.5 将一些数据加载到 HSQLDB 数据库中。我有一个 loadData 命令如下: 在我的 CSV 数据文件中,我试图将
我正在使用 HSQLDB 来持久化一个小数据,在我的查询中,我想在第一次创建表,如果它们不再存在。 但是,对于 HSQLDB,我无法像 mysql 或 mssql 等其他 dbms 一样执行查询“CR
我正在尝试将 HSQLDB 用作 spring 应用程序中的嵌入式数据库(用于测试)。由于目标生产数据库是Oracle,我想使用HSQLDBs Oracle 语法模式特性。 在我使用的 Spring
是否有与 MYSQL SHOW COLUMNS from TABLE 命令等效的 HSQLDB? 最佳答案 HSQLDB 没有用于显示表、列或其他数据库对象的单独命令。 为此目的,您使用 SELECT
我想做什么 如何让 maven/eclipse 指向 2.3.2 jar? 通过 Java/jdbc 连接到 HSQL 数据库(我想一旦我解决了上述问题我应该能够这样做) 我的设置 看来我的hsql服
当我的列名包含连字符“-”时出现异常 Entity : this is the entity name. @Entity @Table(name = "RequestHeade
我们有一个设置,使用嵌入式 HSQLDB 来支持 Java 中的 Hibernate/JPA 单元测试,并且我们使用内存数据库模式,因为我们只是希望在测试运行后丢弃数据库。我的问题是,其中一项测试由于
如何在HSQLDB 和C# .net 之间形成连接?我已经看过 SharpHSQL 和 H2Sharp,但无法连接 HSQLDB。 最佳答案 试试这样:确保您已经添加 hsqldb.dll、IKVM.
我正在尝试使用 Hibernate 和 HSQLDB。我试图将教程改编为更当前的版本,所以可能存在问题。当我使用 JUnit 运行单元测试时会发生这种情况。 我将 HSQLDB 2.3.2 与 Hib
我有 DAO 代码,其中包含一些具有 Oracle 特定语法的 JDBC,例如: select count(*) cnt from DUAL where exists (select null fro
我经常使用内存中的 HSQL 数据库作为测试数据库来编写依赖于数据库的代码的单元测试。最近我决定从 1.8.1.3 升级到 2.2.9 以利用 2.x 版本分支中添加的 ROW_NUMBER() 支持
要使用缓存表,我可以使用驱动程序 URL:jdbc:hsqldb:mem或者我应该使用 jdbc:hsqldb:file ? 我需要一个非持久性临时表来保存更多可以放入内存的数据。 文档中不清楚是否使
我正在尝试使用 2.2.9、Hibernate 3.6.9 和 Spring 3.1.2 版本设置 HSQL 数据库进行测试。我们一直在使用本地 postgresql 数据库,但正在转换以进行测试。我
我正在创建一个使用嵌入式hsqldb的基于Spring的Web应用程序。 我的spring配置非常简单: 但是通过此配置,所有数据都存储在内存中。这是创建的数据源URL jdbc:hsq
“如何索引 HSQL 数据库?” 最佳答案 您不索引数据库,而是在表中的字段上创建索引。您可以在单个列上创建索引,也可以在多个列上创建单个索引。 从这里开始 http://www.hsqldb.org
如何查看HSQLDB中表的结构(列的详细信息等)?它不像Oracle那样“desc”,那又如何呢? 最佳答案 该信息由INFORMATION_SCHEMA中的 View 提供 SELECT * FRO
我是一名优秀的程序员,十分优秀!