Skip to content

Commit

Permalink
add quartz connection provider and check transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreampie committed May 4, 2015
1 parent 2ab5196 commit cf4e999
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 21 deletions.
20 changes: 10 additions & 10 deletions resty-common/src/main/java/cn/dreampie/common/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ public Map<String, Object> getAttrs() {
}

/**
* Set attributes with Map.
* Set attributes with other entity.
*
* @param attrs attributes of this entity
* @param entity the Model
* @return this Model
*/
public M setAttrs(Map<String, Object> attrs) {
for (Map.Entry<String, Object> e : attrs.entrySet())
set(e.getKey(), e.getValue());
return (M) this;
public M setAttrs(M entity) {
return (M) setAttrs(entity.getAttrs());
}

/**
* Set attributes with other entity.
* Set attributes with Map.
*
* @param entity the Model
* @param attrs attributes of this entity
* @return this Model
*/
public M setAttrs(M entity) {
return (M) setAttrs(entity.getAttrs());
public M setAttrs(Map<String, Object> attrs) {
for (Map.Entry<String, Object> e : attrs.entrySet())
set(e.getKey(), e.getValue());
return (M) this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public boolean start() {
}

public boolean stop() {
Metadata.close();
return true;
}

Expand Down
9 changes: 8 additions & 1 deletion resty-orm/src/main/java/cn/dreampie/orm/DataSourceMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public boolean isShowSql() {
* @return 连接对象
* @throws SQLException
*/
Connection getConnection() throws SQLException {
public Connection getConnection() throws SQLException {
Connection conn = connectionTL.get();
if (conn != null) {
return conn;
Expand Down Expand Up @@ -152,6 +152,13 @@ public void endTranasaction() {
}
}

/**
* 关闭数据源
*/
public final void close() {
dataSourceProvider.close();
}

/**
* 关ResultSet闭结果级对象
*
Expand Down
15 changes: 15 additions & 0 deletions resty-orm/src/main/java/cn/dreampie/orm/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ public class Metadata {

private static Map<Class<? extends Entity>, String> tableMetaClassMap = new HashMap<Class<? extends Entity>, String>();

/**
* 关闭所有的数据源
*/
public static void close() {
for (Map.Entry<String, DataSourceMeta> entry : dataSourceMetaMap.entrySet()) {
entry.getValue().close();
}
}

/**
* 判断是不是存在数据源
*
* @param dsName
* @return
*/
public static boolean hasDataSourceMeta(String dsName) {
return dataSourceMetaMap.containsKey(dsName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface DataSourceProvider {
String getDsName();

boolean isShowSql();

void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ public boolean isShowSql() {
return showSql;
}

public void close() {
ds.close();
}

public C3p0DataSourceProvider setShowSql(boolean showSql) {
this.showSql = showSql;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ public void setShowSql(boolean showSql) {
this.showSql = showSql;
}

public void close() {
ds.close();
}

public DruidDataSourceProvider setDriverClass(String driverClass) {
this.driverClass = driverClass;
ds.setDriverClassName(driverClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ public JndiDataSourceProvider setShowSql(boolean showSql) {
this.showSql = showSql;
return this;
}

public void close() {
//do nothing becouse it have no close method
}
}
7 changes: 7 additions & 0 deletions resty-quartz/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
<version>${resty.version}</version>
</dependency>

<dependency>
<groupId>cn.dreampie</groupId>
<artifactId>resty-orm</artifactId>
<version>${resty.version}</version>
<scope>provided</scope>
</dependency>

</dependencies>
<build>
<finalName>${project.name}-v${project.version}</finalName>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cn.dreampie.quartz;

import cn.dreampie.orm.DataSourceMeta;
import cn.dreampie.orm.Metadata;
import org.quartz.utils.ConnectionProvider;

import java.sql.Connection;
import java.sql.SQLException;

/**
* @author Dreampie
* @date 2015-05-04
* @what 为quartz提供数据源
*/
public class QuartzConnectionProvider implements ConnectionProvider {
private DataSourceMeta dataSourceMeta;

public Connection getConnection() throws SQLException {
return dataSourceMeta.getConnection();
}

public void shutdown() throws SQLException {
if (QuartzPlugin.isDsAlone()) {
dataSourceMeta.close();
}
}

public void initialize() throws SQLException {
dataSourceMeta = Metadata.getDataSourceMeta(QuartzPlugin.getDsName());
}
}
38 changes: 32 additions & 6 deletions resty-quartz/src/main/java/cn/dreampie/quartz/QuartzPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.dreampie.common.util.properties.Proper;
import cn.dreampie.common.util.stream.Filer;
import cn.dreampie.log.Logger;
import cn.dreampie.orm.Metadata;
import cn.dreampie.quartz.exception.QuartzException;
import cn.dreampie.quartz.job.QuartzCronJob;
import cn.dreampie.quartz.job.QuartzOnceJob;
Expand All @@ -20,25 +21,50 @@
public class QuartzPlugin implements Plugin {

private static final Logger logger = Logger.getLogger(QuartzPlugin.class);
private static String dsName;
private static boolean dsAlone;
/**
* 默认配置文件*
*/
private String config = "quartz/quartz.properties";

private String jobs = "quartz/jobs.properties";


public QuartzPlugin() {
this(null);
}

public QuartzPlugin(String dsName) {
this(null, null, dsName, false);
}

public QuartzPlugin(String config) {
this.config = config;
public QuartzPlugin(String dsName, boolean dsAlone) {
this(null, null, dsName, dsAlone);
}

public QuartzPlugin(String config, String jobs) {
this.config = config;
this.jobs = jobs;
this(config, jobs, Metadata.getDefaultDsName(), false);
}

public QuartzPlugin(String config, String jobs, String dsName, boolean dsAlone) {
if (config != null) {
this.config = config;
}
if (jobs != null) {
this.jobs = jobs;
}
if (dsName == null) {
throw new IllegalArgumentException("DsName could not be null.");
}
QuartzPlugin.dsName = dsName;
QuartzPlugin.dsAlone = dsAlone;
}

public static String getDsName() {
return dsName;
}

public static boolean isDsAlone() {
return dsAlone;
}

public boolean start() {
Expand Down
2 changes: 1 addition & 1 deletion resty-quartz/src/main/resources/quartz/quartz.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
#==================================================
# \u914D\u7F6E\u6570\u636E\u5E93
#==================================================
#org.quartz.dataSource.db.migration.default.connectionProvider.class = cn.dreampie.common.plugin.quartz.QuartzConnectionProvider
#org.quartz.dataSource.db.migration.default.connectionProvider.class = cn.dreampie.quartz.QuartzConnectionProvider
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ public void intercept(RouteInvocation ri) {
for (DataSourceMeta dsm : dataSourceMetas) {
dsm.rollbackTransaction();
}
throw new TransactionException(t.getMessage(), t);
Throwable cause = t.getCause();
if (cause != null) {
throw new TransactionException(cause.getMessage(), cause);
} else {
throw new TransactionException(t.getMessage(), t);
}
} finally {
for (DataSourceMeta dsm : dataSourceMetas) {
dsm.endTranasaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
public class SessionBuilder {

private final static Logger logger = Logger.getLogger(SessionBuilder.class);

private final static String ANONYMOUS = "anonymous";
private final Sessions sessions;
private final Signer signer;
private final SessionCookieDescriptor sessionCookieDescriptor;
private final Session emptySession;
private final Credentials credentials;
private final static String ANONYMOUS = "anonymous";

public SessionBuilder(long defaultExpires, int limit, int rememberDay, AuthenticateService authenticateService) {
this(defaultExpires, limit, rememberDay, authenticateService, new DefaultPasswordService());
Expand Down

0 comments on commit cf4e999

Please sign in to comment.