王朝网络
分享
 
 
 

read AppFuse 12-实践-建立DAO

王朝java/jsp·作者佚名  2006-01-31
宽屏版  字体: |||超大  

● 按照AppFuse帮助,实践

实践次步时,一切都还正常,唯有最后测试时,始终不通,试了n多办法,无果。如果那位网友看到,知道结果的,烦请告知,在此先谢过了。

具体的错误分析见文尾。

Person是基本的POJO,PersonDAO是Person的管理类,PersonManager是 Business Facades. 这些façade用于连接前端和DAO层之间的通讯。

Ø 在web环境下,Spring通过配置初始化后,使用WebApplicationContextUtils来获得ApplicationContext.

ApplicationContext ctx =

WebApplicationContextUtils.getRequiredWebApplicationContext(context);

Ø 在非web环境中,则通过ClassPathXmlApplicationContext获得ApplicationContext

ResourceBundle db = ResourceBundle.getBundle("database");

String daoType = db.getString("dao.type");

String[] paths = {"/WEB-INF/applicationContext-resources.xml",

"/WEB-INF/applicationContext-" + daoType + ".xml"};

ctx = new ClassPathXmlApplicationContext(paths);

Ø 建立PersonDAO接口

package org.dudu.dao;

import org.dudu.model.Person;

public interface PersonDAO extends DAO

{

public Person getPerson(Long personId);

public void savePerson(Person person);

public void removePerson(Long personId);

}

Ø 建立PersonDAO的的具体实现类

package org.dudu.dao.hibernate;

import org.dudu.model.Person;

import org.dudu.dao.PersonDAO;

import org.springframework.orm.ObjectRetrievalFailureException;

public class PersonDAOHibernate extends BaseDAOHibernate implements PersonDAO

{

public Person getPerson(Long personId)

{

Person person = (Person)getHibernateTemplate().get(Person.class,personId);

if(person == null)

{

throw new ObjectRetrievalFailureException(Person.class,personId);

}

return person;

}

public void removePerson(Long personId)

{

getHibernateTemplate().delete(getPerson(personId));

}

public void savePerson(Person person)

{

getHibernateTemplate().saveOrUpdate(person);

}

}

Ø 建立PersonDAO的测试类

package org.dudu.dao;

import org.dudu.model.Person;

import org.springframework.dao.DataAccessException;

public class PersonDAOTest extends BaseDAOTestCase{

private Person person = null;

private PersonDAO dao = null;

protected void setUp() throws Exception {

super.setUp();

dao = (PersonDAO)ctx.getBean("personDAO");

}

protected void tearDown() throws Exception {

super.tearDown();

dao = null;

}

public void testGetPerson() throws Exception

{

person = new Person();

person.setFirstName("dudu");

person.setLastName("tu");

dao.savePerson(person);

assertNotNull(person.getId());

person = dao.getPerson(person.getId());

assertEquals(person.getFirstName(),"dudu");

}

public void testSavePerson() throws Exception

{

person = dao.getPerson(new Long(1));

person.setFirstName("dudu");

person.setLastName("last name updated");

dao.savePerson(person);

if(log.isDebugEnabled())

{

log.debug("update person" + person);

}

}

public void testAddAndRemovePerson() throws Exception

{

person = new Person();

person.setFirstName("Zhang");

person.setLastName("San");

dao.savePerson(person);

assertEquals(person.getFirstName(),"Zhang");

assertNotNull(person.getId());

if(log.isDebugEnabled())

{

log.debug("remove person...");

}

dao.removePerson(person.getId());

try

{

person = dao.getPerson(person.getId());

fail("Person found in database");

}

catch(DataAccessException dae)

{

log.debug("Excepted exception:" + dae.getMessage());

assertNotNull(dae);

}

}

}

Ø 配置信息

Ø applicationContext-hibernate.xml

<bean id="personDAO" class="org.dudu.dao.hibernate.PersonDAOHibernate">

<property name="sessionFactory"><ref local="sessionFactory"/></property>

</bean>

Ø applicationContext-resources.xml

<beans>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:mail.properties</value>

<value>database.properties</value>

</list>

</property>

</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName">

<value>${hibernate.connection.driver_class}</value>

</property>

<property name="url">

<value>${hibernate.connection.url}</value>

</property>

<property name="username">

<value>${hibernate.connection.username}</value>

</property>

<property name="password">

<value>${hibernate.connection.password}</value>

</property>

</bean>

</beans>

Ø database.properties

#Hibernate Configuration for JUnit tests

#Sun Jul 10 18:03:26 CST 2005

hibernate.connection.username=test

dao.type=hibernate

hibernate.connection.password=test

hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect

hibernate.connection.url=jdbc\:mysql\://localhost/appfuse?autoReconnect\=true&useUnicode\=true&characterEncoding\=utf-8

hibernate.connection.show_sql=true

hibernate.connection.driver_class=com.mysql.jdbc.Driver

Ø 运行测试:ant test-dao -Dtestcase=PersonDAO

报错:

[junit] [dudu] WARN [main] JDBCExceptionReporter.logExceptions(57) | SQL Err

or: 0, SQLState: 08001

[junit] [dudu] ERROR [main] JDBCExceptionReporter.logExceptions(58) | Server

connection failure during transaction. Due to underlying exception: 'java.net.S

ocketException: java.net.SocketException: Permission denied: connect'.

[junit] ** BEGIN NESTED EXCEPTION **

[junit] java.net.SocketException

[junit] MESSAGE: java.net.SocketException: Permission denied: connect

[junit] STACKTRACE:

[junit] java.net.SocketException: java.net.SocketException: Permission denie

d: connect

[junit] at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFa

ctory.java:156)

[junit] at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:283)

[junit] at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)

[junit] at com.mysql.jdbc.Connection.<init>(Connection.java:1474)

[junit] at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDri

ver.java:264)

[junit] at java.sql.DriverManager.getConnection(DriverManager.java:525)

[junit] at java.sql.DriverManager.getConnection(DriverManager.java:171)

[junit] at org.springframework.jdbc.datasource.DriverManagerDataSource.g

etConnectionFromDriverManager(DriverManagerDataSource.java:156)

[junit] at org.springframework.jdbc.datasource.DriverManagerDataSource.g

etConnectionFromDriverManager(DriverManagerDataSource.java:144)

[junit] at org.springframework.jdbc.datasource.DriverManagerDataSource.g

etConnection(DriverManagerDataSource.java:132)

[junit] at org.springframework.orm.hibernate.LocalDataSourceConnectionPr

ovider.getConnection(LocalDataSourceConnectionProvider.java:59)

[junit] at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFa

ctory.java:84)

[junit] at net.sf.hibernate.cfg.Configuration.buildSettings(Configuratio

n.java:1160)

[junit] at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Config

uration.java:794)

[junit] at org.springframework.orm.hibernate.LocalSessionFactoryBean.new

SessionFactory(LocalSessionFactoryBean.java:475)

[junit] at org.springframework.orm.hibernate.LocalSessionFactoryBean.aft

erPropertiesSet(LocalSessionFactoryBean.java:413)

[junit] at org.springframework.beans.factory.support.AbstractAutowireCap

ableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:990)

[junit] at org.springframework.beans.factory.support.AbstractAutowireCap

ableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:275)

[junit] at org.springframework.beans.factory.support.AbstractAutowireCap

.getBean(AbstractBeanFactory.java:163)

[junit] at org.springframework.beans.factory.support.DefaultListableBean

Factory.preInstantiateSingletons(DefaultListableBeanFactory.java:230)

[junit] at org.springframework.context.support.AbstractApplicationContex

t.refresh(AbstractApplicationContext.java:310)

[junit] at org.springframework.context.support.ClassPathXmlApplicationCo

ntext.<init>(ClassPathXmlApplicationContext.java:80)

[junit] at org.springframework.context.support.ClassPathXmlApplicationCo

ntext.<init>(ClassPathXmlApplicationContext.java:65)

[junit] at org.dudu.dao.BaseDAOTestCase.<clinit>(BaseDAOTestCase.java:35

)

[junit] at java.lang.Class.forName0(Native Method)

[junit] at java.lang.Class.forName(Class.java:164)

[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.

<init>(JUnitTestRunner.java:204)

[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.

<init>(JUnitTestRunner.java:177)

[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.

main(JUnitTestRunner.java:519)

[junit] ** END NESTED EXCEPTION **

Ø 错误分析

从错误报告中分析,错误原因是和Mysql数据没有建立起连接。

我尝试了多种方法,直接改写applicationContext-resources.xml文件、更好最新的mysql

驱动等,都徒劳。

但通过web容器访问,使用Jndi

(1)<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName"><value>java:comp/env/jdbc/appfuse</value></property>

</bean>

(2) ApplicationContext ctx =

WebApplicationContextUtils.getRequiredWebApplicationContext(context);

访问时就可以连接数据库。

不知何故,如有那位兄台知道答案,望不吝赐教。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有