日期:2014-05-19  浏览次数:20865 次

Hibernate初始化配置的问题
大概说下我现在的程序
一个BasaDao,大概如下
Java code
public class BaseDao<T> extends HibernateDaoSupport {

    private Configuration conf;
    private HibernateTemplate ht;
    private SessionFactory sessionFactory;

    public BaseDao() {
        conf = new Configuration().configure();
        sessionFactory = conf.buildSessionFactory();
        ht = new HibernateTemplate(sessionFactory);
    }
各种增删改查方法省略
}



然后有各种Dao继承这个BaseDao,这些Dao都是针对数据库里的每个表实现。
我启动一个定时器,并在初始化方法里,初始化一个Dao,每隔30s会检查缓存,然后取出其中的数据使用,Dao将其存入数据库中的表A,同时我会启动定时器,每隔半小时,将数据库里表A的数据删除,

执行处理数据并保存数据库的线程入下:

Java code

public class VehicleDetectQuartzJob implements Job {

    private VehicleDetectDao dao = null;

    public VehicleDetectQuartzJob() {
        dao = new VehicleDetectDao();
    }

    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        // 此处从缓存中取出数据,然后使用dao将其保存
        }
}



执行删除数据库的定时器类如下:
Java code
public class VehicleDetectQuartzJob implements Job {

    private VehicleDetectDao dao = null;

    public VehicleDetectQuartzJob() {
        dao = new VehicleDetectDao();
    }
        public void execute(JobExecutionContext context)
            throws JobExecutionException {
        // 此处执行调用dao执行删除操作
        }
}



数据库是Oracle11g,但是我运行一段时间后,出现如下错误:
java.sql.SQLRecoverableException: Io 异常: The Network Adapter could not establish the connection
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:101)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:173)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:229)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:458)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:411)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:490)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:202)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:474)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:76)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2006)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1289)
at com.traffic.rest.dao.BaseDao.<init>(BaseDao.java:47)
at com.traffic.rest.dao.VehicleDetectDao.<init>(VehicleDetectDao.java:13)
at com.traffic.mina.thread.VehicleDetectQuartzJob.<init>(VehicleDetectQuartzJob.java:31)
at sun.reflect.GeneratedConstructorAccessor20.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.quartz.simpl.SimpleJobFactory.