日期:2014-05-17 浏览次数:20720 次
public class DBUtil {
private static Logger logger = Logger.getLogger(DBUtil.class);
private static DataSource ds;
private static ThreadLocal<Connection> connLocal = new ThreadLocal<Connection>();
static{
Properties props = new Properties();
try {
props.load(DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
ds = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
logger.error("读取文件出现异常,异常信息为:"+e.getMessage());
}
}
public static Connection getConnection(){
Connection conn = connLocal.get();
if(conn==null){
try {
conn = ds.getConnection();
} catch (Exception e) {
logger.error("获取数据库连接出现异常,异常信息为:"+e.getMessage());
}
connLocal.set(conn);
}
return conn;
}
public static void closeConnection(){
Connection conn = connLocal.get();
connLocal.set(null);
if(conn!=null){
try {
conn.close();
} catch (Exception e) {
logger.error("关闭数据库连接出现异常,异常信息为:"+e.getMessage());
}
}
}
}
Connection conn = null;
try {
conn = DBUtil.getConnection();
conn.setAutoCommit(false);
availJourneyDao.saveAvailJourney(availJourneyModel,conn);
List<Flight> list = aj.getFlightList();
for (Flight flight : list) {
FlightModel flightModel = new FlightModel();
flightModel.setArrivalAirport(flight.getArrivalAirport());
flightModel.setArrivalDate(flight.getArrivalDate());
flightModel.setArrivalTime(flight.getArrivalTime());
flightModel.setAvailJourneyId(availJourneyId);
flightModel.setDepartureAirport(flight.getDepartureAirport());
flightModel.setDepartureDate(flight.getDepartureDate());
flightModel.setDepartureTime(flight.getDepartureTime());
flightModel.setEquipment(flight.getEquipment());
flightModel.setFlightId(UUID.getUUID());
flightModel.setFlightNumber(flight.getFlightNumber());
flightModel.setMarketingAirline(flight.getMarketingAirline());
flightModel.setResBookDesigList(flight.getResBookDesigList());
flightDao.saveFlight(flightModel,conn);
}
conn.commit();
} catch (Exception e) {