在我的Service中只有一个方法,如下:
public void insertOneNews(String src,Type type) throws Exception {
String temp = src.substring(0,src.lastIndexOf("/"))+"/sign"+src.substring(src.lastIndexOf("/"));
Parser parser = new Parser(temp);
parser.setEncoding("gb2312");
NodeList divList = parser.parse(new HasAttributeFilter("class", "gaishu"));
String title = "";
String content = "";
Div div = (Div) divList.elementAt(0);
Parser parser2 = new Parser(div.getChildrenHTML());
NodeList nodeList = parser2.parse(new TagNameFilter("div"));
Div div2 = (Div)nodeList.elementAt(0);
title = div2.getStringText();
System.out.println(title);
Div div3 = (Div)nodeList.elementAt(1);
content = div3.getStringText();
Item item = new Item();
item.setTitle(title);
item.setContent(content);
item.setType(type); this.save(item);
}使用save方法使用无法保存到数据库, debug发现Item中不存在空值,到了save这里就无法保存了,一点异常也不报,我怀疑是事务没有提交的问题
但是,同一个包下面的另一个TypeSerivce就一切正常,下面是我的配置文件,和另一个Serivce的源码。
TypeService:
/**
*
*/
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.visitors.NodeVisitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springside.core.dao.HibernateEntityDao;import com.cn.ciecc.search.model.Type;@Service("typeService")
public class TypeService extends HibernateEntityDao<Type> {
@Autowired
private ItemService itemService;
public void insertAllTypes() { // 这个方法保存 非常正常
String str = new String("A B C D E F G H J K L M N P Q R S T W X Y Z");
String[] typesStr = str.split(" ");
Type[] types = new Type[typesStr.length];
for (int i = 0; i < typesStr.length; i++) {
types[i] = new Type();
types[i].setTypeName(typesStr[i]);
this.save(types[i]);
}
}
public void insertItems(String before,String after, Integer type) throws Exception {
//得到当前新闻所属类别
final Type typeName = this.get(Type.class, type); //insertOneNews为什么必须是final
String src = before+typeName.getTypeName().toLowerCase()+after;
//使用htmlParser获取每一条新闻的超链接
try {
Parser parser = new Parser(src);
parser.setEncoding("gb2312");
NodeList divList = parser.parse(new HasAttributeFilter("class","letrcon1"));
for (int j = 0; j < divList.size(); j++) {
Div div = (Div) divList.elementAt(j);
String divClass = div.getAttribute("class");
//取得id为news_list的div
if(divClass != null && divClass.equals("letrcon1")) {
div.accept(new NodeVisitor() {
//遍历news_list里面的所有超链接
public void visitTag(Tag tag) {
if(tag instanceof LinkTag) {
String href = ((LinkTag)tag).getLink();
if(!href.startsWith("http")) {
href = "http://。。/" + href;
}
//找到超链接,将这个超链接所在的页面进行处理,抓取数据
try{
itemService.insertOneNews(href,typeName);
}catch(Exception e) {
e.printStackTrace();
}
}
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
配置文件:
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<!-- 属性文件读入 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc/jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 dataSource -->
<bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- <property name="initialPoolSize" value="10"/>
<property name="minPoolSize" value="10"/>
<property name="maxPoolSize" value="150"/>
<property name="maxIdleTime" value="25000"/>
<property name="acquireIncrement" value="5" />
<property name="idleConnectionTestPeriod" value="1800"/>
<property name="checkoutTimeout" value="2000"/> -->
</bean>
<!-- 配置Session工厂 -->
<!-- annotation.AnnotationSessionFactoryBean 使用标注 进行映射 -->
<bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource"><ref bean = "dataSource" /></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate_dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan" value="com.cn.ciecc.search.model" />
</bean>
<!-- Hibernate TransactionManager -->
<bean id = "transactionManager" class = "org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref bean = "sessionFactory" /></property>
</bean>
<!-- 支持 @Transactional 标记 -->
<tx:annotation-driven/> <!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy/>
<!-- 以AspectJ方式 定义 AOP -->
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.cn.ciecc.search.service..*Service.*(..))" advice-ref="txAdvice"/>
<aop:advisor pointcut="execution(* org.springside.core.dao.*Dao.*(..))" advice-ref="txAdvice"/>
</aop:config>
<!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置.默认的设置请参考Spring文档事务一章. -->
<tx:advice id = "txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 启用类扫描机制以启用注释驱动,使用annotation自动注册bean ,并检查@AutoWired @Required 的属性已注入 -->
<context:component-scan base-package="com.cn.ciecc.search" />
</beans>
public void insertOneNews(String src,Type type) throws Exception {
String temp = src.substring(0,src.lastIndexOf("/"))+"/sign"+src.substring(src.lastIndexOf("/"));
Parser parser = new Parser(temp);
parser.setEncoding("gb2312");
NodeList divList = parser.parse(new HasAttributeFilter("class", "gaishu"));
String title = "";
String content = "";
Div div = (Div) divList.elementAt(0);
Parser parser2 = new Parser(div.getChildrenHTML());
NodeList nodeList = parser2.parse(new TagNameFilter("div"));
Div div2 = (Div)nodeList.elementAt(0);
title = div2.getStringText();
System.out.println(title);
Div div3 = (Div)nodeList.elementAt(1);
content = div3.getStringText();
Item item = new Item();
item.setTitle(title);
item.setContent(content);
item.setType(type); this.save(item);
}使用save方法使用无法保存到数据库, debug发现Item中不存在空值,到了save这里就无法保存了,一点异常也不报,我怀疑是事务没有提交的问题
但是,同一个包下面的另一个TypeSerivce就一切正常,下面是我的配置文件,和另一个Serivce的源码。
TypeService:
/**
*
*/
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.visitors.NodeVisitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springside.core.dao.HibernateEntityDao;import com.cn.ciecc.search.model.Type;@Service("typeService")
public class TypeService extends HibernateEntityDao<Type> {
@Autowired
private ItemService itemService;
public void insertAllTypes() { // 这个方法保存 非常正常
String str = new String("A B C D E F G H J K L M N P Q R S T W X Y Z");
String[] typesStr = str.split(" ");
Type[] types = new Type[typesStr.length];
for (int i = 0; i < typesStr.length; i++) {
types[i] = new Type();
types[i].setTypeName(typesStr[i]);
this.save(types[i]);
}
}
public void insertItems(String before,String after, Integer type) throws Exception {
//得到当前新闻所属类别
final Type typeName = this.get(Type.class, type); //insertOneNews为什么必须是final
String src = before+typeName.getTypeName().toLowerCase()+after;
//使用htmlParser获取每一条新闻的超链接
try {
Parser parser = new Parser(src);
parser.setEncoding("gb2312");
NodeList divList = parser.parse(new HasAttributeFilter("class","letrcon1"));
for (int j = 0; j < divList.size(); j++) {
Div div = (Div) divList.elementAt(j);
String divClass = div.getAttribute("class");
//取得id为news_list的div
if(divClass != null && divClass.equals("letrcon1")) {
div.accept(new NodeVisitor() {
//遍历news_list里面的所有超链接
public void visitTag(Tag tag) {
if(tag instanceof LinkTag) {
String href = ((LinkTag)tag).getLink();
if(!href.startsWith("http")) {
href = "http://。。/" + href;
}
//找到超链接,将这个超链接所在的页面进行处理,抓取数据
try{
itemService.insertOneNews(href,typeName);
}catch(Exception e) {
e.printStackTrace();
}
}
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
配置文件:
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<!-- 属性文件读入 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc/jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 dataSource -->
<bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- <property name="initialPoolSize" value="10"/>
<property name="minPoolSize" value="10"/>
<property name="maxPoolSize" value="150"/>
<property name="maxIdleTime" value="25000"/>
<property name="acquireIncrement" value="5" />
<property name="idleConnectionTestPeriod" value="1800"/>
<property name="checkoutTimeout" value="2000"/> -->
</bean>
<!-- 配置Session工厂 -->
<!-- annotation.AnnotationSessionFactoryBean 使用标注 进行映射 -->
<bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource"><ref bean = "dataSource" /></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate_dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan" value="com.cn.ciecc.search.model" />
</bean>
<!-- Hibernate TransactionManager -->
<bean id = "transactionManager" class = "org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref bean = "sessionFactory" /></property>
</bean>
<!-- 支持 @Transactional 标记 -->
<tx:annotation-driven/> <!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy/>
<!-- 以AspectJ方式 定义 AOP -->
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.cn.ciecc.search.service..*Service.*(..))" advice-ref="txAdvice"/>
<aop:advisor pointcut="execution(* org.springside.core.dao.*Dao.*(..))" advice-ref="txAdvice"/>
</aop:config>
<!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置.默认的设置请参考Spring文档事务一章. -->
<tx:advice id = "txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 启用类扫描机制以启用注释驱动,使用annotation自动注册bean ,并检查@AutoWired @Required 的属性已注入 -->
<context:component-scan base-package="com.cn.ciecc.search" />
</beans>
解决方案 »
- 用过的一个实用的JAVA代码生成,要的就来拿!【有图有真相,来迟了别后悔】
- 为什么我的post方法总是返回false?
- hibrnate如何实现分页,并且要倒序。就像留言板留言一样。
- 问下坦克游戏的重绘问题
- No action instance for path /preAgentarea could be created
- 请教用List来匹配数据库中记录的问题!急!!!
- 怎么才能知道jdbc执行的sql语句,得到了正确的结果。
- Java和VC用SOCKET通信,连接失败,大家帮我看一下
- 各位大侠,请指教设置weblogic的ip的问题。
- log4j写入数据库
- 非对称解密时报错,求解!
- 根据下面的代码给解释一下
Hibernate: insert into bo_lucene_item (type_id, content, title) values (?, ?, ?)看,有啊,但是数据库中就是没有。
而且奇怪的是,一个表是可以正常操作的,一个就不行。
Hibernate: select type0_.id as id1_0_, type0_.typeName as typeName1_0_ from bo_lucene_type type0_ where type0_.id=?
Hibernate: insert into bo_lucene_item (type_id, content, title) values (?, ?, ?)
Hibernate: insert into bo_lucene_item (type_id, content, title) values (?, ?, ?)
Hibernate: insert into bo_lucene_item (type_id, content, title) values (?, ?, ?)
Hibernate: insert into bo_lucene_item (type_id, content, title) values (?, ?, ?)
看sql语句有什么不同
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
Hibernate: insert into bo_lucene_type (typeName) values (?)
没有第一句的查询语句,还是表有问题,但还是不明
Hibernate: select type0_.id as id1_0_, type0_.typeName as typeName1_0_ from bo_lucene_type type0_ where type0_.id=?
insertOneNews(String src,Type type) throws Exception
这个方法的异常你try catch下,别throws,估计会有异常出现。
我把这个方法的 throws Exception 可以保存数据库了
但是新的问题又出现了,Item表中有id title content 和typeid字段 content字段中始终没有数据,其他三列都正常,而且,每次测试时,这个表只保存固定的49条记录,就不在保存了。期间抛出了一个异常 ,但不是每次都抛出,org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been ed as rollback-only
查了一下,好像是因为嵌套事务惹起的
帖子:http://yidianfengfan.iteye.com/blog/550826
Item item = new Item();
item.setTitle(div2.getStringText());
item.setContent(div3.getStringText());
item.setType(type);
this.save(item);
System.out.println(div3.getStringText());
我是这样先保存后输出的,明显这个save方法有问题,还有爆出的那个异常,可能是嵌套事务引起的原因。
又或者是本身表就有问题
谢谢上面那位朋友的帮忙。