出于培训目的我目前正在重写我的应用程序,使用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 Mapping:
<?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 annonation时,我真的不明白为什么数据库访问有效,但后来说使用它时没有连接...对我来说没有意义。
最好的问候 daZza编辑:根据使用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
答案 0 :(得分:1)
我认为您的jdbc驱动程序网址不正确:
对于HSQL而言
org.hsqldb.jdbcDriver
你似乎有
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
这是一个MS-SQL / Sybase驱动程序类。
此时@Transaction注释失败的主要原因是您正在使用hibernate事务管理器(因此仅在您开始使用会话工厂时才能工作)。
<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 with sessionFactory.getCurrentSession()。connection()。这实际上是deprectated所以你最好不要使用sessionFactory.getCurrentSession.doWork()代替